A complete collection of python basic knowledge points

introduction to python

  • Python is an object-oriented interpreted computer programming language, glue language needs an interpreter (translation) into machine language 1991 public release first edition, developed by Guido van Rossum in 1989, as the successor of ABC language Guido is the Monty Python comedy group lovers, so this language was named python 2.0 was released in 2000, 3.0 was released in 2008, and 2.0 will not be updated starting in 2020

python features

1. The running speed is slow (compiled language-interpreted language), the difference is in milliseconds

2. The code cannot be encrypted, and the source code has been released

3. Mandatory indentation

4. GIL global interpreter lock: At any time, only one thread runs in the interpreter, and access to the python virtual machine is controlled by the global interpreter lock GIL, which ensures that only one thread is running at the same time. Python multithreading is not really multithreading. But because the CPU is more efficient, people can't feel the difference.

what python can do

1、WEB:Flask、django、tornado

2. Crawler development: scrapy, urllib, requests, beautifulsoup, selenium

3. Data analysis BI: pandas, numpy, matplotlib, pyplot, scipy, seaborn

4. Machine learning: scikit-learn, tensorflow, keras, opencv

5. Operation and maintenance development:

6. Artificial Intelligence

  • Python development IDE 1, notepad++, sublime, pycharm

python variables and constants

  • Naming rules, big and small hump method, see the name and know the meaning import keyword keyword.keywordlist # View system keywords cannot be used as variable names

data type, data structure

Basic data types: int, float, bool

Advanced data types, data structures: strings, lists, tuples, dictionaries, sets

  • Sequence is a set of data sets arranged in order Python built-in sequence types: string, list, tuple sequence support index and slice operations abs() function, find absolute value; ord() function, return ASCII code value; chr() Function (for 8-bit ASCII strings) or unichr() function (for Unicode objects), ASCII code conversion character; id() function, view memory address; join() function, string function, connect strings with characters Each element; swapcase() string function, converts the case of all letters; public method: in + * not in general method len(), shuffle(), del(), sorted a.isdigit() to determine whether a is integer

list list:

  • It is an ordered collection of data that supports adding, deleting, modifying and checking. The data is variable and the memory address remains unchanged. Data items can be any type of data, such as: lists, tuples, dictionaries, characters, numbers, strings, etc. Commonly used methods: append, count, extend, index, insert, pop, remove, reverse, sort, keys, values ​​list comprehension # list comprehension lst = [x**2 for x in range(1,5) if x% 2==0] # [4,9] lst = [x**2 if x%2==0 else 0 for x in range(1,5)] # There is else, the position of if is different# [0 , 4, 0, 16]

tuple

  • It is an immutable sequence and cannot be modified in any way. Only one element needs to be added with a comma. The tuple element cannot be assigned a value, and the list elements in the tuple can be modified. A tuple can be created without parentheses a = 10,20,30. The zip() function merges multiple lists, tuples, and returns a zip object, which can be converted into a dictionary or a list. List comprehensions also apply to tuples, generating a generator that can be converted to tuples and lists. Builders can only be accessed once.

dict dictionary

  • Key-value pairs, high access efficiency through keys (similar to indexes), support addition, deletion, modification and query, no subscript concept (not sequence type), keys are immutable and not repeated, values ​​can be repeated and can be of any type. dict.keys() dict.items() dict.values() dict.update() modify and add key-value pairs to dict.add(). Can be nested dict.sort_values() sort by value dict.sort_keys() sort by key sorted(dcit.items() , key='a') sort by key a. The sorted() function can also sort the ancestors. After sorting, the list a = dict.fromkeys(['name','age','gender']) is returned, and an empty dictionary popitem() is created through fromkeys and removed randomly a key-value pair

set collection

  • The set collection is an unordered and non-repeating element sequence. set.add() adds elements set.clear() clears the set set1&set2 set1.union(set2) finds the union, same as set1 | set2 set1.pop() deletes elements, returns the deleted elements set1.discard(3) specifies to remove 3 set1.update(4) inserts 4, set1.update( set2) Sets do not support indexing and slicing, similar to dictionaries, only key but no value

