Home >Database >Mysql Tutorial >'Python Basics Tutorial' Notes Conditional Statements and Loop Statements
Boolean variables
The following values will be interpreted as false by the interpreter:
False None 0 "" () {} []
Everything else is interpreted as true.
>>> True
True
>>> False
False
>>> >> True + False +42
43
bool function -- used to convert other values, such as
>>> bool([])
>>> bool('hello ,world')
True
conditional statement
if else elif
is and is not -- determine whether two variables are the same object
>>> x=y=[1,2,3 ]
>>> z=[1,2,3]>>> x == y
True>>> x is y
True
>>> x is z
False
It can be seen in the above example, because the is operator determines identity. The variables x and y are both bound to the same list, and the variable z is bound to another list with the same value and order. Their values may be the same, but they are not the same object.
in and not in -- membership operator
assert -- program crashes when condition is not true
>>> x = 5
>>> assert 0Traceback (most recent call last):
File " assert 5
range -- built-in range function, which includes the lower limit but not the upper limit, such as
>>> range(0,10)
The result is as follows
0 1 2 3 4 5 6 7 8 9
>>> zip(range(5), xrange(10000))
>>> names=['a', 'b', 'c']
>> ;> ages = [45, 23,98]>>> zip(names, ages)
[('a', 45), ('b', 23), ('c', 98) ]
ages = [45, 23,98]for name, age in zip(names, ages): print name, ' is', age, 'old'
result
>>>
b is 23 old
c is 98 old
numbered iteration -- iterate over the objects in the sequence while also Get the index of the current object, such as
names[ index] = 'nan'for name in names: print name,
result
>>>
nan Ms.b nan
flipped and sorted iteration (sorted and reversed) -- Scope any sequence or reversible On the iterative object, instead of modifying the object in place, a flipped or sorted version is returned. However, the returned object cannot directly use indexing, sharding, or call the list method on it. You can use the list type to convert the returned object, such as
> ;>> sorted([4,3,8,6,3,])
[3, 3, 4, 6, 8]
>>> sorted('hello, world!')
>>> list(reversed('hello, world!'))
['!', 'd', 'l', 'r', 'o', 'w', ' ', ' ,', 'o', 'l', 'l', 'e', 'h']
>>> ''.join(reversed('hello, world!'))
The else clause in the loop -- If break is not called in the loop, the else clause is executed, such as
from math import sqrtfor n in range (99, 81, -1):
root = sqrt(n) if root == int(root): print n breakelse: print "Didn't dind it!"
Result
>>>
Didn't dind it!List comprehension - lightweight loopList comprehension is a way to create a new list using other lists, such as
>>> [(x,y) for x in range(3) for y in range(3)]
[(0 , 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2 )]
>>> girls = ['alice', 'bernice', 'clarice']
>>> boys = ['chris', 'arnold', 'bob']
>> ;> [b+'+'+g for b in boys for g in girls if b[0] == g[0]]
['chris+clarice', 'arnold+alice', 'bob+bernice']
The above is the content of conditional statements and loop statements in the notes of "Python Basic Tutorial". For more related content, please pay attention to the PHP Chinese website (www.php.cn)!