入门

API 速查表

https://overapi.com/python

数据类型及流程结构

常用数据类型:

  • 整型 12
  • 浮点型 123.456 1.23456e2
  • 字符串型 hello
  • 布尔型 True False
  • 复数型 3+5j

变量命名,PEP8 要求:

  • 用小写字母拼写,多个单词用下划线连接。
  • 受保护的实例属性用单个下划线开头
  • 私有的实例属性用两个下划线开头

变量类型进行转换:

  • int():将一个数值或字符串转换成整数,可以指定进制。
  • float():将一个字符串转换成浮点数。
  • str():将指定的对象转换成字符串形式,可以指定编码。
  • chr():将整数转换成该编码对应的字符串(一个字符)。
  • ord():将字符串(一个字符)转换成对应的编码(整数)。

运算符

  • [] [:] 下标,切片
  • ** 指数
  • is is not 身份运算符
  • in not in 成员运算符
  • not or and 逻辑运算符

常用数据结构

字符串

定义

a="3.1415926"
b = '1.123456'
s3 = '''
hello,
world!
'''

长度

len(a)

成员运算

s1 = 'hello, world'
print('wo' in s1) # True
s2 = 'goodbye'
print(s2 in s1) # False

索引和切片

s = 'abc123456'
# 获取第一个字符
print(s[0], s[-N]) # a a
# i=2, j=5, k=1的正向切片操作
print(s[2:5]) # c12

编码/解码

b = a.encode('utf-8')
c = a.encode('gbk')
print(b.decode('utf-8'))
print(c.decode('gbk'))

格式化字符串

a = 3.1415926
print('{:.2f}'.format(a))# 保留小数点后两位
print('{:.2%}'.format(a))# 百分比格式
print('{:,}'.format(a))# 逗号分隔格式
print('{:.0f}'.format(a))# 不带小数

更多详见:第 10 课:常用数据结构之字符串

列表

定义

items1 = [35, 12, 99, 68, 55, 87]
items2 = ['Python', 'Java', 'Go', 'Kotlin']
items1 = list(range(1, 10))
print(items1) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
items2 = list('hello')
print(items2) # ['h', 'e', 'l', 'l', 'o']

列表运算

items1 = [35, 12, 99, 68, 55, 87]
items2 = [45, 8, 29]
# 列表的拼接
items3 = items1 + items2
print(items3) # [35, 12, 99, 68, 55, 87, 45, 8, 29]
print(items1.extend(items2))
# 列表的重复
items4 = ['hello'] * 3
print(items4) # ['hello', 'hello', 'hello']
# 列表的成员运算
print('hello' in items4) # True
# 获取列表的长度(元素个数)
size = len(items3)
# 列表的索引
print(items3[0], items3[-size]) # 35 35
items3[-1] = 100
print(items3[size - 1], items3[-1]) # 100 100
# 列表的切片
print(items3[:5]) # [35, 12, 99, 68, 55]
print(items3[4:]) # [55, 87, 45, 8, 100]
print(items3[-5:-7:-1]) # [55, 68]
print(items3[::-2]) # [100, 45, 55, 99, 35]
# 列表的比较运算
items5 = [1, 2, 3, 4]
items6 = list(range(1, 5))
# 两个列表比较相等性比的是对应索引位置上的元素是否相等
print(items5 == items6) # True
# 删除元素
del item5[0]

列表遍历

items = ['Python', 'Java', 'Go', 'Kotlin']
for index in range(len(items)):
print(items[index])
for item in items:
print(item)

列表操作

# 添加
items.append('Swift')
items.insert(2, 'SQL')
# 删除
items.remove('Java')
items.pop(0)
# 清空
items.clear()
# 排序
items.sort()
# 反转
items.reverse()
函数&方法描述
len(list)列表元素个数
max(list)返回列表元素最大值
min(list)返回列表元素最小值
list(seq)将元组转换为列表
list.append(obj)在列表末尾添加新的对象
list.count(obj)统计某个元素在列表中出现的次数
list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
list.index(obj)从列表中找出某个值第一个匹配项的索引位置
list.insert(index, obj)将对象插入列表
list.pop(obj=list[-1])移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list.remove(obj)移除列表中的一个元素(参数是列表中元素),并且不返回任何值
list.reverse()反向列表中元素
list.sort([func])对原列表进行排序