formatted output

  • Formatted output, % placeholder, followed by variable type, %s %d %f %i %c: print('my name is %s' % (name)) format method: print('my name is {1}Age is {0}'.format(age, name )) You can set the order of variable value extraction in front Formatted string u: represents unicode, encodes chucun in unicode, and can store Chinese. By default in python3, strings are stored in unicode encoding b: strings are stored in the form of Ascll codes, and Chinese characters cannot be stored r: represent raw strings, and escapes are not recognized f: represent format, used to format strings

process control

1. Sequential process: execute code from top to bottom by default

2. Selection process judgment statement: if ... elif ... else

3. Loop flow, loop statements: for, while, continue, break, must have an end condition, otherwise it will fall into an infinite loop

4. Process control: use of return in pass and function

operator

  • : + - * / // ** % = : -= += /= **= *= & | or and not ~ == ! = > < <= >= >> << ternary operator#ternary operator The format of operation # is: expression? true: false there is no such result = (8>10) ? correct: false print(result) # true result if expression else false result a=5 b=6 print = (a+b) if a>b else (ab) # The result is the latter, the false result is -1 Operator overloading: + is actually a class method __add__() You can redefine __add__() in the class

Function basis

1. Function definition and naming rules

2. Function parameters: parameter passing, calling, default parameter (parameter default value), *args, kwargs, all parameters in the function, whether it is a variable or immutable data structure such as a dictionary type or a string type, all It belongs to *args; but if it is a parameter passed in the form of key=value key-value pair, then this parameter belongs to kwargs.

3. The positions of the parameters arg, *args variable parameters (tuples, lists), **kwargs (schedule keyword parameter dictionary) must be certain

4. When *kwargs is called, add * in front of the parameter

def stuinfo(**kwargs): 
    print(kwargs) # print dictionary 
    print(*kwargs) # print key 
    for i,v in kwargs.items(): # traverse key values 
        ​​print(i,v) 

dictA= { 
          
   name:marvin ,age:30} 
if __name__ == __main__: 
    stuinfo(**dictA) # pass parameters

5. List comprehension

list1 = [1,2,3,4,5,6,7,8,9] 

list2 = [ list1[i] for i in range(len(list1)) if i%2==0] # return list1 even number bit elements 

list2 = list1[0::2]

6. The return value of the function is return. After executing the return function body, the execution is completed and exit

7. Variable scope: global variables, local variables, global

8. Everything in python is an object

anonymous function lambda

1. (lambda x,y : x if x>y else y)(12 ,2 ) # followed by parameters can be called directly

2. g = lambda x,y : x*y # General definition method

g(12,2) # calling method

Builder

# generator 
def func(): 
    n = 0 
    while True: 
        n += 1 
        yield n # return n and suspend 
g = func() 
print(next(g)) # print(g.__next__()) #1 
print( next(g)) # print(g.__next__()) #2 
print(next(g)) # print(g.__next__()) #3 
# Practice, print the Fibonacci sequence 
def fib(length): 
    n = 0 
    a, b = 0, 1 
    print(0) 
    while n < length - 1: # The first element 0 has been printed 
        print(b) 
        a, b = b, a + b 
        n += 1 
length = int(input (Enter the required number:)) 
fib(length) 

# Practice, use the generator, print the Fibonacci sequence, and error message 
def fib(length): 
    n = 0 
    a, b = 0, 1 
    print(0) # print the first element 
    while n < length - 1: 
        yield b 
        a, b = b, a + b 
        n += 1 
    return no more # When the output result is finished, output this error 

length = int (input (the number required for input:)) 
print(next(fib(length))) # call once to output one

recursive function

1. Call your own function. Generally speaking, to realize the loop, there must be an end condition

2. Realize factorial recursively:

def jiecheng(n): 
    if n > 1: 
        return n * jiecheng(n-1) 
    else: 
        return n 

if __name__ == __main__: 
    print(jiecheng(6))

3. The recursive execution logic is: the first parameter is brought in without returning a value, and the return value is not returned until the last parameter is brought in, and then the return value of the penultimate second parameter is returned in turn.

built-in function

1. Mathematical functions: round() eval() abs() pow() sqrt()

