1. 1 循环
1.2练习
while True: #False #bool判断
....
while 1>2: #判断
....
flag =True #变量
while flag:
...
flag = True
while flag: #while里改变量
print('...')
flag = False
#通过某个值的更新,控制循环的次数
num = 1
while num < 5:
print('...')
num = 5
num = 1
while num < 5:
print('...')
num = num + 1
if ...:
pass
else:
....
#可以正选pass 反选!=
1.3 break中断
- break可用标志位替代
while True:
name = input("Enter your name: ").strip()
pwd = input("Enter your password: ").strip()
if name == 'wupeiqi' and pwd == '123456':
print(f'{name}登录成功')
break
else:
print('登陆失败,请重试')
print('系统结束')
1.4 continue
- 结束本次循环,开始下次循环
#从0到100 绕过7
i = 1
while True:
if i == 7:
i = i+1
continue
if i == 101:
break
print(i)
i = i+1
- continue和break都可以被替代,不过continue和break能让代码更简洁
1.5 while else
- 使用率低
#while else 循环完(当条件不再成立时)后执行else代码
i = 1
while i < 5:
print(i)
i += 1
print('666')
- 遇到break时,不再执行else代码
while True:
print(123)
break
else:
print(6)
2.1 字符串格式化
2.1.1占位符
三种格式
- %
占位符(顺序版)
name = 'alex' # #text = '%s 是一个猪猪侠' %'alex' #text = '%s 是一个猪猪侠' %name text = '%s 是一个%d的猪猪侠' %(name,180) print(text)
- 占位符(无顺序版),第二个百分号跟的大括号键值对
message = '%(name)s 是一个猪猪侠' %{'name':'alex'} print(message)
- 特殊情况,格式化遇到%号,需要加入%%
message_1 = '%s,我已经下载到90%%的进度了!' %'兄弟' print(message_1)
2.1.2 format
- format {}
是否重复利用
- 有序号(可重复利用)
#format 可重复利用,有顺序 message_2 = 'hi {0},这是我的{1},这不是你的{1}'.format('alex','东西') print(message_2)
- 无序号(默认) 不可重复利用(复用)
#format 不可重复利用,有顺序 message_3 = 'hi {},这是我的{}'.format('alex','东西') print(message_3)
- 更改序号 可重复利用
#format 可重复利用,不需要顺序 message_3 = 'hi {name},这是我的{goods}'.format(name='alex',goods='ammo') print(message_3)
- 变量继承
- %继承
text = '%s,今年已经%d岁了'
data1 = text %('alex',30)
- format继承
text = '你好{0},我是{1}'
data1 = text.format('alex',30)
2.1.3 f格式化
- python 3.6及以后
- 用大括号表示,可在内部表示表达式
name = 'alex'
#message_05 = f'hi,{name},你的年龄是{20 + 1}' 代表结果不出=
message_05 = f'hi,{name},你的年龄是{20 + 1 = }'
print(message_05)
#结果
hi,alex,你的年龄是20 + 1 =21
- 也可表达二进制,八进制,十六进制
1 1 1 1 1
16 8 4 2 1
# 进制转换
message_06 = f'你的年龄是{22:#b}'
print(message_06)
# 进制转换
message_07 = f'你的年龄是{22:#o}'
print(message_07)
# 进制转换
message_08 = f'你的年龄是{22:#x}'
print(message_08)
- 也可在括号内使用函数方法
#在括号内使用函数方法
name = 'alex'
message_09 = f'{name.upper()}喜欢大铁锤'
print(message_09)
3.1 运算符
3.1.1算数运算符
- +
- -
- x
- /
- ** 幂
- // 取整
- % 取模(取余)
3.1.2比较运算符
- ==
- <
- <> 已废除python2.0
- ≠ (! =)
- ≥
- ≤
3.1.3赋值运算
- += c=c+a
- -= c=c-a
- *= c=ca*
- /= c=c/a
- %= c=c%a
- //= c=c//a
- = c=ca**
3.1.4成员运算
- in
- not in
逻辑运算
- and
- or
- not 取反运算
flag = 1>2 #Flase
if not flag: #not False = True
print(1)
运算优先级
- 算数运算+-x/ 大于比较运算
if 3+2 > 1:
pass
- 比较运算大于逻辑运算
if 1 > 2 and 2 < 10:
pass
- 逻辑运算内部 not > and > or
if not 1 and 1 > 2 or 3 == 8:
print(1)
else:
print(2)
# not 1 = False
# 1 > 2 = False
#False and False or 3 == 8
#False or False
#False
2
面试题 算数大于比较大于逻辑
True and/or False
#1 比较运算在逻辑运算中输出True or False
v1 = name = 'alex' and pwd = '123' #v1= True and True
# 字符串在逻辑运算中,查看取决于谁(x),然后输出x的值
v2 = 'wupeiqi' and 'alex' # True and True 字符串只要非空为True
#v2取决于第二个True,所以v2输出alex
v3 = '' and 'alex' # False and True 取决于False v3 = ''
#and之中全True取最后一个值,有False取第一个False
#or之中
v4 = 1 or 8 # v4 = 1
v5 = 0 or 8 #v5 = 8
逻辑运算练习题and 看false or看true
- and > or 两两一组
- 简单
v1 = 1 or 2 # v1 = 1
v2 = -1 or 3 # v2 = -1
v3 = 0 or -1 # v3 = -1
v4 = 0 or 100 # v4 = 100
v5 = '' or 100 # v5 = 100
v6 = 'wupeiqi' or '' # v6 = 'wupeiqi'
v1 = 4 and 8 # v1 = 8
v2 = 0 and 6 # v2 = 0
v3 = -1 and 88 # v3 = 88
v4 = '' and 7 # v4 = ''
v5 = 'wupeiqi' and '' # v5 = ''
v6 = '' and 0 # v6 = ''
v7 = 0 and '中国' # v7 = 0
- 复合
#v1 = 0 or 4 and 3 or 7 or 9 and 6
v1 = 0 or 3 or 7 or 6
print(v1)
#v2 = 8 or 3 and 4 or 2 and 0 or 9 and 7
v2 =8 or 4 or 0 or 7
print(v2)
# 0 or 2 and 3 and 4 or 6 and 0 or 3
v3 = 0 or 4 or 0 or 3
print(v3)
- 包含not
v4 = not 8 or 3 and 4 or 2
# not 8(not 后面只能跟True?/False)
#v4 = False or 4 or 2
v4 = 4
#猜数字
i = 0
while i < 3:
i = i + 1
guess_num =input('please enter your guess number: ')
if guess_num==73:
print('你猜对了')
break
else:
print('你猜错了,剩下{}次机会'.format(3-i))
if i==3: #限定失败,去掉这行可以无条件询问
re_choice = input('是否继续Y/N')
if re_choice=='Y':
i=0
elif re_choice=='N':
break
i=0
while i<3:
猜数字的代码
通过y/n判断是否继续猜,通过格式化i来操作循环
while循环代码
#输入三次猜 正反循环
#第一种方法
i = 0:
while i <3:
i+=1
#第二种方法
i = 3
while i > 0:
i-=1
where嵌套where,其中用exit结束整个循环
while True:
i = 3
while i > 0:
i-=1
guess_num = int(input(f'your like number: ').strip())
if guess_num == 73:
print('猜对了')
break
else:
print('猜错了,还剩{}次机会'.format(i))
#必须输入正确标志判断输入
while True:
re_choice = input('请输入是否继续Y/N').strip()
if re_choice == 'Y' or re_choice == 'y':
print('开始下一次循环')
break
elif re_choice == 'N' or re_choice == 'n':
print('结束循环')
exit()
else:
print('输入错误,正在重试')
where True:
#代码A
where 条件:
pass
#代码B判断A是否继续循环
where True:
#如果继续
if 条件:
break #结束当前循环
elif 条件:
exit() #直接退出全部代码
else:
print("输入错误") #重新执行代码