亲宝软件园·资讯

展开

Python基础知识(day1)

RaytheonThor 人气:3
## day1 ### 1.编码 - ASCII码 1字节8位 2^8 = 256 位 - 万国码 unicode 4字节32位 #浪费空间 - UTF-8 对unicode进行压缩 ### 2.注释 - 单行注释 ```python score = input('请输入成绩: ') #整数化成绩 score_int = int(score) #判断等级 ``` - 多行注释 ```python """ 1.循环打印"人生苦短,我用python" """ ``` ```python ``` ### 3.输入、输出 - 输入 ``` #默认input字符型 score = input ("提示输入:") #类型转换 score = int(score) ``` - 输出 ```python #print输出 print('Hello World!') ``` ### 4.变量 - 变量无类型,数据有类型 - 缩进为灵魂 - 变量命名驼峰式或下划线式,推荐下划线式 ### 5.if条件语句 - if-else,缩进为灵魂 - 多个判断条件可用if-elif-else ```python 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模式,打断点,每一步执行 ![image-20200308215231941](C:\Users\10676\AppData\Roaming\Typora\typora-user-images\image-20200308215231941.png) - 循环结束条件,否则进入死循环 ```python """ 1.循环打印"人生苦短,我用python" """ #while 1 > 0 and 2 > 1: #print ("人生苦短,我用python") count = 1 while count < 10: print(count) count = count + 1 print(count) ``` - 跳出本次循环 ```python #方法一,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跳出当前循环 ```python 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跳出本次循环 ```python #continue 跳出本次循环 count = 1 while count <= 10: if count == 7: count = count + 1 continue print(count) count = count + 1 print('The End!') ``` - while-else(极少使用) ```python #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+?

加载全部内容

相关教程
猜你喜欢
用户评论