@
1. 标记/清除算法的核心:
# 如果一个值没有关联任何一条以直接引用作为出发点的引用,那么该值就会被标记下来,并且清除
# 如果一个变量值不存在任何以栈区为出发点的引用,那么标记/清除算法就认为它是垃圾
一.与用户交互
1.input(控制计算机能够像人一样接收外界输入的内容)
# name = input("请输入你的账号:") # "[1,2,3]"
# print(name,type(name))
# print(name == "egon")
# age = input("请输入你的年龄:") # age="19"
# age=int(age)
# print(age > 18) # "19" > 18
# s1 = "abcdef"
# s2 = "az"
# print(s2 > s1)
# l1 = [111,22,333]
# l2 = [111,666]
# l2 = [111,"666"]
# print(l1 > l2)
2.print(控制计算机能够像人一样把自己的处理结果输出给用户)
# print("hello1",end='*')
# print("hello2",end='*')
# print("hello3",end='*')
# msg = "my name is %s my age is %s" % ("lzl","18")
# msg = "my name is %s my age is %d" % ("lzl",18)
# msg = "my name is %s my age is %s" % ("lzl",18)
# msg = "my name is %s my age is %s" % ("lzl",[1,2,3])
# print(msg)
二.流程控制之if判断
完整语法:
if 条件1:
代码1
...
elif 条件2:
代码1
......
else:
代码1
...
1、单分支
# print('start...')
# inp_name = input("请输入您的用户名:")
# if inp_name == "egon":
# print('ok')
# print('end...')
2、双分支
# print('start...')
# inp_name = input("请输入您的用户名:")
# if inp_name == "egon":
# print('ok')
# else:
# print('error')
# print('end...')
3、多分支
# 如果:成绩>=90,那么:优秀
# 如果成绩>=80且<90,那么:良好
# 如果成绩>=70且<80,那么:普通
# 其他情况:很差
# score = input("your score: ")
# score = int(score)
# if score >= 90:
# print("优秀")
# if 10 > 3:
# print('ok')
# elif score >= 80 and score < 90:
# print("良好")
# elif score >= 70 and score < 80:
# print("普通")
# else:
# print('很差')
三.流程控制之while 和 for
# while True:
# inp_name = input("username: ")
# inp_pwd = input("password: ")
#
# if inp_name == "egon" and inp_pwd == "123":
# print("登录成功")
# break
# else:
# print("账号密码错误")
#
# print('end...')
"""
语法
while 条件:
代码1
代码2
代码3
"""
一 基本使用
# i=0
# while i < 5: # 5 < 5
# print(i) # 4
# i += 1 # i=5
"""
0
1
2
3
4
"""
二 死循环:永远也不结束的循环
# while True:
# # print('1111')
# 1+1
三 结束while循环的两种方式
# 1、break:直接终止本层循环,不会有下一次循环
# tag = True
# while tag:
# print('start...')
# break
# print('end...')
# 2、条件改为假:不会直接终止本层循环,会在下一次循环时终止
# tag = True
# while tag:
# print('start...')
# tag = False
# print('end...')
# 案例
# tag = True
# while tag:
# inp_name = input("username: ")
# inp_pwd = input("password: ")
#
# if inp_name == "egon" and inp_pwd == "123":
# print("登录成功")
# # break
# tag = False
# else:
# print("账号密码错误")
# print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
#
# print('end...')
# while True:
# while True:
# while True:
# break
# break
# break
#
# tag = True
# while tag:
# while tag:
# while tag:
# tag=False
四:while+continue:终止本次循环,直接进入下一次
# i = 0
# while i < 5:
# if i == 3:
# i+=1
# continue
# print(i) #
# i+=1 # i=3
# 案例
# while True:
# inp_name = input("username: ")
# inp_pwd = input("password: ")
#
# if inp_name == "egon" and inp_pwd == "123":
# print("登录成功")
# break
# else:
# print("账号密码错误")
# # continue # 画蛇添足
#
# print('end...')
五 while+else
i = 0
while i < 5:
if i == 3:
# i+=1
# continue
break
print(i)
i+=1
else:
print('xxxxxxxxxxxxxxxx')
六.for循环
# dic = {'k1':111,'k2':222,'k3':3333}
# for x in names:
# print(x)
# for x in dic:
# print(x,dic[x])
# ll = [[111,222],[333,444],[555,6666]]
# for x,y in ll: # x,y=[111,222]
# print(x,y)
1、for + break
# for x in [111,222,33,444,555]:
# if x == 33:
# break
# print(x)
2、for + continue
# for x in [111,222,33,444,555]:
# if x == 33:
# continue
# print(x)
3、for + else
# for x in [111,222,33,444,555]:
# if x == 33:
# # break
# continue
# print(x)
# else:
# print('xxxxxxxxxxxxxxx')
4、for+range
# ll = [111,222,33,444,555]
# for i in range(len(ll)):
# print(ll[i])
5、for+enumerate
# ll = [111,222,33,444,555]
# for i,v in enumerate(ll): # i,v=(0, 111)
# print(i,v)
6、for循环嵌套
for i in range(3): # i=2
print('=================>外层循环', i)
for j in range(5):
print("内层循环", j)