更多详见 第 08 课:常用数据结构之列表

list 生成式的创建语法:

[expr for iter_var in iterable]
[expr for iter_var in iterable if cond_expr]

demo

list1=[x * x for x in range(1, 11)]
list1= [x * x for x in range(1, 11) if x % 2 == 0]

元祖

元组是不可变类型,通常不可变类型在创建时间和占用空间上面都优于对应的可变类型。

定义

# 定义一个三元组
t1 = (30, 10, 55)

注意(30,)(30)区别,前者类型为 tuple,后者为数字。

操作

# 查看元组中元素的数量
print(len(t1), len(t2))
# 循环遍历元组中的元素
for member in t2:
print(member)
# 成员运算
print(40 in t2) # True
# 拼接
t3 = t1 + t2
# 切片
print(t3[::3])

元组和列表转换

# 将元组转换成列表
info = (180, True, '四川成都')
print(list(info))
# 将列表转换成元组
fruits = ['apple', 'banana', 'orange']
print(tuple(fruits))

打包和解包

# 打包
a = 1, 10, 100
print(type(a), a) # <class 'tuple'> (1, 10, 100)
# 解包
i, j, k = a
print(i, j, k) # 1 10 100

集合

定义

set1 = {1, 2, 3}
set2 = set('hello')# {'h', 'l', 'o', 'e'}
set3 = set([1, 2, 3, 3, 2, 1])

操作

# 创建一个空集合
set1 = set()
# 通过add方法添加元素
set1.add(33)
set1.update({1, 10, 100, 1000})
# 通过discard方法删除指定元素
set1.discard(100)
# 通过remove方法删除指定元素,建议先做成员运算再删除
# 否则元素如果不在集合中就会引发KeyError异常
if 10 in set1:
set1.remove(10)
print(set1) # {33, 1, 55, 1000}
# pop方法可以从集合中随机删除一个元素并返回该元素
print(set1.pop())
# clear方法可以清空整个集合
set1.clear()
set2 = {'Python', 'Java', 'Go', 'Swift'}
print('Ruby' in set2) # False

字典

定义

person = {
'name': 'trumman, 'age': 55
}
person = dict(name='trumman', age=55)

操作

# 查看多少组键值对
len(person)
# 检查name和tel两个键在不在person字典中
print('name' in person, 'tel' in person) # True False
# 获取字典中所有的键
print(students.keys()) # dict_keys([1001, 1002, 1003])
# 获取字典中所有的值
print(students.values()) # dict_values([{...}, {...}, {...}])
# 获取字典中所有的键值对
print(students.items()) # dict_items([(1001, {...}), (1002, {....}), (1003, {...})])
# 对字典中所有的键值对进行循环遍历
for key, value in students.items():
print(key, '--->', value)
方法和函数描述
len(dict)计算字典元素个数
str(dict)输出字典可打印的字符串表示
type(variable)返回输入的变量类型,如果变量是字典就返回字典类型
dict.clear()删除字典内所有元素
dict.copy()返回一个字典的浅复制
dict.values()以列表返回字典中的所有值
popitem()随机返回并删除字典中的一对键和值
dict.items()以列表返回可遍历的(键, 值) 元组数组

流程

分支

if x > 1:
y = 3 * x - 5
elif x >= -1:
y = x + 2
else:
y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))

多个条件可以结合 or 和 and 来使用

if java > 80 and python > 80:
print('优秀')
else :
print('不优秀')
if ( java >= 80 and java < 90 ) or ( python >= 80 and python < 90):
print('良好')

循环

