1/and//difference:/result reserved decimals, for example 5/3=1.666 and 5//3=1
2 * and * *: in arithmetic operations, the first represents the multiplication and the second represents a power. In string manipulation, the first represents how many identical strings are stitched together, and the second is similar to the following code:
var = 2*3print(Var) # output 6var =2**3print(Var) # output 8 ' Hello ' Print (var*2) # Output Hellohello Print (Str (VAR) **3) # You cannot use this
3 of the three control flow statements in Python, if for while, with this control flow, followed by: end, such as the following test code:
#Test for forIinchRange (1,10): Print(i,end=" ")#Output 1-10#Test if #输出yesvar ='Biao'ifvar = ='Biao': Print('Yes')Else: Print('No')#Testing whileCount = 5 whileCount>=1: Count-= 1Print(count,end=" ")#Output 1-10
4 The use of the switch case does not exist in Python, the test code for if else if else is as follows: Note that elif represents else if:
Number = 23 while1: Guess= Int (Input ('Enter an integer:') ) #input函数将以字符串的形式返回我们输入的内容. We then convert the string to an integer by int. ifGuess = =Number :#the new block starts here . Print('Congratulations, you guessed it.') Print('(but don't win any prizes!)') Break #the new block ends here . elifGuess <Number :#another block of code Print('No, it's a little higher than that') #You can do anything you want to do within that block of code. Else: Print('No, it's a little lower than that') #you have to get here by guessing a number greater than (>) set. Print(' Done')
5 for Loop code in Python: (Range usage)
forIinchRange (1, 5):#Range (1,5) represents left closed right open interval Print(i,end="")Else: Print('\nthe for loop are over') forIinchRange (1, 5, 2):#between [1,5], the step output is 2 Print(i,end="")Else: Print('\nthe for loop are over')#The output results are as follows:1234 the forLoop is Over13 the forLoop isOver
6 usage of while in Python, and use of break, continue
whiletrue:s= Input ('Enter something:') ifs = ='quit': Break #represents exiting while loop, printing last word Input is of sufficient length ifLen (s) < 3: Print('Too Small') Continue #exits this loop, also in while true. Print('Input is of sufficient length')#perform the following testsEnter something:hatoo smallenter something:hahahaenter something:quitinput isof sufficient length
Note: reference book "Byte-of-python-chinese-edition"
Third, Python operators