Python開發入門14天集訓營-第一章,python集訓營
python第一章python變數
變數的作用
存資料 被程式調用和操作
標記資料
聲明變數
name = “Ydh” 變數名 = 變數值
變數定義規範:
變數名只能是 字母、數字或底線的任意組合
變數名的第一個字元不能是數字
一下關鍵字不能聲明為變數名【python的文法關鍵詞、python內建變數】
變數命名習慣
1、駝峰體(每個首字母大寫)
AgeOfOldboy = 56
NumberOfStudents = 80
2、底線(官方推薦)
age_of_oldboy = 56
number_of_students = 80
定義變數的Low方式
變數名為中文、拼音
變數名過長
變數名不達意
如:
你的年齡 = 20 age_of_oldboy = 20
ni_denianling = 20
the_ni_de_mingzi = 20 your_name = 20
name1 = 1
name2 = "北京"
調用變數
print(age_of_oldboy)
修改變數值
age_of_oldboy = 30
常量
永遠不變的量 例如π=3.14.....
python裡邊沒有一個專門的文法代表常亮,程式員約定:常量變數名全部為大寫
如:AGE_OF_OLDBOY = 20
讀取使用者輸入
input()
name = input("input name:")
注釋
作用:
1、注釋掉不用的代碼
2、描述程式碼片段的意思
代碼注釋原則:
1、不用全部加註釋
2、只需要在自己覺得重要或不好理解的部分加註釋即可
3、注釋可以用中文或英文,但絕對不要用拼音
python資料類型
基本類型
數字
整數int
長整型long
浮點型float
負數
字元集
文本str
位元組bytes
布爾
True/Flase
資料集
列表list
元組tuple
字典dict
有序字典
無序字典
集合set
有序集合
無序集合
int(整型)
在32位機器上,整數的位元為32位,取值範圍為-23~231-1
在64位系統上,整數的位元為64位,取值範圍為-263~263-1
long(長整型)
python的長整數沒有指定位寬。即python沒有限制長整數數值的大小,但實際上由於機器的記憶體有限,我們使用的長整數數值不可能無限大。
浮點數float
簡單的理解就是小數
字串
在python中,加了引號的字元都被認為是字串(包括單引號、雙引號、三引號)
單雙引號沒有任何區別,只有下面的情況下,需要考慮單雙引號的配合
msg = "I'm 20 years old!"
多引號作用是多行字串必須用多引號
msg = ‘’‘我愛北京天安門天安門上太陽升’‘’print(msg)
View Code
字串拼接
字串可以相加和相乘,字串只能和字串進行拼接
name + age
name * 10 把name的值列印10次
布爾類型
真/假 True/False
用於邏輯判斷,是正確的為True,還是錯誤的為False!
電腦為什麼要描述這種條件呢?
因為可以根據條件結果來做不通的事情,如:
if a > b : print ("this is a bigger number than b")else: print("this is a smaller number than b")
View Code
格式化輸出
%s 代表 字串
%d代表 數字
%f代表 浮點數 float
運算子
算數運算
“+ - * / 、取餘數:% 、冪:** x的y次冪、取整數//”
比較運算
"等於:== 、不等於:!= 、不等於:<>、> 、<、>= 、<="
邏輯運算
"與and、或or、非not"
賦值運算
“等於=、加等於+=、減等於-=、乘等於*=、除等於/=、模數等於%=、等冪於**=、取整除等於//=”
成員運算
身份運算
位元運算
流程式控制制
單分支
if條件: 滿足條件後要執行的代碼
雙分支
if條件: 滿足條件後要執行的代碼else: if條件不滿足就走這裡的代碼
多分支
if條件: 滿足條件後要執行的代碼elif: 如果上邊的條件不滿足就走這個elif: 如果上邊的條件不滿足就走這個elif: 如果上邊的條件不滿足就走這個
while迴圈
文法:
while 條件: 條件成立,執行代碼...
pass #就是什麼都不做
死迴圈dead loop
count = 0while True: print ("count:", count) count += 1
迴圈終止語句
break 用於完全結束一個迴圈,跳出迴圈體 執行後面的語句
continue 跳出本次迴圈,進行下次迴圈
練習:猜年齡遊戲
練習一、輸入姓名、性別,判斷如果是女生,列印我喜歡女生,否則,列印一起來搞基!
name = input("input name:")sex = input("input sex:")if sex == "女" : print("我喜歡女生!")else: print("一起來搞基!")'''測試結果1:input name:joininput sex:男一起來搞基!-----------測試結果2:input name:小薇input sex:女我喜歡女生!'''
View Code
練習二、輸入姓名、性別、年齡,判斷如果是女生且年齡小於28歲,列印我喜歡女生,否則,列印姐弟戀也很好!
name = input("input name:")sex = input("input sex:")age = int(input("input age:"))if sex == "女" : if age < 28: print("我喜歡女生") else: print("姐弟戀也很好!")'''測試結果1:input name:小薇input sex:女input age:25我喜歡女生----------------測試結果2:input name:小薇input sex:女input age:29姐弟戀也很好!測試結果3:input name:johninput sex:男input age:25輸出為空白
View Code
練習三、輸入姓名、性別、年齡,判斷如果是女生且年齡小於28歲,列印我喜歡女生,否則,列印姐弟戀也很好!如果是男生,列印一起來搞基!
name = input("input name:")sex = input("input sex:")age = int(input("input age:"))if sex == "女" : if age < 28: print("我喜歡女生") else: print("姐弟戀也很好!")elif sex == "男": print("一起來搞基!")else: print("sex 輸入不正確!")
本節練習題
1、流程式控制制;
匹配成績的小程式,成績有ABCDE 5個等級,與分數的對應關係如下:
A 90-100
B 80-89
C 60-79
D 40-59
E 0-39
while True: score = float(input("input your score:")) if score >100 or score < 0 : print("沒有這個成績") elif score >= 90: print("A") elif score >= 80 and score <=89: print("B") elif score >= 60 and score <= 79: print("C") elif score >= 40 and score <=59: print("D") elif score >= 0 and score <=39: print("E")
View Code
2、猜年齡練習
練習一
最佳化猜年齡遊戲,允許使用者最多猜3次,中間猜對了,直接跳出迴圈
name = 25count = 1while True: user_input = int(input("猜年齡,請輸入年齡:")) if count > 3: break elif user_input == name: print("恭喜猜對了!") break count += 1
View Code
練習二
最佳化猜年齡遊戲,允許使用者最多猜3次,猜了3次後,再問是否還繼續玩,
如果使用者選y,就再允許3次,依次迴圈,如果使用者輸入n ,就退出程式
name = 25count = 1while True: if count > 3: while True: judge = input("是否要繼續玩遊戲,y/n:") if judge == "y": count = 0 break elif judge == "n": print("謝謝光臨!") exit() else: print("輸入不正確!") else: user_input = int(input("猜年齡,請輸入年齡:")) if user_input == name : print("恭喜猜對了!") break elif user_input < 25: print("think bigger!!") elif user_input > 25: print("think smaller!!") count += 1
View Code
3、while迴圈練習
練習一、迴圈1-100 個數
count = 1while count <= 100: print("loop:",count) count += 1
View Code
練習二、迴圈1-100 裡邊的偶數:
count = 1while count <= 100: if count%2==0: #偶數能夠整除2 ,相反就是基數 print("loop:",count) count += 1
View Code
練習三、迴圈列印1-100,第50次不列印值,第60-80次,列印對應值的平方
count = 1while count <= 100: if count == 50: pass if count >=60 and count <=80: print("loop count 的平方是:",count * count) else: print("loop:",count) count += 1
View Code
練習四、迴圈終止語句
count = 1while count <= 100: print("loop:",count) if count == 5: break count += 1
View Codewhile else玩法
while 條件匹配: 條件匹配成功,執行此處代碼else: 條件不匹配,執行此處代碼
例子:
count = 1while count <= 5: #當count<=5的時候,條件匹配,執行下邊的代碼 print("loop:",count) count += 1else: print("迴圈終止了!") #當count=6的時候不匹配while條件,執行此處代碼
輸出:
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
迴圈終止了!
本章作業:
基礎需求:
讓使用者輸入使用者名稱密碼
認證成功後顯示歡迎資訊
輸錯三次後退出程式
升級需求:
可以支援多個使用者登入(提示:通過列表存多個賬戶資訊)
使用者3次認證失敗後,退出程式,
再次啟動程式嘗試登入時,還是鎖定狀態(提示:需把使用者鎖定的狀態存到檔案裡)