2. Type conversion function: int() hex() oct() bin() # Hexadecimal conversion bytes() # Character conversion into byte code str() list() bool() # Data type conversion ord() char( ) # ASCII code and character conversion

3、

print(bytes("I love you", encoding=utf-8)) 
# output bxe6x88x91xe7x88xb1xe4xbdxa0

4. Sequence operation function:

all() # Determine whether each element of the iterable parameter is true, which is equivalent to performing an and operation on each element, and an empty element in the list is also True

any() # Determine whether any of the iterable parameters is True, which is equivalent to performing an or operation on each element

sorted() # Iterable parameters are sorted, the default is ascending, the difference with sort is that sorted generates a new sequence

reverse() # Iterable parameters are transposed (reverse)

range() # create a list of integers, (start , stop ,step)

zip() # Pack the corresponding element combination in the parameter into a tuple, and the parameter can be a sequence of 1 or more

enumerate() # The parameter is an iterable object, which returns the subscript and value of the object. enumerate(sequence, [start=0]), sequence – a sequence, iterator or other object that supports iteration. start – The starting position of the subscript. The main purpose is to index objects.

seasons = [Spring, Summer, Fall, Winter]
print(list(enumerate(seasons)))

# 输出:[(0, Spring), (1, Summer), (2, Fall), (3, Winter)]
  • List comprehension list3 = [i for i in list1 if i%==0 ]

object oriented

class, parent class, child class

1. Object-oriented OOP is similar to the level seen by the boss: for example, the boss is an object when he is on a business trip. The boss only needs to provide the time and destination, and the secretary and finance will go to buy tickets, book hotels, and restaurants.

3. A class is a template, a category. An object is an instance of a class. The class is a drawing, and the object is a physical object made according to the drawing.

4. Example:

# Define a person's class 
class Person: 

    def __init__(self,name,age): 
        self.name = name 
        self.age = age 
        print(name+"this year",age,"year") 

    def eat(self,food): 
        print(self.name+"eating"+food) 


if __name__ == __main__: 
    xiaoming = Person(Xiaoming,20) 
    xiaoming.eat("rice")

Inheritance: single inheritance, multiple inheritance

1. If there is a method with the same name in a multi-inherited class, follow the breadth-first search

2. Indirect inheritance: son inherits father, father inherits grandfather, then son can also inherit grandfather

3. All classes inherit the object base class by default

4. isinstance(m,man) returns whether m inherits from man

Overriding the method of the parent class

1. If you define a method with the same name as the parent class in the subclass, you can override the method of the parent class

2. When __init__() is rewritten, if the function is added on the basis of the parent class, first call the init function of the parent class, and then write your own function

The __new__() method is executed before the __init__() method

3、__mro__():

Python classes have multiple inheritance features. If the inheritance relationship is too complicated, it is difficult to see which attribute or method will be called first.

In order to easily and quickly see the inheritance relationship and order, you can use the __mro__ method to obtain the calling order of this class

4、__str__():

When using print to output an object, as long as you define the __str__(self) method, the data returned from this method will be printed

The str' method needs to return a string as a description of this object

5. super().func(): Call the method of the parent class

polymorphism

1. Occurs when inheriting

2. The subclass needs to override the method of the parent class

3. The benefits of polymorphism, through the obj abstract object, uniformly call polymorphic functions

4. The so-called polymorphism is the difference between definition and execution

  • Class attributes and instance object attributes 1. Classes can access class attributes 2. Instance objects can access class attributes and their own instance attributes 3. Object attributes can be modified through objects, and class attributes can be modified through class names class Person(object): def __init__ (self,name,sex): self.name = name self.sex = sex class Child(Person): # Child inherits Person def __init__(self,name,sex,mother,father): Person.__init__(self,name, sex) # Subclass calls to parent class constructor can reduce code redundancy self.mother = mother self.father = father May = Child("May","female","April","June") print (May.name,May.sex,May.mother,May.father)

class method

1. The class method is defined in the class, which is similar to the class attribute and can be called by the class and the instance. The first parameter must be the class itself cls

2. Definition method

@classmethod # This is a decorator for a class method

