Loop: Action List

Traverse the list

bis= ["sss", "ass222","kjjga","dksjkgj"]
for xx in bis :
    print(xx)



sss
ass222
kjjga
dksjkgj

Circulating temporary storage suggestions:

for cat in cats:
for dog in dogs:
for item in list_of_items: 

Doing More cycle:

bis= ["sss", "ass222","kjjga","dksjkgj"]
for xx in bis :
    print(xx + "  你好" "\n")
print("loop  is end ")    


sss  你好

ass222  你好

kjjga  你好

dksjkgj  你好

loop  is end 

Avoid indentation error

The Python indentation lines to determine the relationship between the previous line of code
after the unnecessary indent / forget indentation / indent unnecessary cycles / colon omissions

Create a list of values

List is very suitable for storing digital set
range () function

for xx in range(1,5):
    print(xx)
1
2
3
4

Use range () to create a list of numbers

xxx = list(range(1,5))
print(xxx)
for x in xxx:
    print(x)


 for value in range(1,11): 
        xx = value ** 2
        print(xx)

Perform simple statistical calculations of a list of numbers

>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45 

List comprehension

squares = [value**2 for value in range(1,11)]
print(squares) 

Guess you like

Origin www.cnblogs.com/g2thend/p/11729796.html