python特征
python2 与 python3 区别
2的源码不标准, 重复代码多
3同意, 标准, 去重
---------------
编译型: 一次性执行所有程序编译成二进制文件, 开发效率低, 运行速度快, 不能跨平台.
解释型: 一行一行执行, 开发效率高, 可以快平台, 运行速度较慢.
a = '''
多行注释
可以赋值
给变量
'''
while else
当while被 break 打断, 不执行else
练习题 Day 1
1、使用while循环输入 1 2 3 4 5 6 8 9 10
num = 0 while num <= 9: num += 1 if num > 6 and num < 8: continue print(num)
2、求1-100的所有数的和
num = 1 sum = 0 while num < 101: sum += num num += 1 print(sum)
3、输出 1-100 内的所有奇数
odd = 0 while odd < 100: odd += 1 if odd % 2 ==1: print(odd)
1 a = 0 2 3 while a < 100: 4 a += 1 5 if a % 2: 6 print(a) 7 8 ------------- 9 10 a = 1 11 12 while a < 101: 13 print(a) 14 a += 2
4、输出 1-100 内的所有偶数
odd = tuple(range(1,100)) for i in odd: if i % 2 == 1: print(i)
5、求1-2+3-4+5 ... 1000的所有数的和
1 odd = tuple(range(1,1000)) 2 sum = 0 3 4 for i in odd: 5 if i % 2 != 0: 6 sum -= i 7 else: 8 sum += i 9 10 11 print(sum)
View Code
6、用户登陆(三次机会重试)
error = 0 left = 3 - error NAME = 'XIONG' PASSWORD = 'qq112233' while left > 0: name = input("Please input your sign in name: ") password = input("Please input your password: ") if name == NAME and password == PASSWORD: print("YOUR NAME IS: " + name) print("YOUR PASSWORD IS: " + password) print('Congratulations,you are correct') break else: error += 1 if error == 3: print('Sorry, too many times error') break print("YOUR NAME IS: " + name) print("YOUR PASSWORD IS: " + password) print('Sorry, you are wrong. Please try again') print(str(left - error) + 'time(s) left')