python-day1
exit()退出python界面
6,变量。
变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
1,必须由数字,字母,下划线任意组合,且不能数字开头。
2,不能是python中的关键字。
['and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'exec',
'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
'raise', 'return', 'try', 'while', 'with', 'yield']
3,变量具有可描述性。
4,不能是中文。
7,常量。
一直不变的量。
一般用大写字母表示常量。
8,注释。
单行注释:#
多行注释:'''用三个单引号,或三个双引号注释‘’‘,也可用于字符串附值
#-*- encoding:utf-8 -*- 这个用于低版本python输出显示中文
9,用户交互
a = input('请输入名字:')
input出来的数据类型是str
10,基础数据类型初始。
数字:int
字符串:str,python当中凡是用引号引起来的都是字符串。
运算符: + - * / %取余 and or
** 多少次幂
bool:布尔值,True,False
print('100',type('100')) #默认后面会输出一个回车
11,if。
if 条件:
结果 #缩进部分属于生效部分
else:
结果2
if 条件(如a == 1):
结果
elif 条件2: #可以多个
结果2
else:
12,while。
while 条件:
循环体 #缩进生效
出循环:1,条件改变;2,break跳到循环体外;3,continue结束本次循环,回到循环头部
pass跳过本行语句
作业:
1、使用while循环输入 1 2 3 4 5 6 8 9 10
count = 0 while count < 10: count = count + 1 if count == 7: continue key_input = "请输入"+str(count)+":" a=input(key_input) while a != str(count): a=input(key_input)
2、求1-100的所有数的和
count = 1 sum = 0 while count <= 100: sum = count + sum count = count + 1 print("1-100的和:"+str(sum))
3、输出 1-100 内的所有奇数
count = 1 while count <= 100: if (count % 2) == 1: print(count) count = count + 1
4、输出 1-100 内的所有偶数
5、求1-2+3-4+5 ... 99的所有数的和
count = 1 sum = 0 check_sum = "" while count <= 100: if (count % 2) == 0: sum = sum - count check_sum = check_sum + "-" + str(count) else: sum = sum + count if count > 1: check_sum = check_sum + "+" + str(count) else: check_sum = str(count) count = count + 1 print(check_sum+"="+str(sum))
6、用户登陆(三次机会重试)
count = 1 password = "" while count <= 3: password = input("请输入密码:") if password == "honey": print("密码正确!") break else: if count < 3: print("第"+str(count)+"次密码错误!请重新输入。") else: print("第"+str(count)+"次密码错误!") count = count + 1 if count > 3: print("密码错误超过3次。结束!")