def class_func(cls):

pass

3. The difference from ordinary methods: ordinary methods need to be called by an instance of the class, and cannot be called directly by the class name

4. In the class method, the value of the class attribute can be modified

static method

1. Declare @staticmethod

2. Generally, static methods are not accessed through instance objects, reducing the instantiation operations of class objects and reducing the waste of computing resources, but instances can also be accessed

3. It is used to store logical code, which does not involve the operation of class attributes and methods

4. For example, access system time

5. When using it, call it directly with the class name without parameters

time.strftime("%H:%M%S",time.localtime())

6. Ordinary functions without decorators can only be accessed through instantiated objects

7. The first parameter of a common method is the instance itself

privatization attribute

1. Hide the attributes of the class, which cannot be directly accessed outside the class, but can be accessed inside the class, and can be used as parameters of class methods

2. It cannot be inherited by subclasses

3. You can modify the private attribute inside the class

class Person: 
    def __init__(self,name,age,nation): 
        self.name = name 
        self.age = age 
        self.__nation = China# privatization attribute 

lisi = Person(lisi,30,Japan) 
print(lisi.name, lisi.age) 
# print(lisi.__nation) not accessible

Privatization method

1. The concept is similar to the privatization method

2. Definition

class Person: 

    def __read(self,book_name): #Privatization method 
        print(reading,book_name) 
    def acta(self): 
        self.__read(Youthology) #Internal callable 
xiaoli = Person() 
# xiaoli.__read(100 years Lonely) # The instance cannot call this method 
# 
xiaoli.acta()

class attribute

1. Access private properties that cannot be accessed through the class attribute function property()

class Person: 

    def __init__(self): 
        self.__age = 18 

    def get_age(self): 
        return self.__age 

    def set_age(self,age): 
        self.__age = age age 

    = property(get_age,set_age) 

xiaoli = Person() 
# xiaoli.__read(One Hundred Years of Solitude) # The instance cannot call this method 
print(xiaoli.age) 
xiaoli.age= 20 
print(xiaoli.age)

2. The above functions can be realized through decorators

magic method _str_

1. As long as you define the __str__(self) method, the data returned from this method will be printed

2. The __str__ method needs to return a string as a description of this object

3. When the instance object is printed directly, the string returned by __str__() will be output

4. dir (class) displays the methods and properties owned by the class

5. __new__() method executed before instantiation

Destructor

  • When the object is deleted, the _del_() method is automatically called, the destructor class Animal: def __init__(self,name): print("This is the initialization method, I have a kitten called {}".format(name) ) def __del__(): print("This is the destructor method") cat=Animal("Little Flower") This is the initialization method, I have a kitten named Xiaohua and this is the destructor method

Assignment operation of class object, deep copy, shallow copy

class CPU: 
    pass 

class Disk: 
    pass 

class Computer: 
    def __init__(self,cpu,disk): 
        self.cpu = cpu 
        self.disk = disk 

cpu1 = CPU() 
cpu2 = cpu1 
print(cpu1:,id(cpu1)) 
print (cpu2:,id(cpu2)) # The id of cpu2 is the same as that of cpu1, two different variables point to the same memory space 
disk = Disk() 

print(------------) 
computer1 = Computer(cpu1,disk) 

import copy 

# shallow copy 
computer2 = copy.copy(computer1) # shallow copy, only copy computer class object 
print(computer1:,id(computer1)) 
print(computer2:,id(computer2)) #computer2 The memory address of computer1 is different from that of computer1, but the ids of the cpu and disk attributes included are the same 

print(computer1.cpu:,id(computer1.cpu)) 
print(computer2.cpu:,id(computer2.cpu)) # consistent with the above 
print(---------- --)
# Deep copy 
computer3 = copy.deepcopy(computer1) # Deep copy the computer class object and its sub-objects cpu, disk 
print(computer1:, id(computer1)) 
print(computer3:, id(computer3)) # computer3 memory The address is different from computer1, and the id of the cpu and disk attributes contained in it is also different 

print(computer1.cpu:,id(computer1.cpu)) 
print(computer3.cpu:,id(computer3.cpu)) #Different from the above

composition, similar to inheritance