for x in range(1, 101):
if x % 2 == 0:
sum += x
print(sum)
while True:
counter += 1
number = int(input('请输入: '))
if number < answer:
print('大一点')
elif number > answer:
print('小一点')
else:
print('恭喜你猜对了!')
break
print('你总共猜了%d次' % counter)

函数和模块

可变参数

# 用星号表达式来表示args可以接收0个或任意多个参数
def add(*args):
total = 0
# 可变参数可以放在for循环中取出每个参数的值
for val in args:
if type(val) in (int, float):
total += val
return total

默认参数

def add(a,b=0):
return a+b

只有在形参表末尾的那些参数可以有默认参数值

关键参数

关键参数即不用关心传递参数的顺序

def print_user_info(name, age, sex):
# 打印用户信息
print('昵称:{}'.format(name))
print('年龄:{}'.format(age))
print('性别:{}'.format(sex))
print_user_info(age=10, name='张三', sex='男')

只接受关键字参数,将强制关键字参数放到某个参数或者单个后面

def print_user_info(name, *, age, sex):
# 打印用户信息
print('昵称:{}'.format(name))
print('年龄:{}'.format(age))
print('性别:{}'.format(sex))
return
print_user_info(age=10, name='张三', sex='男')
print_user_info('张三',10, '男')# 报错

def f(*, name, age, sex)def f(name, *, age, sex)可以实现一样的效果

匿名函数

python 使用 lambda 来创建匿名函数,lambda 参数是在运行时绑定的。

基本语法

lambda [arg1 [,arg2,.....argn]]:expression

demo

sum = lambda num1 , num2 : num1 + num2;
print( sum( 1 , 2 ) )

模块

module1.py

def foo():
print('hello, world!')
import module1 as m1
from module1 import foo as f1
# 用“模块名.函数名”的方式(完全限定名)调用函数,
m1.foo() # hello, world!
f1() # hello, world!

装饰器

import random
import time
from functools import wraps
def record_time(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f'{func.__name__}执行时间: {end - start:.3f}秒')
return result
return wrapper
@record_time
def download(filename):
print(f'开始下载{filename}.')
time.sleep(random.randint(2, 6))
print(f'{filename}下载完成.')
@record_time
def upload(filename):
print(f'开始上传{filename}.')
time.sleep(random.randint(4, 8))
print(f'{filename}上传完成.')
download('MySQL从删库到跑路.avi')
upload('Python从入门到住院.pdf')
# 取消装饰器
download.__wrapped__('MySQL必知必会.pdf')
upload = upload.__wrapped__
upload('Python从新手到大师.pdf')

面向对象

定义

class Student:
"""学生"""
def __init__(self, name, age):
"""初始化方法"""
self.name = name
self.__age = age
def study(self, course_name):
"""学习"""
print(f'{self.name}正在学习{course_name}.')
def play(self):
"""玩耍"""
print(f'{self.name}正在玩游戏.')
def __repr__(self):
"""类似于java中toString方法,将对象内容按如下输出"""
return f'{self.name}: {self.age}'
def __del__(self):
"""类似于java中finalize方法,对象被销毁时调用"""
print(f'{self.name}对象被销毁了.')
stu1 = Student('truman', 18)

__age 表示一个私有属性,私有属性不是真正的私有,通过其他方式还是可以获取的。

进阶

class Student:
def __init__(self, name, age):
self.__name = name
self.__age = age
# 属性访问器(getter方法) - 获取__name属性
@property
def name(self):
return self.__name
# 属性修改器(setter方法) - 修改__name属性
@name.setter
def name(self, name):
# 如果name参数不为空就赋值给对象的__name属性
# 否则将__name属性赋值为'无名氏',有两种写法
# self.__name = name if name else '无名氏'
self.__name = name or '无名氏'
@property
def age(self):
return self.__age
stu = Student('王大锤', 20)
print(stu.name, stu.age) # 王大锤 20

通过一个@符号表示将装饰器应用于类、函数或方法

静态方法和类方法

