Python - Demo6-- flow control statements

1, control conditions:

if..else statement

Syntax: if keyword, condition, colon, the next line indented code block, else keyword, a colon, the next line indented code blocks

Example:

# IF the else statement
age = 23
IF Age> 18 :
     Print ( ' we are all adults, speak .. ' )
     Print ( ' adult' )
 the else :
     Print ( ' Do not be led astray minors ' )

Output:

We are all adults, speak ..
Adults

if..elif..else .. statement

Syntax: if keyword, condition, colon, the next line indented code blocks, elif keyword, condition, colon, indenting the next line of code faster

Example:

= 23 Age
 # more elif => abbreviation for else if (python is really the province to province) 
IF Age> 18 :
     Print ( ' adults, come in ... ' )
 elif Age> 12 :
     Print ( ' middle school students ' ) elif Age>. 6 :
     Print ( ' primary ' ) the else :
     Print ( ' you are A Baby ' )

Output:

Adults, come in ...

Note: colon marks the emergence of the next line needs to be indented.

2, the control loop

while statement:

Syntax: while, conditions of the colon, the next line of the code block start indented

Example:

# To find out all the odd 1 to 100 and 
jishuhe = 0   # loop variable initial value 
initValue = 0
 the while initValue <100:   # must cycle condition 
    IF initValue% 2 =! 0:
         Print ( ' odd: D% ' % initValue )
        jishuhe + = initValue
    initValue +. 1 =   # Change loop variable 
Print (jishuhe)

Output:

Odd: an 
odd number: 3 
odd: 5 
odd: 7 
Odd: 9 
....

Description: as long as the condition is true pieces of code that will not stop; while loop change loop variables in need each execution of the code block, to advance the end of the while loop, otherwise it becomes a cycle of death.

fro ... in statement:

Syntax: for keywords, variable names, in keywords, a iterable, colon, the next line indented code blocks

Example 1:

# Calculated and added to a 10 
SUM = 0
 for Item in [1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10]:   # Do not forget colon 
    SUM = SUM + Item
 Print (SUM)

Output:

55

Example 2:

# For in list or can be iteratively tuple 
Students. = [ ' BOGE ' , ' minshener ' , ' yezi ' , ' Yanchang ' ]
 for Student in Students.:
     Print (Student)

Output:

boge
minshener
yezi
yanchang

Example 3:

# Python Providing Rang (n) function capable of generating a sequence of zero to an integer smaller than n by the function of the sequence list () becomes a listing 
oneTo100 = list (Range (101))   # here does not have to write the write 100 101 
Print (len ( oneTo100))
count = 0
for item in oneTo100:
    COUNT + = Item
 Print ( ' and is 1 to 100 D%' % COUNT)

Output:

It is 1 to 100 and 5050

More use for in range () is very fragrant

>>> for item in range(5):
...     print(item)
...
0
1
2
3
4

3, range () function

Syntax: Range () has three parameters, range () function to generate the sequence can not be used directly.

Usage scenarios: either use for in the; either have to use list (range ()) is converted to a list in order to use

The number of parameters:

  • A write-only parameter range (n) - represents the sequence generated from 0 to n-1,
  • Write only two parameter range (n, m) - indicates the serial from the n to m-1
  • Three write parameter range (n, m, p) - indicates the serial sequence increased from n to m-1 is the step size p

Example:

>>> range(5)
range(0, 5)
>>> range(1,5)
range(1, 5)
>>> print(range(1,5))
range(1, 5)
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,5))
[1, 2, 3, 4]
>>> list(range(1,5,2))
[1, 3]
>>> list(range(1,100,2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, ..., 89, 91, 93, 95, 97, 99]

Explanation: see following range () call does not return values ​​from the above example. We have to use list () to convert it to a list to use.

In fact, range () returns after the call is an iterator, the iterator is valid? Generally speaking about, as long as can be for in the iterative iterators. More detailed later.

Guess you like

Origin www.cnblogs.com/bigbosscyb/p/12318599.html