python 3 kinds of control-flow statements for the

Format is as follows:

# For Item in a data type (strings, lists, tuples, dictionaries, set): 
    # code block

E.g:

= s ' Hello ' 
L = [l, 2,3 ] 
D = { ' Age ' : 18 is, ' name ' : ' Kite ' }
 for Item in s: # for looping through s in the elements, and then assigned to Item 
    Print (Item)
 for Item in L:
     Print (Item)
 for Item in D:
     Print (Item)   # returned Key 
    Print (D [Item]) # print the values

 work1: adding complete list of all data

= L [5,6,7,8,9 ] 
SUM = 0
 for I in L: 
    SUM + = I
 Print ( ' L is the sum of all the elements: ' , SUM) 

# Range (m, n-, K ) m the first n tail (m from zero by default), step k (default 1), take the first generation does not take an integer tail sequence 
Print (List (Range (1,5,2 )))
 Print (List (Range ( 8)))

work2: The index value N, the value of each element print out

N = [5,6,7,8,9]
for s in range(len(N)):
    print(N[s])

work3: using a for loop and a function of range integer addition and completion 1-100 (containing 1,100)

sum1 = 0
v = 0
for v in range(1,101):
    sum1 += v
print('1+2+3+...+100= ',sum1)

# Please use nested loop for generating a right triangle

for ks in range(6):
    for kl in range(1,ks+1,1):
        print('*'* kl)
    print("\n")


for km in range(8):
    print('$'* km)

# Nested loops

L = [['Time','kite','shiguang'],['rice','sleep','swimming','water']]
for k in range(len(L)):
    for x in range(len(L[k])):
        print(L[k][x])

 

Guess you like

Origin www.cnblogs.com/kite123/p/11655970.html