# Inherit 
class A1: 
	def say_a1(self): 
		print("a1a1a1") 
class B1(A1): # B1 inherits A1 
	pass 
b1 = B1() 
b1.say_a1() # Print a1a1a1 

# Combine 
class A2: 
	def say_a2(self ): 
		print("a2a2a2") 
class B2: 
	def __init__(self,a): # a is an attribute of class B2, this attribute can be a class 
		self.a = a 
b2 = B2(A2()) # put A2 The instantiated object is an attribute of B2, and passed to B2 
b2.a.say_a2() # Pass the attribute a of b2 (that is, the instantiated object of A2), and then call the say_a2 method of the instantiated object of A2, and print a2a2a2

run as main program

if __name__ == __main__: 
	pass # Code that will only be executed if this file is run directly

package-module-code

1. import can import packages and modules

2. When using from, you can import functions, variables, and classes from packages and modules

Encoding format

1、ASCII,ISO8859-1,UNICODE,utf-8,gk2312,gbk,gb18030

2. The default encoding of the python file is utf-8, add # encoding = GBK at the front to modify the file encoding

3. txt is saved as utf-8, a Chinese character is 4 bytes, and a Chinese character is 2 bytes in ANSI

base and conversion

#bit operation 
#bin() to binary function 
a=100 
print(bin(a)) # 0b1100100 
b=0b1100100 
print(int(b)) # 100 
c=-2 
print(bin(c)) # -0b10 

# 0o octal one octal to 3-bit binary, 0x hexadecimal 
d=0o635742 
print(int(d)) #octal to decimal 
e=0x9ff9d 
print(int(e)) #hexadecimal to decimal 

& and 
| or 
~ Not 
<< Left Shift 
>> Right Shift 
^ XOR 

print(8>>1) #8 is converted into binary, then shifted right by 1 bit by bit, and the first bit on the left is filled with 0 
print(5 & 3) #5 and 3 Convert to binary, complete 8 bits or 1 byte, and calculate bit by bit, 1&1=1, the rest are 0, get a new binary, and convert to decimal output print(8^6) # Convert to binary alignment, bit by 
bit Perform XOR operation, that is, the same is 0, and the different is 1

file read and write

1. File reading and writing is commonly known as IO

2. open(filepath, mode), mode=r reads from the beginning, w writes if there is no one, creates it, a does not, then creates additional content, rb, wb binary, a+, r+, w+ read and write

3. Common methods, use the method filestrem.funciont() to call the file stream

  • read([size]) read the content of size size, read the content without writing readlines() read all the content, put the line into the list, readline() read a line write(str) writelines(s_list) seek(offset[,whence]) Read from the offset byte tell() Return the current position of the file pointer flush() Write the contents of the buffer into the file, but do not close the file, you can continue to operate after flushing close() After closing, you can no longer operate the file

4. with open(filepath,mode) as filestrem can be closed manually, there are two functions __enter__() and __exit__(), which are automatically executed when the file is opened and closed

5. Built-in OS module

  • os.system('notepad.exe') is equivalent to start, run os.startfile(filepath) execute a program file getcwd() return the current directory chdir(path) change the directory to path listdir(path) return the files and Folder walk(path) Traversing all files and folders under the path, traversing layer by layer import os path = os.getcwd() lst_files = os.walk(path) # Return all folders under path, files for dirpath,dirname, filename in lst_files: # hierarchical traversal print(dirpath) # current path print(dirname) # directory under the path print(filename) # file under the directory mkdir(path) create directory makedirs(path1/path2) create multi-level directory rmdir (path) delete directory removedirs(path1/path2) delete multi-level directory

6. os.path module

  • abspath(file) Get the absolute path of a file or directory exists(path) Determine whether a file or directory exists join(path,name) split(path) separates the file name from the path splitext() separates the file name from the extension basename(path) from the target Extract the file name dirname(path) Extract the file path from the path without the file name isdir(path) Determine whether it is a directory

regular expression

exception handling

try...except...Basic exception handling structure

# try..except..Basic exception handling structure 
try: 
	pass 
	# There may be error code blocks, and the code below the error location will not be executed. The code before the error is executed as usual 
