python memo

When writing a function, the default parameters must be written at the end! ! !

So how can a global variable modified in the local be called externally? First, we define a global variable externally  a=None, and then  fun() declare that this  a is from the outside  a. The declaration method is  global a. Then  a after modifying the external, modify The effect of will be applied to the external one  a . So we will be able to see that after the run  fun()a the value of is  None changed from  20.


Class defines the first letter of the class to be capitalized (this is the class by default) 

When running the class, add ()


ListAdd 

a = [1,2,3,4,1,1,-1]
a.append(0) # Append a 0 at the end of a
a = [1,2,3,4,1,1,-1]
a.insert(1,0) # add 0 at position 1

ListRemove

a = [1,2,3,4,1,1,-1]
a.remove(2) # remove the first occurrence of the item in the list with the value 2

List index

a = [1,2,3,4,1,1,-1]
print(a[0]) # Display the value of the 0th position of the list a
# 1

print(a[-1]) # Display the value of the last digit of list a
# -1

print(a[0:3]) # Display the value of all items in list a from position 0 to position 2 (before position 3)
# [1, 2, 3]

print(a[5:]) # Display the value of all items in the 5th and later of the list a
# [1, -1]

print(a[-3:]) # Display the value of all items in the 3rd place from the bottom of list a and beyond
# [1, 1, -1]
a = [1,2,3,4,1,1,-1]
print(a.index(2)) # Display the index of the first item in list a with value 2
# 1
a = [4,1,2,3,4,1,1,-1]
print(a.count(-1))
# 1

List sort

a = [4,1,2,3,4,1,1,-1]
a.sort() # Sort by default from small to large
print(a)
# [-1, 1, 1, 1, 2, 3, 4, 4]

a.sort(reverse=True) # Sort from big to small
print(a)
# [4, 4, 3, 2, 1, 1, 1, -1]


Module storage path description:

On Mac systems, downloaded python modules are stored in an external pathsite-packages

                        Library/Framworks/Python.framework/Versions/3.6/lib


zipThe function accepts any number of sequences (including 0 and 1) as parameters, and returns a list after combiningtuple

a=[1,2,3]
b=[4,5,6]
ab=zip(a,b)
print(list(ab)) #Need to add list to visualize this function
"""
[(1, 4), (2, 5), (3, 6)]
"""


lambdaDefine a simple function to implement the function of simplifying the code, and the code will be better understood.

fun = lambda x,y : x+y, Before the colon x,yis the independent variable, after the colon x+yis the specific operation.


fun= lambda x,y:x+y
x=int(input('x=')) #define int integer here, otherwise it will default to string
y=int(input('y='))
print(fun(x,y))

"""
x=6
y=6
12
"""

mapis to bind functions and parameters together.

>>> def fun(x,y):
	return (x+y)
>>> list(map(fun,[1],[2]))
"""
[3]
"""
>>> list(map(fun,[1,2],[3,4]))
"""
[4,6]
"""



Guess you like

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