python基础(1)
python3+文件路径 回车
1、变量:将一些运算的中间结果暂存到内存中,以便后面代码使用
2、变量规范:必须由数字,字母下划线任意组合,且不能数字开头;不能python中的关键字;变量具有可描述性;不能是中文
3、常量:字母全是大写
4、注释:方便自己 方便他人
单行注释:# 多行注释:三个''' . 三个"""
5、基础数据类型初始
数字:int 。 + - * / ** % . 字符串转化为整数 字符串必须是数字 int(str),数字转换为字符串:str(int)
type判断数据类型
字符串:string 。 python中用引号引起了的都是字符串,字符串可以相加,可相乘 str*int
bool:布尔值。True False 首字母大写
6、用户交互 input:等待输入;将你输入的内容赋值给前面的变量;input出来的数据类型全是str
7、if 条件:
一个tab键 结果
第一种:
print(111) if True: print(666) print(777) 和if无关系 都会打印
结果:111 666 777
第二种: if 4>3: print(666) else: print(777) 二选一
多选: num=input('请输入您猜的数字:') if num=='1': print('ooo') elif num=='2': print('kkkk') elif num=='3': print('lllll') else: print('错了') 只要满足一个就不再往下执行
嵌套if: name=input('请输入名字:') age=input('请输入年龄:') if name == '小二' if age=='18' print(666) else:
print(333) else:
print('错了')
8、while 条件:
循环体
无限循环
终止循环:改变条件,使其不成立 break
while True: print('我们不一样') print('在人间')
while True: print('我们不一样') print('在人间') print('痒') print('222')
1-100输出
1 count=1 2 flag=True 3 while flag: 4 print(count) 5 count=count+1 6 if count>100: 7 flag=False
1 count=1
2 while count<=100:
3 print(count)
4 count=count+1
1-100的和:
1 count=1 2 num=0 3 while count<=100: 4 print(count) 5 num=num+count 6 count=count+1 7 print(num)
break用法:
1 count=1 2 while True: 3 print(count) 4 count=count+1 5 if count>100: 6 break
continue用法:
1 count=1
2 while count<=100:
3 print(count) 4 continue 5 count=count+1 6 print(num)