First, the data type
The most common data types are: integer (int), float (float), string (STRs). For example: #整型 a=8 #浮点型 b=8.0 #字符串 c= ' Hello World '.
Conversion of data types: variables of different data types cannot be compared or calculated, need to be converted before you can view the data type of variable A, use print (type (a)). For example: #字符串转整型 a= ' 1 ' a =int (a) #整型转字符串 a=1 a=str (a) #整型转浮点型 a=1 a=float (1) floating-point rounding can be used with the round function.
Placeholder use: Sometimes printing a sentence contains a few variable stitching, the middle with a plus sign connection is more troublesome, you can use the placeholder. For example:%s string%d integer%f is decimal
msg = ' Welcome%s ' login, today is%s '% (user,today)
Second, list (array)
Arrays have one-dimensional arrays, multidimensional arrays, and array subscripts starting with 0. Additions and deletions of arrays, examples:
List=[' A ', ' B ', ' C ']
#增加 (add an element at the end of the list) List.append (' d ') operation result: [' A ', ' B ', ' C ', ' d ']
#增加 (add an element at the specified position) List.insert (2, ' d ') results: [' A ', ' B ', ' d ', ' C ']
#删除 (Delete the element at the specified position) List.pop (2) or del list[2] run result: [' A ', ' B ']
#删除 (delete the specified element) List.remove (' a ') run result: [' B ', ' C ']
#删除 (empty entire list) List.clear ()
#改 list[0]= ' A1 ' run Result: [' a ' 1, ' B ', ' C ']
#查 List[-1] is now labeled-1 o'clock takes the last element List.count (' a ') is a statistic a This element appears in the list a few times
#其他用法 list.count (' A ') is statistic a which appears several times in the list, List.index (' a ') is the subscript that returns the first occurrence of this element, List.reverse () is reversed, and the elements in the array are arranged in turn,
List1.extend (List2) is to add list2 elements into the List1, List.sort (reverse=true) the list of elements in descending order, without reverse=true is the default ascending order
Multidimensional arrays: my=[[1,2,3,4,5,6],[' name ', ' age ', ' sex ', ' haha ', [' xiaoming ', ' Little black ', ' Little white ']],890]
Print (my[1][4][0]) run result: Xiao Ming
My[1][4].append (' Little Violet ') running results: [[1, 2, 3, 4, 5, 6], [' Name ', ' age ', ' sex ', ' haha ', [' xiaoming ', ' Little black ', ' Little white ', ' Little Violet '], 890]
my[1][2]= ' sex ' running results: [[1, 2, 3, 4, 5, 6], [' Name ', ' age ', ' gender ', ' haha ', [' xiaoming ', ' Little black ', ' Little White '], 890]
Iii. Conditions of Judgment
Python conditional judgments are basically if-elif-else, examples:
If score>=90:
Print (' You are excellent ')
Elif score<90 and score>=80:
Print (' good ')
Elif score<80 and score>=60:
Print (' Pass ')
Else
Print (' fail ')
Iv. circulation
Loops are repeated execution of code in the loop body, with a while loop and a for loop in Python. The while loop must have a counter.
Example 1 (while loop):
Import Random
num = random.randint (1,100) #产生一个随机的数字
Count = 0
While count<7:
Guess = input (' Please enter your guess number: ')
guess = Int (guess)
If Guess>num:
Print (' Big up ')
Elif Guess<num:
Print (' Guess small ')
Else
Print (' Congratulations on your guess right ')
Break #立即结束整个循环, the bottom-most else will not execute
Count=count+1
else: #循环正常结束之后, the else is executed
Print (' The number of games has been exhausted, please recharge ')
Example 2 (For loop):
num = 10
For I in range (3): #循环三次
Guess = input (' Please enter your guess number: ')
guess = Int (guess)
If guess > num:
Print (' Big up ')
Elif Guess < num:
Print (' Guess small ')
Else
Print (' Congratulations on your guess right ')
Break # immediate End loop
else: #循环正常结束之后执行的
Print (' ran out of games ')
Notice the use of break and continue in the loop, and break is the immediate end of the entire loop, and continue is the end of the cycle, into the next loop.
Python data types, conditional judgments, loops