Python3 cycle

The general form in Python while statement:

while judgment conditions:

       Statement

 

Also note the colon and indentation, the other did not do ... while loop in Python

The following Examples 1 to 100 is calculated sum

##calc.py
n = 100

sum = 0
counter = 1
while counter <= n:
    sum = sum + counter
    counter += 1

print("total from 1 to 100 : %d",sum)

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3.5 calc.py

total from 1 to 100 : %d 5050

 

while loop using else statements

Els execute a block of statements in the conditional statement is false in a while ... else

#while.py
count = 0
while count < 5:
    print(count," < 5")
    count = count + 1
else :
    print(count ," >= 5")

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3.5 while.py

0  < 5

1  < 5

2  < 5

3  < 5

4  < 5

5  >= 5

 

for loop:

Python for loop can iterate any sequence of items, such as a string or a list of

It follows the general format for loop

for <variable> in <sequence>:
    <statement>
else:
    <statement>

Example:

statement is used to break out of the current loop:

##break.py
sites = ["Baidu","Google","Runoob","Taobao"]
for site in sites:
    if site == "Runoob":
        print("cainiao!")
        break
    print("loop data " + site)
else:
    print("Having no loop data!")
print("loop end!")

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3.5 break.py

loop data Baidu

loop data Google

cainiao!

loop end!

range () function

If you need to traverse a sequence of numbers, you can use the built-in range () function, it will generate a number of columns, for example:

 

 

You can also specify the range to start with a number and specify a different increment (even negative, sometimes also called steps)

 

  

negative number:

 

 

It can also be combined range () and len () function to traverse an index of a sequence:

 

 

 

You can also create a list using the range () function:

 

 

 

break and continue statements and loop else clause

break statement can jump for and while loop, or if you break out for while loop, any corresponding loop else block will not be executed:

#else.py
for letter in 'Runoob':
    if letter == 'b':
        break;
    print('the current letter : ',letter)

print("the next example")

var = 10
while var > 0:
    print('the current variable : ',var)
    var = var - 1
    if var == 5:
        break;
print("GOOF bye!")

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3 else.py

the current letter :  R

the current letter :  u

the current letter :  n

the current letter :  o

the current letter :  o

the next example

the current variable :  10

the current variable :  9

the current variable :  8

the current variable :  7

the current variable :  6

GOOF bye!

 

Python continue statement is used to bounce the remaining statements in the current cycle block, and then continues the next cycle.

Loop else clause can have it in an exhaustive list (for cycling) or a condition becomes false (with while loop) cause to be executed when the loop terminates, but does not perform the loop is terminated break.

The following is an example query cycle of prime numbers:

##prime.py
for n in range(2,10):
    for x in range(2,n):
        if n % x == 0:
            print(n," == ",x, '*', n//x )
            break
    else:
        print(n," is prime")

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3 prime.py

2  is prime

3  is prime

4  ==  2 * 2

5  is prime

6  ==  2 * 3

7  is prime

8  ==  2 * 4

9  ==  3 * 3

 

The pass statement

Python pass is an empty statement, in order to maintain the integrity of the program structure.

pass without doing anything, commonly used as a placeholder statement:

#pass.py
for letter in 'Runoob':
    if letter == 'o':
        pass
        print('execute pass block')
    print('the current letter : ',letter)

print("Good bye!")

operation result:

 robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3 pass.py

the current letter :  R

the current letter :  u

the current letter :  n

execute pass block

the current letter :  o

execute pass block

the current letter :  o

the current letter :  b

Good bye!

 

pass only to prevent a syntax error

pass is a null statement, when the code segment or a defined function, if there is no content, or without any treatment on the first, skip, can be used to pass

 

Decimal:

#translate.py
while True:
    number = input('please input a integer(enter Q exit ):')
    if number in ['q','Q']:
        break
    elif not number.isdigit():
        print("input error,please continue input : ")
        continue
    else:
        number = int(number)
        print("decimal --> hexadecimal: %d -> 0x%x"%(number,number))
        print("decimal --> octonary: %d -> 0x%o"%(number,number))
        print("decimal --> binary: %d -> "%number,bin(number))

operation result:

robot @ ubuntu: ~ / wangqinghe / python / 20190826 $ python3 translate.py

please input a integer(enter Q exit ):9

decimal --> hexadecimal: 9 -> 0x9

decimal --> octonary: 9 -> 0x11

decimal --> binary: 9 ->  0b1001

please input a integer(enter Q exit ):18

decimal --> hexadecimal: 18 -> 0x12

decimal --> octonary: 18 -> 0x22

decimal --> binary: 18 ->  0b10010

please input a integer(enter Q exit ):q

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11414083.html