课堂笔记:
- 变量的三大特征
type:
查看数据的类型
id:
表示变量在内存中的地址,是一串数字
value:
变量的值
- 流程控制
对流程的控制,控制某些事情的发生。
- if分支判断:
语法:
if 判断条件:
# 条件若成立执行
print("条件成立!")
# 若if条件不成立,则执行此处
elif 判断条件:
print("另一个条件成立")
# 若if条件不成立,则执行此处
elif 判断条件:
print("另一个条件成立")
# 若if与elif条件不成立,则执行此处else:
# 若条件不成立则执行
print("条件不成立!")
```
while循环:
语法:
while判断条件:
# 若条件成立则“循环”执行以下代码
print("111")
break: 结束本层循环
```
# import time
# while Ture:
# print(11111)
# time,sleep(1) # break
代码:
username = input ("请输入用户名:")print(username)
password = input("请输入密码:")
print(password) if username =="warren" and password =="666666":
print("登陆成功")
else:
print("登陆失败") str = "everybaby666" print("warren:%s" %(str,)) # - 算术运算符
print(1+1) # 2
ni = 1
# n1 = n1 + 1# n1 += 1
# print(n1) # 2
n1 = 1 # 赋值运算符: -=
print(10 - 1) #9 # *=
print(12 * 12) # 144
print(11/3) # 3.666...
print(11//3) # 3
print(11 % 3) # 2 # 比较运算符
print(1 == 2) # False
print(1 > 2) # False
print(1 < 2) # Ture
print(1 >= 2) # False
print(1 <= 2) # Ture # is:比较两个变量的id是否相等
x = 10
print(id(x) == id(x)) # Ture
print(x is x) # True # 逻辑运算符
# and: 左右两边的条件都为Ture才为True,否则为False
print(x == 1 and x == 10) # False
print(x == 10 and x == 10) # True # or:判断两边的条件是否成立,其1成立则为True
print(x == 10 or x == 1) # True # not:取反
print(x == 10) # True
print(not x == 10) # False # no-->and-->or
# True or False
print(not x == 1 or x == 10 and x == 20) # True # 链式赋值
n = 10
y = n
z = y
print(n,y,z) n = y =z = 10
print(n, y, z,) # 交叉赋值
x = 10
y = 20
# x与y的值互换
# x = 20 y = 10
z = x # 10
x = y # 20
y = z # 10 x = 10
y = 20
# 10, 20 = y= 10, x=20
x, y = y, x
print(x, y ) # 20 10 # 解压缩赋值
# 0 1 2
list1 = [1,2,3]
x = list1[0]
y = list1[1]
z = list1[2]
print(x,y,z) # 1 2 3
x, y, z = list1
print(x,y,z) # 1 2 3 # 猜字符游戏:猜测用户输入的字符是否是9527. number = 9527 guess = input("请输入猜测数字:") print(guess)
print(type(guess)) # str # 把字符串类型转化为整型
guess = int(guess)
print(type(guess)) # int if guess == number:
print("猜对了") elif guess < number:
print("猜小了") else:
print("猜大了!") # 猜字符游戏:猜测用户输入的字符是否是9527,限制只能猜3次。
number = 9527
init = 1 # 1 2 3 4
while True:
# 若init大于3结束循环
if init > 3:
print("猜错3次了,gun吧!")
break guess = input("请输入猜测数字:")
guess = int(guess) if guess == number:
print("猜对了!")
break elif guess < number:
print("猜小了") else:
print("猜大了!") # 若没有退出循环证明猜错了,init + 1
init +=1 list1 = [1, 2, 3, 4, 5]
# list1_len = len(list1) # 5
# while list1_len > 0:
# list1_len -=1
# =print(list1_[list1_len])
# 依次循环取出list1中的每一个值
for x in list1:
print(x)