Class Notes


Class Notes

The standard library does not need to be installed, the library that can be imported directly, comes with Python

Third-party library, you need to download and install it to use the module

1. sys module import sys print(sys.path)# When printing environment variables to import modules, first search in the current directory, then in site-package, and then in global environment variables, if there is no imported module name, then Error print(sys.argv)#Print script relative path print(sys.argv[2])#Get the third parameter passed in by the user

2. os module import os os.system("dir")#Execute the command without saving the result cmd_res=os.popen("dir").read()#The result can be saved after executing the command os.mkdir("test_dir")# Create a directory

3. What is .pyc? Python is a language that is compiled first and then interpreted. When executing, the pyc file is searched first. There is a direct call. If it does not exist, the compilation result is saved in the memory. After the program runs, it is saved to the pyc file. middle

4. Data type

4-1 Numeric integer, long integer, floating point, complex complex long integer: Python's long integer does not specify a bit width, but it is not infinite scientific notation: 50000=5E4=5*10**4

4-2 Boolean 1 or 0 True or False

4-3 Strings

4-4bytes data type encoding conversion (string<-->bytes) encode('utf-8') encodes into binary, before encoding is utf-8 decode('utf-8') decodes with utf-8 for example : Encoding: 'aa'.encode('utf-8') b'\xe2\....' Decoding: b'\xe2\....'.decode('utf-8') 'aa'

5. Ternary operator

6. Hexadecimal representation method, suffix H or prefix ox

7. List name=['aa','bb','cc','dd'] name[-2:]# Take out all the items from the penultimate

name[:2]#From the first to all elements with subscript 2

Add name.append("ee")

addition

name.insert(2,"ff") #Insert ff after subscript 1

Modify name[2]='gg

' Delete name.remove('gg')# delete the specified element directly

del name[1]#Delete by subscript name.pop()#Do not specify parameters to pop up the last name.pop(1)#Pop up the element with subscript 1 to find the element position name.index('aa') Empty the list name .clear() sorting name.sort()#Default ASCII sorting name.extend(list)#Merge list import copy shallow copy (copy memory address) and deep copy (completely independent copy) copy.copy() and copy. deepcopy() list loop for i in list: print(i) step slice print(name[0:-1:2])#jump slice, step 2 8, tuple list=['a','b ','c'] for i in enumerate(list): print(i) #Print list and subscript values ​​(0,a) (1,b) (2,c) 9. String name='colby' name .capitalize#Capitalize the first letter>>Colby name.center(50,'-')#Print 50 characters, if the length is not enough, use '-' to fill in both ends name.ljust(50,'-')#Print 50 characters , if the length is not enough, use '-' to make up name.rjust(50,'-')#Print 50 characters, if the length is not enough, use '-' to fill in the right >>---colby--- name.endwith('by ')#Determine what character ends with >>True name.expand()#How many characters the tab key turns into name.find('y')#Return the index of the character >>4 name.rfind('c')#Find The rightmost specified character >>name='colby{name},{age}' name.format(name='colby',age=22) name.format_map({'name':'colby','age':22}) The incoming value will be brought to the name variable by default name.isalnum()# Judging whether it is Arabic numerals and characters name.isalpha()# Judging whether it is a pure English character name.isdecimal()# Judging whether it is a decimal name.isdigit()# Judging whether it is an integer name.isidentifier()# Judging whether it is a legal Identifier variable name.islower()#Judging whether it is lowercase name.isupper()#Judging whether it is uppercase name.isnumeric()#Judging whether it is an integer join usage print(','.join(['a',' b','c']) >>a,b,c #Convert the list to characters, separated by commas name.lower()#Convert to lowercase name.upper()#Convert to uppercase name.strip()#Remove Newline characters at both ends name.lstrip()#Remove the left newline character name.rstrip()#Remove the right newline character ,'1234567')#map abcdefg to 1234567 print('colby'.translate(p) >>3ol2y name.replace('c','C')#string replace name.split(',')#characters Strings are separated into lists according to the specified characters name.splitlines()#Separated by newlines, Linux is the same as the windows method name.swapcase()#Case swap name.title()#CamelCase name.zfill()#Complete 10 with 0, add info['age']=10 to dictionary, delete del info.get("name")#Safely delete and modify info['age']=10 Find info.get("name" )#Return if it exists, None if it does not exist info['name'] merge and update b={'name':'colby','age':'29','height':'75Kg'} info.update()# If it exists, update it, if it does not exist, add it info.values()#print all values ​​info.keys()#print the key info.setdefault('name','age')#if it exists, it will be displayed, if it does not exist, add a new one Value multi-level dictionary nested operation info['person']['age'][1]=90 info.item()#Convert fields into lists initDict=dict.fromkeys([6,7,8],[1 ,{"name":"colby"},444]) #dictionary initialization#The same memory address referenced by the value, note that modifying a loop equal to modifying the All dictionary#Efficient writing method, it is recommended to use for i in info: print( i,info[i]) #Low efficiency, it is not recommended to use for k,v in info.items(): print(k,v) 11. Set intersection &intersection Returns the union of sets | union Returns the difference of sets - difference Returns the symmetric difference of sets^ symmetric_difference There is no subset in two sets at the same time issubset True or False Parent set issubset True or False Set adds one element add Set adds multiple elements update Set delete element remove string, list, dictionary, set to determine whether the character exists in it for in str/list/set/dict: print(i)

Guess you like

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