今日内容
1、内存管理机制
垃圾回收机制GC
1.引用计数:被引用的个数
2.分代回收:为了解决引用计数的效率问题
问题:1.个别垃圾可能得不到及时的清理
3.标记/清除:为了解决循环引用带来的内存泄露问题
核心:一个变量值没有任意一条可以从栈区出发到自己的引用,就会把标记下 来,方便后续清除。
小整数池
2.与用户交互
接受用户输入
python3中的input会把用户输入的所有内容都存储成str类型
python2中raw_input()与python3的Input一样
age = int(input("请输入您的年龄:"))
print(age)
补充:int可以把纯数字组成的字符串转换整型
格式化输出
print("my name is egon and my age is 18")
str_2 = "my name is %s and my age is %s" % ("egon", 18)
print(str_2)
str_1 = "my name is %s and my age is %s" % ("egon", [1, 2, 3])
print(str_1)
3.运算符
3.1算数运算符:+、-、*、/、//、%、**
补充:python是一门解释型、强类型、动态语言
GO是一门编译型型、强类型、静态语言
3.2比较运算符
判断是否相等,没有类型限制
print("abc" == 10) # 判断的是值及其类型是否相等
print("abc" != 10) # 判断的是值及其类型是否相等
x = "abcdefg"
y = "abz"
print(x > y)
x = [111, 'abcdefg', 666]
y = [111,'z']
print(x > y)
3.3赋值运算符
3.3.1 增量赋值
age = 18
age += 1 # age = age + 1
age -= 1 # age = age - 1
print(age)
3.3.2 链式赋值
x = y = z = 100
3.3.3 交叉赋值
x = 100
y = 200
temp = x
x = y
y = temp
del temp
x,y=y,x
print(x) # 200
print(y) # 100
3.3.4 解压赋值
salaries = [11, 22, 33, 44, 55]
mon1 = salaries[0]
mon2 = salaries[1]
mon3 = salaries[2]
mon4 = salaries[3]
mon5 = salaries[4]
mon1, mon2, mon3, mon4, mon5 = salaries
mon1, mon2, mon3, mon4, mon5 ,mon6= salaries # 错误
mon1, mon2, mon3, mon4 = salaries # 错误
print(mon1, mon2, mon3, mon4, mon5)
mon1,mon2,*_ ,last = salaries
print(mon1)
print(mon2)
print(_)
_,*x,_ = salaries
print(x)
# 了解:
# x, y, z = {'k1': 111, 'k2': 222, 'k3': 3333}
# x, y, z = "abc"
# print(x,y,z)
3.4 逻辑运算符
# 逻辑运算符是用来运算条件的,那什么是条件???
# 只要结果为布尔值的都可以当做条件
# 总结:逻辑运算符,算的是显式的布尔值或者隐式的布尔值
#3.4.1 not
print(not 10 > 3)
print(not False)
print(not 123)
# 3.4.2 and: 链接多个条件,多个条件必须同时成立,最终结果才为True
print(10 > 3 and True and 'xx' == 'xx')
print(10 > 3 and False and 'xx' == 'xx')
print(10 > 3 and None and 'xx' == 'xx')
print(10 > 3 and True and 'xx' == 'xx' and 111)
# 3.4.3 or: 链接多个条件,多个条件但凡有一个成立,最终结果就为True
print(10 > 3 or False or 'xx' == 'xx')
print(0 or None or "" or 1 or True or 10 > 3)
# 强调:优先级not>and>or
print(3 > 4 and 4 > 3 or 1 == 3 and not 'x' == 'x' or 3 > 3)
#(3 > 4 and 4 > 3) or (1 == 3 and not 'x' == 'x') or 3 > 3
# ps:短路运算-》偷懒原则
# 3.5 成员运算符
print("abc" in "xxxabcxxxxx")
print(111 in [222,333,1111])
print('k1' in {'k1':111,'k2':222}) # 字典的成员运算判断的是key
print('k3' not in {'k1': 111, 'k2': 222}) # 推荐
print(not 'k3' in {'k1': 111, 'k2': 222}) # 不推荐
# 3.6 身份运算符:is判断的是id是否一样
x = 100
y = x
print(x is y)
print(x == y)
l1 = [111]
res=l1.append(222)
print(res == None) # 可以,但是不够好
print(res is None) # 推荐
总结:==成立is不一定成立,但是is成立==一定成立
is:是比较内存地址一不一样
==:是比较值是否相等