except Exception as e: # Catch exceptions, exception includes most exceptions, BaseException includes all exceptions 
	print(e) # print exception prompt information, e can be classified, and more accurate exception prompt information can be printed

try...except...except... exception handling structure

# Exception handling structure 
try: 
	pass 
	# There may be an error code block, and the code below the error location will not be executed. The code before the error is executed as usual 
except exception type 1: 
	print("prompt message 1") 
except exception type 2: 
	print("prompt message 2") 
except BaseException: # Take care of all other abnormal situations 
	print("prompt message") # general reminder message

try...except...else... exception handling structure

# try..except..else.. Exception handling structure 
try: 
	pass 
	# There may be an error code block, and the code below the error location will not be executed. The code before the error is executed as usual 
except Exception as e: # Catch exceptions, exception includes most exceptions, BaseException includes all exceptions 
	print(e) # print exception prompt information, e can be classified, and more accurate exception prompt information can be printed 
else: 
	pass 
	# Continue to execute the code block here when there is no exception

try...except...else...finally... exception handling structure

# try..except..else..finally.. Exception handling structure 
try: 
	pass 
	# The code block that may have errors, the code below the error location will not be executed. The code before the error is executed as usual 
except Exception as e: # Catch exceptions, exception includes most exceptions, BaseException includes all exceptions 
	print(e) # print exception prompt information, e can be classified, and more accurate exception prompt information can be printed 
else: 
	pass 
	# Continue to execute the code block here when there is no exception 
finally: 
	pass 
	# Execute the code block here regardless of whether there is an exception

traceback uses the module to output error information to a file

import traceback 
try: 
	print("execute the previous code") 
	m = 1/0 
except: 
	with open("d:/error.txt",a) as f: 
		traceback.print_exc(file=f) # put the error message output to file

custom exception class

class AgeError(Exception): # Inherited from exception class 
	def __init__(self,errorinfo): 
		Exception.__init__(self) 
		self.errorinfo = errorinfo 
	def __str__(self): 
		return str(self.errorinfo) + "Age error, should Between 1-150" 
if __name__ == "__main__": 
	age = int(input("Please enter the age:")) 
	if age<1 or age>150: 
		raise AgeError(age) # Throw a custom exception 
	else : 
		print("The entered age is:",age)

with context management

1. Compared with try...except...

2. Convenient file management and communication management

3、with open(filepath) as f:

software design pattern

singleton pattern

class SingleCase(object): 
	__instance = None # Define a privatization attribute to save the instance object 
	def __init__(self,name,age): 
		print(name,age) 
	def __new__(cls,*args,**kwargs): 
		# If the value of the class attribute __instance is none, then create a new object 
		# If the value of the mine attribute __instance is not none, return the object saved by __instance 
		if not cls.__instance: 
			cls.__instance = supre(SingleCase, cls) .__new__(cls) # Call the parent class __new__() method to generate an instance object return 
			cls.__instance 
		else: 
			return cls.__instance 
obj1 = SingleCase("Xiaoming",18) 
obj2 = SingleCase("Xiaohua",28) 
print( id(obj1)) 
print(id(obj2)) 
# If the two ids are the same, they are the same object, and the printed content is also the same

factory pattern

class carFactory: 
    def create_car(self,brand): 
        if brand == Benz: 
            return Benz() 
        elif brand == BMW: 
            return BMW() 
        elif brand == Honda: 
            return Honda() 
        else: 
            print("Unknown brand, cannot Production") 
class Benz: 
    def __new__(self): 
        print("Produce a Mercedes-Benz") 
class BMW: 
    def __new__(self): 
        print("Produce a BMW") 
class Honda: 
    def __new__(self): 
        print("Produce a Honda") 
factory = carFactory() 
car1 = factory.create_car("Benz") 
car2 = factory.create_car("Honda")

hashlib encryption algorithm

# hashlib library, encryption method 
import hashlib 
print(hashlib.md5("abc".encode(utf-8)).hexdigest()) # 900150983cd24fb0d6963f7d28e17f72

Guess you like

Origin blog.csdn.net/qq_41221596/article/details/131624103