Important concepts of Python3

Among the six standard data types of Python3:
immutable data (3): Number (number), String (string), Tuple (tuple);
variable data (3): List (list), Dictionary (dictionary) ), Set.

Number (number) includes: int, float, bool, complex (plural)

listDemo = ['fd', 467, 'ng', 949]
tupleDemo = (1,3, 'jj')
setDemo = {'fdjj', 2,2,1}
dictDemo = {1: 33, 'fdjj': 827,213: 'fdjjj'}

list1=[1,3,4,5]
print(id(list1))
#View memory address list1=['fdjj',28,'fjdjj']
print(id(list1))

The two output addresses are different

List list: multiple lists can be spliced, elements can be modified
list1=[4,'eja',3]
list2=['3ad',5,8,1]
list3=list1+list2
list[5]='93jfdh'
del list [5]
list3[3:6]=[]


Tuples are similar to lists. The difference is that the elements of tuples cannot be modified. tupel1=(5,'fdjj','9aak',817)
tupel2=tuple(list1)
tupe1[2]=183 #illegal
del tuple [6] #illegal

Different types can be
converted to each other: list1=[1,3,4,1,3,5]
tuple1=('21jf',1,3,5,1,5)
set1={'vncd',1,69}
dict = {'name': list1,'likes': 123,'url': tuple1}
list2=list(tuple1)
list3=tuple(set1)
set2=set(list1)
set3=set(dict)

Arithmetic operators:
division / and // difference, / result is floating point, // result is integer
variable length parameters: parameters
with an asterisk * will be imported in the form of tuples, storing all unnamed Variable parameter

Writable function description

def printinfo( arg1, *vartuple ):
"Print any incoming parameters"
print ("Output: ")
print (arg1)
print (vartuple)

Call printinfo function

printinfo( 70, 60, 50)
output:
70
(60, 50)

Parameters with two asterisks ** will be imported in the form of a dictionary.
def printinfo( arg1, **vardict ):
"Print any incoming parameters"
print ("Output: ")
print (arg1)
print (vardict)

Call printinfo function

printinfo(1, a=2,b=3)
output:
1
{'a': 2,'b': 3}

lambda creates an anonymous function.

Writable function description

sum = lambda arg1, arg2: arg1 + arg2

Call the sum function

print ("Added value: ", sum( 10, 20 ))
print ("Added value: ", sum( 20, 20 ))

The problem with the following code is that when the execution is complete, the file will remain open and not closed
for line in open("myfile.txt"):
print(line, end="").

The keyword with statement can ensure that objects such as files will be executed correctly after use:
with open("myfile.txt") as f:
for line in f:
print(line, end= "")

After the above code is executed, even if there is a problem in the processing, the file f will always be closed.

Guess you like

Origin blog.csdn.net/weixin_51417950/article/details/114810467