Home >Database >Mysql Tutorial >'Python Basics Tutorial' Notes Conditional Statements and Loop Statements

'Python Basics Tutorial' Notes Conditional Statements and Loop Statements

黄舟
黄舟Original
2016-12-20 17:23:051090browse

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([])

False

>>> 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 0>>> assert 5

Traceback (most recent call last):

File "", line 1, in

assert 5AssertionError

Loop


range -- built-in range function, which includes the lower limit but not the upper limit, such as

>>> range(0,10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for num in range(0, 10): PRint num,

The result is as follows

>>>

0 1 2 3 4 5 6 7 8 9

Loop through the dictionary, you can use sequence unpacking, such as

d = {'x':1, 'y':2}for key, value in d.items(): print key, 'corresponds to', value


result

> It can cope with sequences of unequal lengths and will stop when the shortest sequence is "used up", such as


>>> zip(range(5), xrange(10000))

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

>>> names=['a', 'b', 'c']

>> ;> ages = [45, 23,98]

>>> zip(names, ages)
[('a', 45), ('b', 23), ('c', 98) ]

Parallel iteration, such as

names=['a', 'b', 'c']

ages = [45, 23,98]for name, age in zip(names, ages): print name, ' is', age, 'old'


result

>>>

a is 45 old

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=['Mr.a', 'Ms.b', 'Mr.c']for index, name in enumerate(names): if 'Mr' in name:

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!')

[ ' ', '!', ',', 'd', 'e', ​​'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w' ]

>>> list(reversed('hello, world!'))
['!', 'd', 'l', 'r', 'o', 'w', ' ', ' ,', 'o', 'l', 'l', 'e', ​​'h']
>>> ''.join(reversed('hello, world!'))

'!dlrow , olleh'

break/continue -- Break out of the loop/continue to the next cycle


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 loop

List 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)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn