標籤:記憶體 var 中文 input 位元組 開發 and 選擇 think
第一周主要介紹了python的發展史,以及當前的一些應用情況。
變數
python的編寫過程中會用到許多資料,那為了方便操作,需要把這些資料分別用一個簡單的名字代表,方便在接下來的程式中引用,變數就是代表某個資料(值)的名稱。
1、變數名可以包括字母、數字、底線,但是數字不能做為開頭
2、系統關鍵字不能做變數名使用
var = ‘hello world!‘
print(var)
執行後
hello world!
字元編碼
字元編碼從ASCII,到GB2312、GBK、GBK18030,到現在的萬國碼Unicode,因為Unicode的英文佔用記憶體太大,所以其中的UTF-8最佳化後,一個中文字元佔3個位元組,一個英文字元佔1個位元組。
if else
1 _username = ‘doulen‘ 2 _password = ‘home1234‘ #聲明使用者名稱和密碼的變數 3 4 username = input(‘username:‘) 5 password = input(‘password:‘) 6 7 if username == _username and password == _password: 8 print(‘welcome {name}, login...‘ .format(name=username)) #只要輸入的使用者名稱密碼和預先存的一致 那就輸出這段話 9 else:10 print(‘username or password is wrong‘) #否則 輸出這段話
while迴圈
1 true_age = 24 2 count = 0 3 while count < 3: 4 g_age = int(input(‘age:‘)) 5 chances = 2-count 6 7 if g_age == true_age: 8 print(‘you got it!‘) 9 break10 elif g_age > true_age:11 print(‘oops! think smaller, you have %s chances left‘ %(chances))12 else:13 print(‘oops! think bigger, you have %s chances left‘ %(chances))14 count +=115 else:16 print(‘you have guessed too many times‘)
讓使用者自行選擇繼續還是終止
1 true_age = 24 2 count = 0 3 while count < 3: 4 g_age = int(input(‘age:‘)) 5 chances = 2-count 6 7 if g_age == true_age: 8 print(‘you got it!‘) 9 break10 elif g_age > true_age:11 print(‘oops! think smaller, you have %s chances left‘ %(chances))12 else:13 print(‘oops! think bigger, you have %s chances left‘ %(chances))14 count +=115 if count == 3:16 countine_comfirm = input(‘do you want countine?y/n:‘)17 if countine_comfirm != ‘n‘:18 count = 0
python全棧開發—Week1