Python's data types are briefly described in the previous sections, and then we'll talk about Python's control-judgment loop.
In real-world programming, we often use computers to help us do a lot of repetitive calculations, in which case, the machine can judge a condition, or a behavior to repeat the operation
Then we have to know how to write a circular judgment statement.
If ... else elif
If that is the meaning, after the need to add a judgment condition, if the condition is true, then perform if the operation, if False then skip the operation
Note that a colon is appended to each judgment condition, and the statement below the if is to be aware of the indentation
In [1]: num = 20In [2]: If num >: ...: print ("condition") ...: condition established
Tips: The difference between "=" and "= =": A general equals sign is an assignment that can only be used to compare with the object name in a loop, and the "= =" Double equals sign can be used directly to compare values, such as numbers, and strings
For example
In [3]: name = "Susmote" in [4]: if name = = "Susmote": ...: print ("name is Susmote") ... : name is Susmotein [5 ]: If name = "Susmote": #如果不用 "= =" Comparison value, the syntax error will be reported ... : print ("name is Susmote") ...: File "< Ipython-input-5-06510f3ebd56> ", line 1 if name =" Susmote ": ^syntaxerror:invalid syntax
The other relational operators are as follows
Greater than or equal to >=
Less than or equal to <=
elif is called "else if" in other languages, Python simplifies this expression, elif is generally used to judge multiple expressions, that is, in a judgment statement can have more than elif, which is a bit similar to other language switch case, Plus else, of course.
In [8]: num = 20In [9]: If num >: .: print ("num greater than") ... : elif num = = ...: print ("Nu M equals ") ... : Elif num <: ...: print (" num less than ") ... : num equals 20
else The following execution statement executes only if the preceding conditions are not met, and else is optional in a judgment statement, but for the program to run more clearly, it is advisable to add
A complete if, elif ... else statement
num = input ("Please enter a number not greater than 5: ") num = int (num) if num = = 0: print ("This number is 0") elif num = = 1: print ("This number is 1") elif num = = 2: print ("This number is 2") elif num = = 3: print ("This number is 3") elif num = = 4: print ("This number is 4") elif num = = 5: print (" This number is 5 ") Else: print (" The number you entered does not meet the requirements ")
Run results
[[email protected] ~]# Python3 a.py Please enter a number not greater than 5: 3 This number is 3[[email protected] ~]# Python3 a.py Please enter a number not greater than 5: 14 The number you entered does not meet the requirements
While
The correct definition of the while loop is that while () parentheses need to add a judgment condition, if the inside of the judgment is true, then always execute the following statement, know while the judgment condition is false to jump out of the loop
In [1]: num = 10In [2]: while (num > 0): ...: print (num) ... : num-= 1 ...: 10987654321
Of course, if the condition has always been true to form a dead loop, the inside of the statement will always be executed, the general procedure or try not to appear in the dead cycle, the death cycle is very consumption of system resources
Let's talk about the Boolean types commonly used in conditional judgments:
Boolean type only true and false two values, true or flase
In general, all strings are true, and only 0 of the numbers represent false false.
In [All]: while (0): ...: print ("condition is false, do nothing") ... :
For
For loops in other languages, such as C,java, can only be iterated by numbers, and in Python you can get values for the for loop of any sequential data, such as lists and strings
The statement structure for the for loop in Python for the item in sequence item represents each value obtained, sequence represents a recyclable data, list, or string
For example
in [+]: info = ["Susmote", "+", "13488888888"]in []: For item in Info: ...: print (item) ... : susmote1 813488888888
If you are working on a list, you can also make other changes to it, such as sorting the elements in the list by the For loop (the most primitive way, judging each)
in [+]: For i in num: ...: if I < num[0]: ...: num.remove (i) ... : num.insert (0, i) ... : elif i < num[1]: ...: num.remove (i) ...: num.insert (1,i) ... : elif i < NUM[2]: ...: num.remove (i) ...: num.insert (2, i) ... : elif i < num[3]: ...: num.remove (i) ...: num.insert (3, i) ... : elif i < num[4]: ...: num.remove (i ) ... : num.insert (4,i) ... : in []: numout[29]: [1, 3, 5, 6, 7, 9]
Subsequent in-Talk sorting algorithm
Range ()
If you need to generate a regular set of numbers repeatedly, then you need to use the range function, he can generate progressive numbers
For example, in combination with a For loop
in [+]: For I in range (5): ...: print (i) ...: 01234
The default is the generation of iterations starting with 0.
Range can pass three parameters range (A, B, C) a represents the starting number, B is the ending number, and C is the increment (can be negative)
Only one argument is passed when the range method is called, a defaults to 0, B is the parameter you passed in, and C defaults to 1.
Call the Range method simultaneous two parameters, A is the first parameter, B is the next parameter, C defaults to 1, remember that Baotou does not wrap tail
For example
In [all]: For I in range (2, 6): ...: print (i) ...: 2345In [MAX]: For I in range (2, 2): ...: prin T (i) ... : 2468
Of course, it can be negative.
in [+]: For I in range ( -10, -80, -10): ...: print (i) ...: -10-20-30-40-50-60-70
For a list, you can also traverse his index to get the value
In []: weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunda ...: Y"]in []: For I In range (len (weekdays)): ...: print (i, weekdays[i]) ... : 0 Monday1 Tuesday2 Wednesday3 Thursday4 Friday5 Saturday6 Sunday
Generally when we do this, we use the enumerate () method in order to be more convenient, and he can iterate directly to build the index.
The value returned by range () is not a list, but an object, which we call an iterative object.
Not only can we output the value of the object iteration through the for loop, but we can also directly generate the list by using list ()
In [MAX]: print (range) range (0, ten) in [Max]: List (range) out[48]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
07_python control Judgment Loop Statement 1 (if judgment for loop) _python programming path