1.while 循环
循环:不断地重复做某件事情就是循环
while 关键字
while True: #死循环
print("坚强")
print("过火")
print("单身情歌")
print("痒")
print(1111)
while True: #死循环
print("坚强")
print("过火")
print("单身情歌")
print("痒")
循环5次
count = 0
while True: # 死循环
count = count + 1 # 先执行 = 右边的内容
if count == 5:
print(count)
break # 有限循环
count = 0
while True: #死循环
count=count+1 #先执行=右边的内容
if count == 5:
print(count)
break # 有限循环
count = 0
while True: # 死循环
count = count + 1 #先执行 = 右边的内容
if count == 5:
print(1111)
continue # continue 就是伪装成循环体中最后一行代码
print(count)
count = 0
while True: #死循环
count = count+1 #先执行 = 右边的内容
continue # continue 就是伪装成循环体中最后一行代码
print(count)
while循环中的两个关键字:
break:continue
break:终止当前循环
continue:跳出本次循环,继续下次循环(就是伪装成循环体中最后一行代码)
与continue,break循环体中,下方的代码都不会执行
while 条件:
缩进 循环体
通过条件控制循环次数
count = 0
whilecount<2:
print(count)
count = count+1
4~67(包含4和67)
count = 4
while count < 68:
print(count)
count = count + 1
100~6包含(100和6)
count = 100
while count>5:
print(count)
count = count-1
1,3,5,7,9
count = 1
while count<10:
print(count)
count = count+2
while wlse 与 if else相似
print(222)
count = 0 #计算器
while count < 3:
print(count) # 一直死循环,计算器没有进行运算
print(111)
print(222)
count(0)
while count < 3:
print(count)
count = count + 1
print(111)
print(222)
count=0
while count<3:
print(count)
count = count + 1
else:
print(111)
print(222)
count=0
while count <3:
print(count)
count = count + 1
break
print(111)
运行结果:222 0 111
print(222)
count = 0
whle count<3:
print(count)
count = count + 1
break
else:
print(111)
运行结果:222 0
print(222)
count = 0
while count<3:
print(count)
count = count + 1
else:
print(111)
运行结果:222 0 1 2 111
2.格式化
msg = """
------info------
name:meet
age:18
sex:男
hobby:女
-------end------
"""
a = "------info------"
b = "name:"
c = "age:"
d = "sex:"
e = "hobby:"
f = "-------end------"
name = input("name")
age = input("age")
sex = input("sex")
hobby = input("hobby")
print(a)
print(b + name)
print(c + age)
print(d + sex)
print(e + hobby)
print(f)
name = input("name")
age = input("age")
sex = input("sex")
hobby = input("hobby")
a = "哈哈啊"
msg = """
------info------
name:%s
age:%s
sex:%s
hobby:%s
-------end------
"""
print(msg%(name,int(age),sex,hobby))
%s 字符串 : %s可以填充字符串也可以填充数字
%d|%i 整型 : 必须填充数字
%% 转义: 变成普通的%
按照位置顺序一一对应(占了几个位置就填几个位置)
msg = "山哥,目前的学习进度为%s%%"
print(msg%(2))
msg = "my name is %s I'm %s years old" # 单双引号配合使用
print(msg%(input("name:"),input("age:")))
msg = f"my name % {{}} is {input('name:')} I'm {input('age:')} years old"
print(msg)
f-strings python3.6版本及以上才能使用
3.运算符
print(1)
3>2 and 4>2 or True and not False and True
3>2 and 4>2 or True and True and True
True and True or True and True and True
True or True
True和False进行逻辑运算时:
# and:
# 一真一假,就是假
# 同真为真,同假为假
# or:
# 一真一假,就是真
# 同真为真,同假为假
面试时 : 笔试题
# and数字进行逻辑运算时:
# 数字不为 0 时和不为 False
# and运算选择and后边的内容
# and运算都为假时选择and前的内容
# and 运算一真一假选择假
# or 数字进行逻辑运算时:
# 数字不为 0 时和不为 False
# or运算选择or前边的内容
# or运算都为假时选择or后边的内容
# or 运算一真一假选择真
# print(1 and 3)
# print(0 and 8)
# print(9 and 4)
# print(False and 5)
# print(1 and 5)
# print(3 and 1)
# print(0 and False)
# print(1 or 5)
# print(5 or 9)
# print(5 or 0)
# print(False or 0)
# print(False or True)
# print(9 or True)
if user == "aelx" and pwd == "alex123" and or and:
5.成员运算法
# in(在) not in(不在)
# name = "alex"
# msg = input(">>>")
# if name in msg:
# print(111)
# else:
# print(222)
4.编码初始
编码集(密码本)
ascii 不支持中文
a 一个字符占用8位
gbk(包含ascii)国标
a 一个字符占用8位(1字节)
中文 一个字符占16位(2字节)
unicode
英文 4个字节 32位
中文 4个字节 32位
utf-8 (最流行的编码集)
英文 1字节 8位
欧洲 2字节 16位
亚洲 3字节 24位
单位转换:
# 1字节 = 8位 *****
# 1Bytes = 8bit *****
# 1024bytes = 1KB
# 1024KB = 1MB
# 1024MB = 1GB
# 1024GB = 1TB
# 1024TB = 1PB