Python基础知识(day1)
day1
1.编码
-
ASCII码 1字节8位 2^8 = 256 位
-
万国码 unicode 4字节32位 #浪费空间
-
UTF-8 对unicode进行压缩
2.注释
-
单行注释
score = input('请输入成绩: ') #整数化成绩 score_int = int(score) #判断等级
-
多行注释
""" 1.循环打印"人生苦短,我用python" """
3.输入、输出
-
输入
#默认input字符型 score = input ("提示输入:") #类型转换 score = int(score)
-
输出
#print输出 print('Hello World!')
4.变量
-
变量无类型,数据有类型
-
缩进为灵魂
-
变量命名驼峰式或下划线式,推荐下划线式
5.if条件语句
-
if-else,缩进为灵魂
-
多个判断条件可用if-elif-else
score = input('请输入成绩: ') #整数化成绩 score_int = int(score) #判断等级 if score_int > 90: print('你的成绩为 ', score_int, '分,等级为: '+'A') elif score_int > 80: print('你的成绩为 ', score_int, '分,等级为: '+'B') elif score_int > 70: print('你的成绩为 ', score_int, '分,等级为: '+'C') else: print('你的成绩为 ', score_int, '分,等级为: '+'D')
6.while循环
- debug模式,打断点,每一步执行
-
循环结束条件,否则进入死循环
""" 1.循环打印"人生苦短,我用python" """ #while 1 > 0 and 2 > 1: #print ("人生苦短,我用python") count = 1 while count < 10: print(count) count = count + 1 print(count)
-
跳出本次循环
#方法一,pass count = 1 while count < 10: if count == 7: pass else: print(count) count = count + 1 print(count) #方法二,if != count = 1 while count < 10: if count != 7: print(count) count = count + 1 print(count)
-
break跳出当前循环
while True: print(666) break #中止当前循环 print('结束') #通过break实现1~10 count = 1 while True: print(count) if count == 10: break count = count + 1 print('The End!')
-
continue跳出本次循环
#continue 跳出本次循环 count = 1 while count <= 10: if count == 7: count = count + 1 continue print(count) count = count + 1 print('The End!')
-
while-else(极少使用)
#while-else count = 1 while count <= 10: print(count) if count == 10: break count = count + 1 else: print('不再满足while条件执行或条件为false!') print('The End!')
7.其他
- 快速注释 ctrl+?