Conditional statements and looping statements in the basic Python tutorial notes

Source: Internet
Author: User

    • Boolean variable

The following values are interpreted as false (false):

False None 0 "" () {} []

Everything else has been interpreted as true.

>>> True
True
>>> False
False
>>> True = = 1
True
>>> False = = 0
True
>>> True + False +42
43

bool Function--used to convert other values, such as

>>> bool ([])
False
>>> bool (' Hello,world ')
True

    • Conditional statements

If Else elif

Is and is not--determine if two variables are not the same object

>>> x=y=[1,2,3]
>>> z=[1,2,3]
>>> x = = y
True
>>> x = = Z
True
>>> x is y
True
>>> X is Z
False

This is visible in the example above, because the IS operator is a judgment identity. The variables x and Y are bound to the same list, and the variable z is bound to another list with the same values and order. Their values may be the same, but they are not the same object.

In and not in--membership operators

Assert-The program crashes when the condition is not true

>>> x = 5
>>> assert 0<x<10
>>> assert 5<x <4

Traceback (most recent):
File "<pyshell#25>", line 1, in <module>
Assert 5<x <4
Assertionerror

    • Cycle

Range-The built-in range function, which contains the lower bound, but does not contain the upper bound, as

>>> Range (0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 for  in range (0, ten):    print num,

The results are as follows

>>>
0 1 2 3 4 5 6 7 8 9

Iterating through a dictionary, you can use sequence unpacking, such as

D = {'x'y': 2 }for in D.items ( ):    print'correspondsto', value

Results

>>>
Y corresponds to 2
x corresponds to 1

Zip--You can "compress" any number of sequences together, and then return a list of tuples, and he can handle unequal sequences that stop when the shortest sequence "runs out", 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 ', '), (' B ', '), (' C ', 98)]

Parallel iterations, such as

names=['a'b'c'= [45 , 23°c, 98] for in zip (names, ages):    print'  is ' '  Old '

Results

>>>
A is
B is
C is 98 old

Number iteration--iterate over the object in the sequence, and also get the index of the current object, as

names=['MR.A','ms.b','mr.c'] forIndex, nameinchEnumerate (names):if 'Mr' inchName:names[index]='nan' forNameinchnames:PrintName

Results
>>>
Nan ms.b nan

Flip and sort iterations (sorted and reversed)--scope any sequence or iterator object, instead of modifying the object in situ, instead returns the flipped or sorted version, but the returned object cannot directly use indexes, shards, and calls to the list method. You can use the list type to convert the returned objects, 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--Jump out of the loop/continue the next cycle

Else clause in the loop-if no break is called in the loop, the ELSE clause executes, as

 from Import sqrt  for  in range (1):    = sqrt (    N)if root = = Int (root):         Print N          Break Else  :     Print " didn ' t dind it! "

Results

>>>
Didn ' t dind it!

    • List derivation--lightweight loops

A list derivation is a way to create a new list with 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)]
>>> 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 ']

Conditional statements and looping statements in the basic Python tutorial notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.