Python common basic knowledge points

Python basics

Environment setup

Python operating environment

Python package management

  • Use pip directly
  • Use conda
  • Use virtual environment
  • Use scene acceleration (https://pypi.tuna.tsinghua.edu.cn/simple)
创建一个stenv目录,然后进入到stenv目录
创建虚拟环境
python3 -m venv .
激活虚拟环境
source bin/activate #windows中直接执行activate.bat
这虚拟环境就创建完成
退出虚拟环境
终端执行命令:deactivate

Run Python

  • Use terminal
  • Directly as a script
  • Run in the compiler
  • Packaged as an executable program (pyinstaller -F source)

Basic grammar

Quick start

'''
基本命名方式
module_name,  模块
package_name,  包
ClassName,  类 驼峰
method_name,  方法
ExceptionName,   异常
function_name,  函数 下划线
GLOBAL_VAR_NAME, 全局变量
instance_var_name,  实例
function_parameter_name,   参数
local_var_name.  本变量
'''

"""
关键字
and	exec	not
assert	finally	or
break	for	pass
class	from	print
continue	global	raise
def	if	return
del	import	try
elif	in	while
else	is	with
except	lambda	yield
"""

#基本变量

a = 3
b = 3.5
c = "hello"
d = ['1',123,3.5]

#条件判断,使用4个空格缩进,不使用tab
if a == 3:
    print("a is 3")
else:
    print("a is not 3")

#循环
for i in range(3):
    print(i)

#函数
def add(a,b):
    """
    实现两个数相加
    :param a: 加数
    :param b: 被加数
    :return: 返回相加结果
    """
    return a + b

#类
class StMath(object):
    """
    实现数学计算的类
    """
    def __init__(self,a_in):
        """
        初始化方法
        :param a_in: 初始化数值
        """
        super().__init__()
        self.a = a_in

    def add(self,B):
        """
        实现两个math对象相加
        :param B: 被加math对象
        :return: 相加结果
        """
        return StMath(self.a + B.b)

basic type

Number type

High precision! High precision! High precision! The important thing is said three times

String

Basic operation


#增删改查

content = "abcxabcyabc"

print(content+"123")
print(content[:2]+content[4:])
print(content.replace("abc","python"))
print(re.sub("abc","python",content)) #使用正则表达式替换
print(content.find("xyz",2)) #或"xyz" in content
print(re.findall("abc",content))

Coding problem

In python3, text is always Unicode, represented by str type, and binary data is represented by bytes type. Python 3 will not mix str and bytes in any implicit way, which makes the distinction between the two particularly clear. You cannot concatenate strings and byte packets, nor can you search for strings in byte packets

A garbled problem demonstration

"你好".encode("utf-8").decode("gbk")
#str,bytes再一个事例
"你好".encode("unicode-escape").decode()
ord('你')

List

List has paradigm characteristics (after talking about the object, review the sorting)

#增删改查
a = [1,2,3,4,5,6,7]
#增加元素
a.append(8)
#删除元素
a.remove(3)
#查找元素
a.index(6)
#修改元素
a[4] = 10
#切片操作,会忽略结束元素
a[2:5] #注意不会包括索引5的元素
a[::-1] #反序操作

#便利操作
for v in a:
	print(v)

#如果属性要索引
for i,v in enumerate(a):
    print(i,v)


dictionary

#增删改查
stu_dict = {"name":"jack","age":21,"socre":100}
#增加
stu_dict["phone"] = "18000000000"
#删除
del stu_dict["phone"]
#修改
stu_dict["name"] = "lucy" #stu_dict.update({})
#查询
stu_dict.get("name") #和stu_dict["name"]区别

Use function

The function is a first-class citizen of python and becomes:

  • Created at runtime
  • Assignable
  • Can pass
  • Can return

Function parameters

Immutable object

str, int, number are all immutable objects, passed by value

a = 20

def change_value(x):
    x = 100

change_value(a)
print(a) #结果仍然是20

Mutable object

List, dict are mutable objects, passed by reference

def change_list(x):
    x.append(20)

y = [1,2,3]
change_list(y)
print(y) # 结果是1,2,3,20

Targeting and keyword parameters

Positioning parameters must be in order. Keyword parameters can be in random order, and default values ​​can be assigned

def extract_content(tag_name,start_pos = 20)
		print("tag_name is ::::::",tag_name)

* args, ** karges

Use args and kargs to pass in variable-length parameters, the positioning parameters captured by args are organized as tuples, and the parameters captured by kargs are organized as a dictionary

def extract_content(*args,**kargs):
    print(args)
    print(kargs)

extract_content("a",3.5,[1,2,3],name="jack",age="22")

The array plus * will be unpacked by args and captured one by one

list_args = [1,2,3,4,5,6]
extract_content(*list_args)

Function annotation

Metadata can be added to parameters and return values, which can improve readability and development efficiency when supporting complex data. Python itself does not check or verify, and will not affect the behavior of the program. It is just a convention

def add(add_a:int,add_b:int) -> int:
    return add_a + add_b

print(add(10,20))

Variable threshold

Global scope and local scope

#下面的代码错在什么地方?使用global纠正
m = 10
def show_diff():
    print("********show_diff*********")
    global
    n = 20
    print(m)
    m = 90
    print(n)

Functions and closures


#函数和闭包
def create_sum():
    sum = 0
    def sum_all(a_in):
        nonlocal sum
        sum += a_in
        return sum
    return sum_all

s_custom = create_sum()
#本质是什么
#print(s_custom.__closure__[0].cell_contents)
print(s_custom(10))
print(s_custom(20))
print(s_custom(30))

How to be a first-class citizen?

Can be assigned

p = add
print(p(10,20))

Can be passed as a parameter (linux, spring design)

def download_url(url_str,extract_rule):
    print("下载了某个网页")
    result = "<html><head><a href='//www.baidu.com'></a></head></html>"
    urls = extract_rule(result)
    print("页面的所有地址是:",urls)

def normal_url_rule(result):
    return re.findall("<a\s+href=\'(.*?)\'",result)

def no_http_url_rule(result):
    origin_url = re.findall("<a\s+href=\'(.*?)\'",result)
    return ["http:"+ url for url in origin_url]

download_url("",no_http_url_rule)

As return value

def math_rule():
    def rule(a,b):
        return a+b

    return rule


rule = math_rule()
print(rule(20,30))

Decorator

The essence of decorators, nested calls of functions

import time

#装饰器
def download_html():
    time.sleep(1)
    print("执行了下载操作")



def record_time(download_func):
    def record_wrapper():
        start_time = time.time()
        download_func()
        end_time = time.time()
        print("下载经过了时间",end_time - start_time)
    return record_wrapper


decorate_download = record_time(download_html)
decorate_download()

Use syntactic sugar for a more intuitive experience:

@record_time
def download_html():
    time.sleep(1)
    print("执行了下载操作")


download_html()

Passing parameters in the decorator:

def record_time(level):
    def decorate(download_func):
        def record_wrapper(*args,**kargs):
            print("下载网址是:",args)
            print("日志等级是",level)
            start_time = time.time()
            download_func()
            end_time = time.time()
            print("下载经过了时间",end_time - start_time)
        return record_wrapper
    return decorate


@record_time(level=9)
def download_html():
    time.sleep(1)
    print("执行了下载操作")

Object-oriented

The basic characteristics of object-oriented, encapsulation, inheritance, polymorphism, abstraction is used to design the hierarchical structure of the class

Object Oriented Fundamentals

self, method rewrite, @classmethod @staticmethod, built-in method rewrite

class Person(object):
    """
    定义抽象基类
    """
    def __init__(self,score):
        self.score = score
        self.__name = "jack" #加上两个下划线,表示私有方法

    @classmethod
    def from_file(cls,file_name):
        """
        从文件读取学生成绩
        :param file_name: 文件名
        :return:
        """
        score = open("score.txt").read()
        return cls(score)

    @staticmethod
    def extract_address():
        return "no.90"

    def run(self):
        print("person run!!!!")


    def __add__(self, other):
        return Student(self.score + other.score)


class Student(Person):
    """
    学生信息类
    """
    def __init__(self,score):
        super().__init__(score)

    def ask(self):
        print("ask a question!!")

    def run(self):
        super().run()
        print("student run!!",self.score)

# p = Person(85)
# p.run()

s = Student.from_file("test.txt")
print(Student.extract_address())
s.run()

Abstraction layer construction

The abstract methods in the abstract base class must be implemented in the subclass, otherwise the subclass cannot be instantiated.

#抽象接口
from abc import ABC,abstractmethod

class Person(ABC):
    """
    定义抽象基类
    """
    def __init__(self):
        pass

    @abstractmethod
    def run(self):
        pass

class Student(Person):
    def __init__(self):
        super().__init__()

    def ask(self):
        print("ask a question!!")

    def run(self):
        print("run run run1!!")

Dynamic characteristics

Dynamically judge properties and set properties during operation, which can be combined with configuration files to flexibly load and modify classes

  • hasattr
  • setattr
  • hasattr
package = __import__('st_python_base')
student_class = getattr(package,'Student')

s = student_class(90)
s.run()

When an unknown access attribute appears, handle it

 def __getattr__(self, item):
         print('getattr')

Memory management

About reference counting

import weakref
def over():
    print("student is over!!!")
    
x = st_python_base.Student(10)
ender = weakref.finalize(x,over)

y = x

About deep copy and shallow copy

Pay attention to the difference between direct assignment, copy and deepcopy?

from copy import copy
from copy import deepcopy

class Student(object):

    def __init__(self):
        self.course = [1,2,3,4,5]
        self.name = "jack"


s1 = Student()
s2 = deepcopy(s1)

s1.course.append(12)
print(s2.course)

Context management

the essence of with...as...

class StTools(object):

    def __enter__(self):
        print("进入上下文块。。。。")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("从上下文块退出")

    def make_sign(self):
        print("test!!!")



with StTools() as st:
    st.make_sign()

About iterators and generators

Understand yield and send

def s():
    print('begin test')
    a = yield 10
    print(a)
    b = yield 20
    print('end test!')

Guess you like

Origin blog.csdn.net/weixin_46046193/article/details/108632580