@staticmethod
def is_valid(a, b, c):
"""判断三条边长能否构成三角形(静态方法)"""
return a + b > c and b + c > a and a + c > b
@classmethod
def is_valid(cls, a, b, c):
"""判断三条边长能否构成三角形(类方法)"""
return a + b > c and b + c > a and a + c > b

继承与多态

class Person:
"""人类"""
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print(f'{self.name}正在吃饭.')
def sleep(self):
print(f'{self.name}正在睡觉.')
class Student(Person):
"""学生类"""
def __init__(self, name, age):
# super(Student, self).__init__(name, age)
super().__init__(name, age)
def study(self, course_name):
print(f'{self.name}正在学习{course_name}.')
class Teacher(Person):
"""老师类"""
def __init__(self, name, age, title):
# super(Teacher, self).__init__(name, age)
super().__init__(name, age)
self.title = title
def teach(self, course_name):
print(f'{self.name}{self.title}正在讲授{course_name}.')

抽象类

ABC 是 Python 标准库中的一个模块,它用于定义抽象基类。

abstractmethod 装饰器用于定义在抽象基类中必须被子类实现的方法

from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
circle = Circle(5)
print(circle.area())

枚举类

from enum import Enum
class Suite(Enum):
"""花色(枚举)"""
SPADE, HEART, CLUB, DIAMOND = range(4)

线程

方式一:通过继承 Thread 类创建线程

import threading
# 自定义线程类
class MyThread(threading.Thread):
def run(self):
print("Hello from thread")
# 创建线程实例并启动线程
thread = MyThread()
thread.start()

方式二:通过传递函数创建线程

import threading
# 线程执行的函数
def my_function():
print("Hello from thread")
# 创建线程实例并启动线程
thread = threading.Thread(target=my_function)
thread.start()

方式三:使用线程池

import concurrent.futures
# 线程执行的函数
def my_function():
print("Hello from thread")
# 创建线程池
with concurrent.futures.ThreadPoolExecutor() as executor:
# 提交任务给线程池
future = executor.submit(my_function)
# 获取任务的结果
result = future.result()
import os
import random
import time
from multiprocessing.pool import Pool
def echo(s):
print('task:{0} 进程的PID: {1} '.format(s, os.getpid()))
time.sleep(random.random() * 3)
p = Pool(4)
for i in range(10):
p.apply_async(echo, (i,))
p.close()
p.join()

文件与异常

读写文件

file = open('致橡树.txt', 'r', encoding='utf-8')
for line in file:
print(line, end='')
file.close()
file = open('致橡树.txt', 'r', encoding='utf-8')
lines = file.readlines()
for line in lines:
print(line, end='')
file.close()
file = open('致橡树.txt', 'a', encoding='utf-8')
file.write('\n标题:《致橡树》')
file.write('\n作者:舒婷')
file.write('\n时间:1977年3月')
file.close()

大文件读取

try:
with open('guido.jpg', 'rb') as file1, open('吉多.jpg', 'wb') as file2:
data = file1.read(512)
while data:
file2.write(data)
data = file1.read()
except FileNotFoundError:
print('指定的文件无法打开.')
except IOError:
print('读写文件时出现错误.')
print('程序执行结束.')

异常

python 中所有的异常都是BaseException的子类型,它有四个直接的子类,分别是:SystemExitKeyboardInterruptGeneratorExitException。其中,SystemExit 表示解释器请求退出,KeyboardInterrupt 是用户中断程序执行(按下 Ctrl+c),GeneratorExit 表示生成器发生异常通知退出。

# 定义自定义异常类
class MyException(Exception):
pass
# 抛出自定义异常
raise MyException("This is a custom exception")
# 定义自定义异常类
class MyException(Exception):
def __init__(self, message, code):
super().__init__(message)
self.code = code
def get_code(self):
return self.code
# 抛出自定义异常
raise MyException("This is a custom exception", 1001)

参考

  1. Python-100-Days
  2. Python-Core-50-Courses
  3. walter201230/Python