第二天学习内容
---恢复内容开始---
一、作业:
1、使用while循环输入1 2 3 4 5 6 7 9 10
i = 0 while i < 10: i = i + 1 if i == 7: continue print(i)
i = 0 while i < 10: i = i + 1 if i == 7: continue else: print(i)
i = 0 while i < 10: i = i + 1 if i == 7: pass else: print(i)
2、打印1-100的和
i = 1 sum = 0 while i < 101: sum = sum + i i = i + 1 print(sum)
i = 0 sum = 0 while i < 100: i = i + 1 sum = sum + i print(sum)
3、输出1-100中所有的奇数
for a in range(1, 100, 2): print(a)
i = 1 while i < 101: print(i) i += 2
i = 1 while i < 101: if i% 2 == 1: print(i) i += 1
4、求1-2+3-4+……+99所有数的和
i = 1 sum = 0 while i < 100: if i % 2 == 0: sum = sum - i else: sum = sum + i i += 1 print(sum)
5、用户登录,三次机会重试
i = 0 while i < 3: username = input('请输入你的账号:') passwors = input('请输入你的密码:') if username == "zhanghao" and passwors == str(123): print('登陆成功') else: print('登录失败') i +=1
二、格式化输出
1、两种方式:
①、占位符方式:% s d 占位符 s:替换字符串;d:替换数字
name = input('输入你的名字:') age = input('输入你的年龄:') height = input('输入你的身高:') message = "我叫%s ,今年%s ,身高%s.学习进度为3%%' %(name, age, height)" print(message)
②、format()方式:
s = '我叫{},今年{},爱好{},再说一遍我叫{}'.format('老王', '18', '游戏', '老王') print(s) s = '我叫{0},今年{1},爱好{2},再说一遍我叫{0}'.format('老王', '18', '游戏') print(s) s = '我叫{name},今年{age},爱好{hobby},再说一遍我叫{name}'.format(name ='老王', hobby='游戏', age=18) print(s)
三、初识编码:
四、运算符:
①、<, >, >=, <=, ==, !=
②、逻辑运算:not and or (优先级:()> not > and > or) or:有一个为真就为真
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2 ) # T or T or F # T or F # T
③.Ⅰ x or y: x、y为非零的数,就返回x;x为零,就返回y,返回非零的数
print(1 or 3) # --> 1 print(3 or 2) # -->3 print(0 or 5) # -->5 print(0 or 8) # -->8 print(5 or 0) # -->5
③.Ⅱ x or y or z or f :第一个数为非零时,返回第一个数;第一个数为零时,返回第二个数
print(3 or 9 or 2 or 4 or 1) # -->3 print(2 or 3 or 5 or 0) # --> 2 print(5 or 2 or 8 or 0 or 9) # -->5
print(0 or 3 or 2 or 1) # -->3 print(0 or 2 or 7 or 0) # -->2
④.Ⅰ x and y: x、y为非零时,返回y;x、y其中有一个为0时,返回非零那个数
print(1 and 3) # -->3 print(3 and 5) # -->5 print(0 and 4) # -->0 print(4 and 0) # -->0
④.Ⅱ x and y and z and f : 当第一个数为非零时,返回最后一个数;第一个数为0时,返回0
print(3 and 2 and 4 and 5 and 6) # -->6 print(2 and 8 and 9 and 3 and 0) # -->0 print(0 and 2 and 0 and 9 and 4) # -->0 print(0 and 4 and 8 and 3 and 2) # -->0
面试考题:
print(0 or 4 and 3 or 2) # 0 or 3 or 2 # 3 or 2 # 3
⑤、int -->bool :非零的数转换为bool就是True;零就是False
print(bool(2)) # True print(bool(-1)) # True print(bool(0)) # False
⑥、bool -->int: True就是1;False就是0
print(int(True)) # 1 print(int(False)) #0
---恢复内容结束---