UTF-8规定英文用ASCII表示,中文用3个字节表示
Unicode规定无论中文还是英文都用2个字节表示
print(chr(0b100111001011000))#要加0b不然提示后面的01串太长
print(ord('乘'))
8python中的标识符和保留字
import keyword
print(keyword.kwlist)
9.变量的定义和使用
name = 'maliya'
print(id(name))
print(type(name))
print(name)
10.变量的定义和使用
11.python中常见的数据类型
12.整数类型
13.浮点类型
14.布尔类型
15.字符串类型
16.类型转换str和int函数
17.类型转换:float函数
18.python中的注释
第三章:
19.input函数的使用
20.input函数的高级使用
21.算数运算符
22.赋值运算符
23.比较运算符
==比较两个对象的值,is比较对象的标识
24.布尔运算符
25.位运算符
26.运算符的优先级
27.程序的组织结构-顺序结构
28.对象的布尔值
29.分支结构-单分支结构
30.分支结构-双分支结构
31.分支结构多分支结构
32.分支结构-嵌套if的使用
33.条件表达式
34.pass语句
if age 是因为python皆对象,只要值不是空,就是bool结果 就是 True,可以用来作为条件进行判断
35.range函数的使用
36.while循环
37.练习题:1到100的偶数和
not bool(a%2) 也即not False
38.for in 循环
39.for in 练习题
40.流程控制语句break
41.流程控制语句continue
42.else语句
43.嵌套循环
44.二重循环中的break和continue
45.为什么需要列表
1.列表的创建与删除
2.列表的查询操作
3.列表元素的增,删,改操作
4.列表元素的排序
5.列表推导式
列表可以存储多个元素,并且可以存储不同的数据类型
46.列表对象的创建
使用中括号[]或者使用list()
47.列表的特点
元素按顺序有序排序
索引映射唯一个数据
可以存储重复数据
任意数据类型混存
根据需要动态分配和回收内存
48.获取指定元素的索引
49.获取列表中指定的元素
50.获取列表中的多个元素-切片操作
切出来的结果是一个新的对象
51列表元素的判断及遍历
52列表元素的添加操作
切片是从指定位置开始插入元素,指定位置开始原先的元素被删除
53列表元素的删除操作
想要删除某些元素可以用‘【】’进行赋值
54列表元素的修改操作
55列表元素的排序操作
56列表生成式
57什么是字典
可以进行增、删、改等改变的操作,就是可变序列,相反不可进行如上操作的就是不可变序列,整数,字符串就是不可变序列,列表,字典都是可变序列
字典是无序的,列表是有序,是因为往字典中存元素的时候,用的是hash函数计算的存储位置
58字典的实现原理
底层hash(key)查找key对应的值
59字典的创建
60字典元素的获取
两种元素的获取方式的区别在于:当用[],key不存在时会报错,但是.get()方法的key不存在时,会返回none
.get('key',99),当字典中不存在key键时,默认返回99
61字典元素的增删改
62获取字典视图
63字典元素的遍历
64字典的特点
65字典生成式
66什么是元组
####################下面的print结果是一样的,所以说列表是可比那序列 还有字典
lst = [10,20,45]
print(id(lst))
lst.append(30)
print(id(lst))
####下面的print结果是不一样的,所以说字符串是不可变序列,还有元组
s = ‘hello'
print(id(s))
s = s+'world'
print(id(s))
67元组的创建方式
t3 = 'python','world',98也是一个元组类型,是因为省略括号,默认是元组
但是t2 = 'python'为了被误认为是字符串,所以加括号变成:t2 = ('python'),依然被程序认为是str类型,此时就对应上图中最后一条规则,只有一个元素是需要加逗号的
68为什么要元组设计成不可变序列
69元组的遍历
70集合的概述与创建
71集合的相关操作
72集合间的关系
73集合的数学操作
74集合生成式
75字符串的创建与预留机制
字符串在python中是基本数据类型,是一个不可变的字符序列
76字符串的常用操作,字符串的查询
77字符串的常用操作,字符串的大小
78字符串内容对齐操作方法
79字符串的劈分
80字符串判断的相关方法
81替换与合并
82字符串的比较操作
83字符串的切片操作
84格式化字符串
85字符串的编码与解码
86函数的定义与调用
87函数调用的参数传递-位置实参-关键字实参
88函数参数传递的内存分析
89函数的返回值
90函数参数定义-默认值参数
91个数可变的位置形参和关键字形参
92函数的参数总结
93变量的作用域
94递归函数------------------------------95非伯纳切数列
106类与对象
107 定义python 的类
规范:创建类的名的首字母要大写
在静态方法中是不允许使用self 的
init为赋值操作,self.name为实例属性
类外定义的称为函数,类内定义的称为方法
108对象的创建
#1.定义python中的类
'''class Student:
pass
#python中一切皆对象,所以Student 有id:内存空间,类型,值
print(id(Student))#内存空间 3095599168800
print(type(Student))#类型 <class 'type'>
print(Student)#值 <class '__main__.Student'>'''
class Student:
native_pace = 'jilin'#直接写在类里的变量,称为类属性
def __init__(self,name,age):#称为实例属性,\
#进行了一个赋值的操作,将局部变量的name的值赋值给实例变量
self.name = name
self.age = age
#实例方法:类之外定义的叫函数,之内叫方法
def eat(self): #self可以不写,即使不写,默认也是存在的,也可以用其他的字符代替
print('学生在吃饭')
#静态方法。该方法中不允许写self
@staticmethod
def method():
print('我使用了staticmethod进行修饰,所以我是静态方法')
#类方法,要求传递 cls
@classmethod #注意后面加()和不加()的区别
def cm(cls):
print('我是类方法,应为使用了classmethod进行修饰')
#2.对象的创建 又成为实例化
stu1 = Student('张三', 20)
print(id(stu1))
print(type(stu1))
print(stu1) #输出0x000002D0C1FBF730 实际上就是id(stu1)的值(3095630968624 )
print(stu1.native_pace)
print(stu1.name)
#Student为类对象,根据Student创建出来的叫做实例对象
stu1.eat()
Student.eat(stu1)
#类方法、静态方法的使用方法
Student.cm()
Student.method()
109.类属性、类方法、静态方法的使用方式
110.动态绑定属性和方法
#动态绑定属性和方法
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print(self.name+'在吃饭')
stu1 = Student('张三', 20)
stu2 = Student('李四',30)
print(id(stu1))
print(id(stu2))
print('--------------为stu2动态绑定属性')
stu2.gender='女'
print(stu1.name,stu1.age) #如果让stu1.gender是有问题的
print(stu2.name,stu2.age,stu2.gender)
print('----------------------')
stu1.eat()
stu2.eat()
def show():
print('定义在类之外的,称为函数')
stu1.show=show()
stu1.show
11 0.面向对象的三大特征==封装的实现方式
class Car:
def __init__(self,brand):
self.brand = brand
def start(self): #不要忘了括号里面的self
print('汽车已经启动')
car = Car('baomaX5')
car.start()
print(car.brand)
class Student:
def __init__(self,name,age):
self.name = name
self.__age = age #年龄不希望在类的外部进行访问,所以使用了两个_ _
def show(self):
print(self.name,self.__age)
stu = Student("zhangsan", 20)
stu.show()
#在类的外部使用name age
print(stu.name)
#print(stu.__age)
print(dir(stu))#返回下面的内容
'''['_Student__age', '__class__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'name', 'show']'''
print(stu._Student__age)#这样就可以获取到了 可以通过_Student__age访问
111继承及实现方式
#继承的代码实现
class Person(object):#继承了object类
def __init__(self,name,age):
self.name = name
self.age = age
def info(self):
print('姓名{0},年龄{1}'.format(self.name, self.age))
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age)
self.score = score
class Teacher(Person):
def __init__(self,name,age,teachyear):
super().__init__(name,age)
self.teachyear = teachyear
stu = Student('Jack', 20, 100)
teacher = Teacher('Mike', 34, 10)
stu.info()
teacher.info()
112方法重写
#方法重写
class Person(object):#继承了object类
def __init__(self,name,age):
self.name = name
self.age = age
def info(self):
print('姓名{0},年龄{1}'.format(self.name, self.age))
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age)
self.score = score
#方法重写
def info(self):
super().info()
print(self.score)
class Teacher(Person):
def __init__(self,name,age,teachyear):
super().__init__(name,age)
self.teachyear = teachyear
#方法重写
def info(self):
super().info()
print(self.teachyear)
stu = Student('Jack', 20, 100)
teacher = Teacher('Mike', 34, 10)
stu.info()
teacher.info()
113、Object类
class Student():
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return '我的名字是{},今年{}岁了'.format(self.name, self.age)
stu = Student('zhangsan',30)
print(dir(stu))#输出['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__'
print(stu) #输出我的名字是zhangsan,今年30岁了默认调用__str__这样的方法
print(type(stu))#输出<class '__main__.Student'>
114.多态的实现
有人说python中没有多态的概念,因为python的变量是没有数据类型的
虽然没有数据类型,但是具有多态的属性
python是动态语言,可以在创建对象之后,动态的绑定属性和方法
动态语言多崇尚鸭子类型,不需要关心对象是什么类型,到底是不是鸭子,只关心对象的行为
不需要关心person是谁的子类,只关心person是否具有eat的行为
115特殊属性
#特殊的属性
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name = name
self.age = age
x = C('Jack',20)
print(x.__dict__)# 实例属性的字典 {'name': 'Jack', 'age': 20}
print(C.__dict__)#{'__module__': '__main__', '__init__': <function C.__init__ at 0x000002D0C1E01D30>, '__doc__': None}
print(x.__class__)#<class '__main__.C'> 对象所属的类
print(C.__bases__)#C类的父类类型的元素 (<class '__main__.A'>, <class '__main__.B'>)
print(C.__base__)#<class '__main__.A'>
#如果class C(B,A) 那么上面输出<class '__main__.B'>
print(C.__mro__)#类的层次结构 (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
a = 20
b = 100
c = a+b
d = a.__add__(b)
print(c,d)
class Student():
def __init__(self,name):
self.name = name
def __add__(self,other):
return self.name + other.name
stu1 = Student('zhangsan')
stu2 = Student('lisi')
print(stu1+stu2) #zhangsanlisi
print(stu1.__add__(stu2))#zhangsanlisi
class Person(object):
def __new__(cls,*args,**kwargs):
print('__new__被调用执行了.cls的值为{0}'.format(id(cls)))
super().__new__(cls)
obj = super().__new__(cls)
print('创建的对象的ID为:{0}'.format(id(obj)))
return obj
def __init__(self,name,age):
print('__init__被调用了,self的id值为{0}'.format(id(self)))
self.name = name
self.age = age
print('object这个类对象的ID为:{0}'.format(id(object)))
print('Person这个类对象的ID为:{0}'.format(id(Person)))
p1 = Person('张三',20)
print('p1这个person类的实例对象的ID:{0}'.format(id(p1)))
'''
object这个类对象的ID为:140715812174672
Person这个类对象的ID为:2868079632752
__new__被调用执行了.cls的值为2868079632752
创建的对象的ID为:2868110306848
__init__被调用了,self的id值为2868110306848
p1这个person类的实例对象的ID:2868110306848'''
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))#<__main__.CPU object at 0x0000029BC87EB7C0> 2868106934208
print(cpu2,id(cpu2))#<__main__.CPU object at 0x0000029BC87EB7C0> 2868106934208
#(2)类的浅拷贝
print('-----------------------------')
disk = Disk()
print(disk,id(disk))#<__main__.Disk object at 0x0000029BC8B7B340> 2868110668608
computer = Computer(cpu1,disk)
import copy
computer2 = copy.copy(computer)
print(computer,computer.cpu,computer.disk)
#<__main__.Computer object at 0x0000029BC8B226D0> <__main__.CPU object at
#0x0000029BC8B22E80> <__main__.Disk object at 0x0000029BC87D34C0>
print(computer2,computer2.cpu,computer2.disk)
#<__main__.Computer object at 0x0000029BC8B22DC0> <__main__.CPU object at
#0x0000029BC8B22E80> <__main__.Disk object at 0x0000029BC87D34C0>
import schedule
import time
def job():
print('haha')
schedule.every(3).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
126.编码格式的介绍
UTF-8是unicode的具体实现方式
127.文件读写的原理,读取磁盘文件中的内容
128常见的文件打开模式
129文件对象的常用方法
130with语句
131 OS 模块的常用函数