Python Notes

1. Modify the values ​​in the list in turn

a = [1,2,3,4]
for i in range(len(a)):
    a[i] = 1
print(a)#[1,1,1,1]

#This does not work to change temp
a = [1,2,3,4]
for temp in a: # is equivalent to giving the value to temp, and the operation on temp has nothing to do with a
    temp = 1
print(a)#[1,2,3,4], no value modified

2.

a = 4
type(a/2)
<class 'float'>
type(a//2)
<class 'int'>

3.int(False) = 0

int(True) = 1


4. When you need to use the maximum value in the sequence for many times, first calculate the maximum value of the sequence, and then use it. Don't use max(list) every time, it will waste a lot of time when the list length is very long.

5.

a = [True,True,False]
sum(a)
Out[6]: 2

6

a = 001#Error

int("001")=1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326024818&siteId=291194637