day02
while--关键字(死循环)
-
格式:while 条件:
循环体
print(1) while True: print("痒") print("鸡你太美") print("卡路里") print("好运来") print("小三") print("小白脸") print("趁早") print("过火") print(2) falg = True while falg: print(1) print(2) # 正序的示范 count = 1 while count <= 5: print(count) count = count + 1 # 倒序的示范 count = 5 while count: print(count) count = count - 1 # 正序打印 25 - 57 count = 25 while count < 58: print(count) count = count + 1 # 倒序打印 57 - 25 count = 57 while count > 24: print(count) count = count - 1
-
数字中非零都是True 零是False
# print(bool(0)) # 数字中非零的都是True
-
break:终止循环,break下面的代码不会执行
while True: print(123) print(234) break # 终止当前循环,break下方的代码不会进行执行 print(345) print(1111)
-
continue:结束本次循环,直接跳入下一次循环(伪装成循环体中的最后一行),下面的代码不会执行
while True: print(123) print(234) print(345) while True: print(123) print(234) continue #continue 伪装成循环体中的最后一行代码(跳出当前循环继续下次循环) #continue 下面的代码不会执行 print(345) print(1111)
-
while else :是一个总体(循环),当条件成立的时候不执行,当条件不成立的时候执行
while True: print(111) break else: print(222) while True: print(111) break print(222) # while else # 0.退出 # 1.开始登录 # 如果用户选择序号0就提示用户退出成功 # 如果用户选择序号1就让用户输入用户名和密码然后进行判断,正确就终止循环,错误就重新输入 srr = """ 0.退出 1.开始登录 """ print(srr) num = int(input("请输入你要的数字:")) while num == 1: name_input = input("请输入你的用户名:") password_input = input("请输入你的密码:") name = "alex" password = "123" if name_input == name and password_input: print("登录成功") break else: print("用户名或者密码错误,请重新输入!") else: print("退出成功")
-
总结:
- 打断循环的方式:
- 自己修改条件
- break
- 打断循环的方式:
-
break -- 打破当前循环 (终止当前循环)
- continue -- 跳出当前循环继续下次循环(将continue伪装成循环体中的最后一个行代码)
- break和continue相同之处:他们以下的代码都不执行
字符串格式化
a = "------------- info -------------"
b = "name:"
c = "age:"
d = "job:"
e = "-------------- end -------------"
name = input("name")
age = input("age")
job = input("job")
print(a + "
" + b + name + "
" + c + age + "
"+ d + job + "
" +e)
-
%就是占位
-
%%(转义) 转义成普通的%
-
%s 是占的字符串类型的位置
-
%d 是占的数字类型的位置
-
按照位置顺序传递,占位和补位必须要一一对应
s = """ ------------- info ------------- name:%s age:%s job:%s -------------- end ------------- """ name = input("name") age = int(input("age")) job = input("job") print(s%(name,age,job)) # num = input("学习进度:") # s = "大哥黑的学习进度为:%s%%" # print(s%(num)) # num = input("学习进度:") # num1 = input("aaaa:") # s = "大哥黑的学习进度为:%s%" # print(s%(num,num1)) # num = input("学习进度:") # num1 = input("aaaa:") # s = "大哥黑的学习进度为:%s%s" # print(s % (num, num1))
-
扩展:f"{变量名}{字符串}"3.6版本及以上才能使用
# s = f"今天下雨了{input('>>>')}" # print(s) # s = f"大黑哥的学习进度为{input('<<<')}" # # print(s) # s = f"{1}{2}{3}" # print(s)
运算符
算术运算符
-
-
-
- / 除({5/2结果}python2中结果是整数,python3中结果是小数)
- // 整除(地板除)
- ** 幂(次方)
- % 模(取余)
比较运算符
- --- >
- --- <
- --- ==
- --- !=
- --- >=
- --- <=
赋值运算符
- = 赋值
- += 自加
- -= 自减
- *= 自乘
- /= 自除
- //=
- **=
- %=
逻辑运算符
-
and (与):全真则真、
- 都为真的时候取and后面的值
- 都为假的时候取and前面的值
- 一真一假取假的值
# print(3 and 4) # print(0 and 4) # print(0 and False) # print(3 and 5 and 9 and 0 and False) # print(5 and False and 9 and 0) # print(1 and 2 and 5 and 9 and 6)
-
or(或):一真则真
- 都为真的时候取or前面的值
- 都为假的时候取or后面的值
- 一真一假取真的值
# print(1 or 0) # print(1 or 2) # print(0 or False) # print(1 or 9 or 4 or 0 or 9)
-
not(非):对立的
# print(not False)
-
级别:()> not > and > or
-
顺序:从左向右执行
# print(9 and 1 or not False and 8 or 0 and 7 and False)
成员运算符
- in:存在
- not in:不存在
# s = "alexdsb"
# if "sb" not in s:
# print(True)
# else:
# print(False)
编码初始
编码集(密码本)
# 今 0101
# 天 0110
# 晚 0010
# 上 0001
# 去 1001
# 便 1000
# 利 0100
# 店 1111
#
#
# 00000101 00000110 0010000110011001
- ascii(美国):不支持中文
- gbk(国家标准密码本):英文8位(1字节),中文16位(2字节)
- unicode(万国码):英文16位(2字节),中文32位(4字节)
- utf-8(可变长的编码,最流行的):英文8位(1字节),欧洲文16位(2字节),亚洲24位(3字节)
- linux系统 -- utf-8
- mac系统 -- utf-8
- windows系统 -- gbk
- 单位转换:
- 一字节(1Bytes) = 8位(8bit)
- 1024Bytes = 1KB
- 1024KB = 1MB
- 1024MB = 1GB
- 1024GB = 1TB 常用的
- 1024TB = 1PB
- 1024PB = 1EB