今日内容:
1、内存管理机制
垃圾回收机制GC
引用计数
分代回收
标记/清除
小整数池
2、与用户交互
接收用户输入
格式化输出
3、运算符
4、流程控制之if判断
**一 垃圾回收机制GC** # 1.1 引用计数 # 引用计数增加 x = 18 # 值18的引用计数为1 y = x # 值18的引用计数为2 l = [111, x] # 值18的引用计数为3 # print(id(x)) # print(id(y)) # print(id(l[1])) # 引用计数减少 # del x # 值18的引用计数为2 # y = 100 # 值18的引用计数为1 # del l[1] # 值18的应用计数为0 # 1.2 分代回收:为了解决引用计数的效率问题 # 问题:个别垃圾有可能得不到及时的清理 # 1.3 标记/清除:为了解决循环引用带来的内存泄漏问题 # 核心:一个变量值没有任意一条可以从栈区出发到自己的引用,就会被标记下来,方便后续清除 # lll = [66,77,[88,99]] # l1 = [111] # l2 = [222] # l1.append(l2) # l2.append(l1) # print(l1) # l1 = [111的内存地址,l2的内存地址] # print(l2) # l2 = [222的内存地址,l1的内存地址] # print(l1[1][1]) # del l1 # print(l2[1][0]) # del l2 **二 小整数池** x = "abc" y = "abc" print(id(x)) print(id(y))
**1、接收用户输入** python3中的input会把用户输入的所有内容都存成str类型 age = input("请输入您的年龄: ") # "内容" print(age,type(age)) age=int(age) print(age > 10) # "18" > 10 补充:int可以把纯数字组成的字符串转换整型 int("31") # ok res = int(" 31 ") # ok res = int("3 1") # no print(res,type(res)) python2(***) raw_input()与python3的input一模一样 input()要求用户必须输入一个明确的数据类型,输入什么类型就存成什么类型 **2、输出** print("hello1",end='*') print("hello2",end='*') print("hello3",end='*') msg = "my name is %s my age is %s" % ("egon", "18") msg = "my name is %s my age is %d" % ("egon", 18) msg = "my name is %s my age is %s" % ("egon", 18) msg = "my name is %s my age is %s" % ("egon", [1,2,3]) print(msg)
**2.1 算术运算符** x = 10 y = 20 print(x + y) print(10 + 20) print(10 // 3) print(10 / 3) print(10 % 3) print(10 ** 2) print(10 + 3.1) 了解:+与* print("abc"+"def") print([1,2,3]+[3,4,5]) print("abc"*3) print([1,2,3]*3) 补充:python是一门解释型、强类型、动态语言 补充:go是一门编译型、强类型、静态语言 "18" + 10 x = 10 **2.2 比较运算符** 判断是否相等,没有类型限制 print("abc" == 10) # 判断的是值及其类型是否相等 print("abc" != 10) # 判断的是值及其类型是否相等 > >= < <=:主要用于数字类型 print(10 > 3.1) 了解:> >= < <=也可以给其他类型用,但仅限于同类型之间 x = "abcdefg" y = "abz" print(x > y) x = [111, 'abcdefg', 666] y = [111,'z'] print(x > y) **2.3 赋值运算符** 2.3.1 增量赋值 age = 18 age += 1 # age = age + 1 age -= 1 # age = age - 1 print(age) **2.3.2 链式赋值** x = y = z = 100 2.3.3 交叉赋值 x = 100 y = 200 temp = x x = y y = temp del temp x,y=y,x print(x) # 200 print(y) # 100 2.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) **2.4 逻辑运算符** 逻辑运算符是用来运算条件的,那什么是条件??? 只要结果为布尔值的都可以当做条件 **总结:逻辑运算符,算的是显式的布尔值或者隐式的布尔值** 2.4.1 not print(not 10 > 3) print(not False) print(not 123) 2.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) 2.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 **2.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}) # 不推荐 **2.6 身份运算符**:is判断的是id是否一样 x = 100 y = x print(x is y) print(x == y) **总结:==成立is不一定成立,但是is成立==一定成立** l1 = [111] res=l1.append(222) print(res == None) # 可以,但是不够好 print(res is None) # 推荐