python基礎第一課

一  python第一個程序python

print('hello world!')   # python3.x
 
print 'hello world!'   # python2.x

 

二  變量linux

2.1  變量名稱規則程序員

  • 變量名只能是 字母、數字或下劃線的任意組合
  • 變量名的第一個字符不能是數字
  • 如下關鍵字不能聲明爲變量名

['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']python3.x

 

2.2 案例oop

_name = 'sam'   # 正確
1name = 'yang'  #  錯誤
name_it_1 = 'yuyu'  # 正確

 

2.3  代碼案例優化

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao


# print("Hello world")

name = "Sam Gao"   #  name -----> "Sam Gao" 
name2 = name   #  name -----> "Sam Gao"   and   name2 -----> "Sam Gao" 

print('My name is', name, name2)   # 輸出: My name is Sam Gao Sam Gao

name = 'pao che ge'  #   name -----> "pao che ge" and name2 -----> "Sam Gao" 

print(name, name2)   # 輸出:  pao che ge Sam Gao

 

三  字符編碼編碼

3.1  asciispa

ASCII(American Standard Code for Information Interchange,美國標準信息交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其餘西歐語言,其最多隻能用 8 位來表示(一個字節),即:2**8 = 256-1,因此,ASCII碼最多隻能表示 255 個符號。注:python2.x 默認使用ascii編碼3d

 

3.2  中文字符集code

GB2312

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

GBK

  GB2312 支持的漢字太少。1995年的漢字擴展規範GBK1.0收錄了21886個符號,它分爲漢字區和圖形符號區。漢字區包括21003個字符。

GB18030

  2000年的 GB18030是取代GBK1.0的正式國家標準。該標準收錄了27484個漢字,同時還收錄了藏文、蒙文、維吾爾文等主要的少數民族文字。如今的PC平臺必須支持GB18030,對嵌入式產品暫不做要求。因此手機、MP3通常只支持GB2312。

從ASCII、GB23十二、GBK 到GB18030,這些編碼方法是向下兼容的,即同一個字符在這些方案中老是有相同的編碼,後面的標準支持更多的字符。在這些編碼中,英文和中文能夠統一地處理。區分中文編碼的方法是高字節的最高位不爲0。

 

按照程序員的稱呼,GB23十二、GBK到GB18030都屬於雙字節字符集 (DBCS)。繁體中文,big5。

 

GB2312 GBK GB18030 佔兩個字節,而且可以向下兼容    GB18030 --> GBK --> GB2312 --> ASCILL

 

3.3  unicode

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

注:python3.x 默認使用unicode編碼

 

3.4  utf-8

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

 

3.5  案例

#!/usr/bin/env python
# encoding: utf-8    # 告訴解釋器用utf-8編碼
# Author: Sam Gao

name = '高紹陽'      # python2.x

 print name
#!/usr/bin/env python3

# Author: Sam Gao

name = '高紹陽'  # 默認用unicode編碼

print(name)

 

四  input 輸入

4.1 簡單輸入

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

import getpass

_username = 'sam'
_password = '123456'

username = input('username:')
passwd = getpass.getpass('password:')   # 在pycharm裏面用不了,只能在交互式或者 在linux裏以 python3 passwd.py 執行,做用:不會顯示輸入內容

if _username == username and _password == passwd:
    print('welcome user {name} login'.format(name=username))
else:
    print('Invaild username or password')

4.2 案例 猜年齡遊戲

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28

while True:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

 

五  while for循環 和 if判斷

5.1 while True:

# 參考 4.2 案例 猜年齡遊戲    當不知道循環到哪裏的時候,就使用

 

5.2 案例

知識點:1.  有while......else.....的形式; 2. break和continus的用法;if  和  if.....elif...else     和  if...else

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28
count = 0


'''
猜三次,若是猜正確,則退出
'''

# while True:
#     if count == 3:
#         break
#
#     guess_age = int(input('guess age:'))
#
#     if age_of_sam == guess_age:
#         print('yes, you got it')
#         break
#
#     elif age_of_sam > guess_age:
#         print('think big')
#     else:
#         print('think small')
#
#     count += 1
#####################################################
# while count < 3:
#     guess_age = int(input('guess age:'))
#
#     if age_of_sam == guess_age:
#         print('yes, you got it')
#         break
#
#     elif age_of_sam > guess_age:
#         print('think big')
#     else:
#         print('think small')
#
#     count += 1
#     if count == 3:
#         print('you guess too many times, fuck you !')

######################################################
#  或者

while count < 3:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

    count += 1
else:
    print('you guess too many times, fuck you !')

5.3  使用for只玩3次

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28

for i in range(3):
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

else:
    print('you guess too many times, fuck you !')


for i in range(1, 12, 3):   #   3表明步長
    print('loop:', i)

 

5.4  任性玩5.2的遊戲

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28
count = 0

while count < 3:  #   while True:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break                            # break  結束整個循環,不結束父循環
    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

    count += 1
    if count == 3:
        while True:
            continue_play = input('Do you want to play:[yes/no]:')
            if continue_play == 'yes':
                count = 0
                break
            elif continue_play == 'no':
                break
            else:
                print('pls input valid string')
                continue             #   continus  跳出當前循環
else:
    print('you guess too many times, fuck you !')

 

 六  字符串格式化

案例

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

# username = input('usrname:')
# password = input('password:')
#
# print('username:', username)
# print('password:', password)

name = input('name:')   #  =====  python2 raw_input      python3   eval(input) =====  python2  input
age = int(input('age:'))   #  默認輸出的是字符串
job = input('job:')
salary = int(input('salary:'))

print('name:', type(name), 'age', type(age))

info = '''
----------------info of %s---------------
Name: %s
Age: %d
Job: %s
Salary: %d
''' % (name, name, age, job, salary)

# %s 字符串    %d  整數      %f   浮點數

info2 = '''
----------------info of {_name}---------------
Name: {_name}
Age: {_age}
Job: {_job}
Salary: {_salary}
'''.format(_name=name, _age=age, _job=job, _salary=salary)

info3 = '''
----------------info of {0}---------------
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''.format(name, age, job, salary)

print(info)

print(info2)

print(info3)
相關文章
相關標籤/搜索