1. 程式人生 > >我的“便(bian)宜"Python網課第二課

我的“便(bian)宜"Python網課第二課

finall ise 普通人 ase 使用 第一個字符 class fin 中文

二、變量、字符編碼

  Variables are used to store infomation to be referenced and manpulated in a computer program. They also provide a way of labeling data with a descriptive name, so our program can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold infomation. Their sole puepose is to label and store data in memory. This dat can then be used thougout your program.

  變量的定義規則:

*變量名只能是字母、數字或下劃線的任意組合

*變量名的第一個字符不能是數字

*以下關鍵字不能聲明為變量名:

  [‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

  python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill)

  ASCII(American Standard Code for Information Interchange,美國標準信息交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言,其最多只能用 8 位來表示(一個字節),即:2**8 = 256-1,所以,ASCII碼最多只能表示 255 個符號。

  關於中文

  為了處理漢字,程序員設計了用於簡體中文的GB2312和用於繁體中文的big5。

GB2312(1980年)一共收錄了7445個字符,包括6763個漢字和682個其它符號。漢字區的內碼範圍高字節從B0-F7,低字節從A1-FE,占用的碼位是72*94=6768。其中有5個空位是D7FA-D7FE。

GB2312 支持的漢字太少。1995年的漢字擴展規範GBK1.0收錄了21886個符號,它分為漢字區和圖形符號區。漢字區包括21003個字符。2000年的 GB18030是取代GBK1.0的正式國家標準。該標準收錄了27484個漢字,同時還收錄了藏文、蒙文、維吾爾文等主要的少數民族文字。現在的PC平臺必須支持GB18030,對嵌入式產品暫不作要求。所以手機、MP3一般只支持GB2312。

  從ASCII、GB2312、GBK 到GB18030,這些編碼方法是向下兼容的,即同一個字符在這些方案中總是有相同的編碼,後面的標準支持更多的字符。在這些編碼中,英文和中文可以統一地處理。區分中文編碼的方法是高字節的最高位不為0。按照程序員的稱呼,GB2312、GBK到GB18030都屬於雙字節字符集 (DBCS)。有的中文Windows的缺省內碼還是GBK,可以通過GB18030升級包升級到GB18030。不過GB18030相對GBK增加的字符,普通人是很難用到的,通常我們還是用GBK指代中文Windows內碼。

  Unicode(統一碼、萬國碼、單一碼)是一種在計算機上使用的字符編碼。Unicode 是為了解決傳統的字符編碼方案的局限而產生的,它為每種語言中的每個字符設定了統一並且唯一的二進制編碼,規定雖有的字符和符號最少由 16 位來表示(2個字節),即:2 **16 = 65536,

註:此處說的的是最少2個字節,可能更多

  UTF-8,是對Unicode編碼的壓縮和優化,他不再使用最少使用2個字節,而是將所有的字符和符號進行分類:ascii碼中的內容用1個字節保存、歐洲的字符用2個字節保存,東亞的字符用3個字節保存...

import getpass

_username = "JingyuWang"
_password = "wxl952780"

username = input("username:")
password = getpass.getpass("password:")

if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
age_of_oldboy = 66
count = 0
while count<3:
Guess_age = int(input("Please input age you guess:"))
if age_of_oldboy == Guess_age:
print("Yes,you gei it")
elif age_of_oldboy >= Guess_age:
print("Think older")
else :
print("Think younger")
count +=1
if count == 3:
continue_confirm = input("Do you want to keeping guessing..?N:Quit;Y:Keep")
if (continue_confirm != ‘N‘):
count = 0
else:
print("Quit!")
else:
print("You have tried too many timers...fuck off")
else:
print("Invalid username or password")

我的“便(bian)宜"Python網課第二課