Python Process Control If-else statement
- Examples of IF statements:
if1<‘a‘:... "chen"...chen
- The logical value (BOOL) is used to denote: right and wrong, true and false, empty and non-empty concepts.
- The logical value consists of two values:
True: Indicates a non-empty amount (such as string,tuple,list,set,dictionary, etc.)
False: Indicates 0,none, empty amount
- Function: Mainly used in judging statements, used to judge
- Whether a string is empty
- Whether the result of an operation is 0
- Whether an expression is available
"0"if a:... "not null"...not null
0if a:... "chen"...else:... "ok!"...ok!
- Else statement can have more than one
#!/usr/bin/python while 1 : x = Int (raw_input ()) if x >= 90 :
print
"x >=" elif
x >= 80 : print " > x >= " elif x >= 60 : print "> x >=" else : print "< 6 0 " break
logical operators
- If the conditional statement after the IF, you can use And,or, or not
- And means two conditions are set up at the same time
- Or have a set up both can
- Inverse of Not condition value
>>>True and FalseFalse>>>1 and 1 and 00>>>True or 0True>>>True or 0 or FalseTrue>>>True and 0 or FalseFalse
For loop
>>> a = (1,3,4,78,12forin a:... print i...1347812forin"ads87":... print i...ads87
Range (I,j, step value)
Note :
I is the starting value, including
J is the terminating value, not included
(similar to slices)
forin range(1,4):... print i...123
- The step values are shown below:
forin range(1,10,2):... print i...13579
Range can produce a sequence:
range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Traverse Dictionary
>>> a = {1:11,2:22,3:33,4:44forin a:... print i...1234
- As you can see, this iterates through the dictionary's key values, so you can access the dictionary by a[the key value].
- In fact, another method is also possible.
>>> A.items () [(1, One), (2, A), (3, -), (4, -)]>>> forIinchA.items ():...Print I...(1, One)(2, A)(3, -)(4, -) >>> forKey,valueinchA.items ():...Print key...Print value...1 One2 A3 -4 -
Loop control
- For also can be paired with else ~
forin range(10):... pass...else:... "end~"...end~
- What are the eggs for?
- Is that the content in else does not appear when the program is not performing properly. Or the break jumps out of the box and does not execute the contents of else.
>>> import time>>> For i in range (10 ): ... Print I ... Time.sleep (2 ) ... else : ... Print "OK" ... 0 1 2 ^ctraceback (most recent call last): File <stdin> ", line 3 , in <module> Keyboardinterrupt
forin range(10):... if4:... break...else:... "end"...
- Break jumps out of the loop
- Continue end this cycle and enter the next cycle
- Exit () end the entire program
- Pass placeholder, equivalent to an empty statement
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Process Control