1. 程式人生 > >python 筆記 week1

python 筆記 week1

繼續 世界 == 必須 正常 job continue password b+

windows要加環境變量。
linux若升級版本不一致,
#!/usr/bin/env python 調用環境變量中的python
#!/usr/bin/python 調用系統中默認的python

變量定義規則:
1.變量名只能為字母、數字或下劃線的任意組合
2.變量名的第一個字符不能是數字
3.有部分系統已經使用的關鍵字不能聲明為變量名:"and" "as" "break" "def" "with" etc.
4.變量名盡量達意,以_或大寫字母表達(GF_of_sb 或 GFOfSb),常量(pai 3.1415926就是一個常量)通常以大寫字母來命名。


===============================================================================
字符編碼:

二進制到數字的轉換:x位0與1 可以表示的最大數字為2**x -1
字符編碼
ASSCII(美國標準信息交換代碼):將數字與字符相聯系,從而二進制表示出數字轉化為字符。其最多只能用8byte位來表示一個字節。即2**8-1=255,故其最多只能表示255個字符。
其中0-127先使用了,後邊預留了128-255,1980年中國取了一個數字範圍作為中文的索引,引入GB2312,共7000多個漢字加字符,1995年擴展到GBK1.0。2000年GBK18030收錄了27484個漢字,現在的pc平臺必須支持GBK18030,對嵌入式產品暫不做要求,故手機、MP3一般只支持GB2312。

ASSCII ---> GB2312 ---> GBK ---> GBK18030都是向下兼容的。
之後由於多個國家都進行重編,字符混亂。

Unicode(統一碼),為每種語言每個字符設置了統一並唯一的二進制編碼 。每個字符占兩個字節,即16byte位。缺點:所占用的空間大。

utf-8在Unicode基礎上做了改進,存英文字符占用1個字節,存其他語言占用兩個字節。即 00000000 10001110 有一個字節的8位都是0,就省略。
-----------------------------------------------------
python3字符編碼默認utf-8
python2字符默認ASCII,固要想在py2中使用中文需要先聲明字符集為utf-8,即 # -*- coding:utf-8 -*-
eg: 2,3不同點

name = "你好,世界!"
print (name)

註釋:
1.單行註釋 #
2.多行註釋‘‘‘ 多行 ‘‘‘ 三個單/雙引號
3.打印多行 ‘‘‘ 多行 ‘‘‘ 三個單/雙引號
python中單/雙引號用法完全一致,唯一要註意單雙引號互套的情況 "i‘m a girl"
eg:
name = "你好,世界!"
‘‘‘
print (name)
print (name)
print (name)
‘‘‘
msg = ‘‘‘
print (name)
print (name)
‘‘‘
#print (name)
print (msg)
print (name)

執行結果:

print (name)
print (name)

你好,世界!

腳本等待用戶輸入執行,如同shell的read -p:
username = input(‘usrname:‘)
print (username)

格式化輸出:字符串拼接 4種方法‘‘‘str‘‘‘ 與%s
註:%s 代表此處接受字符串
%d 代表此處接受整數
%f 代表此處接受浮點數
要註意對應的變量的type,在接受變量時可強制轉化
eg:當引入變量 %d 將字符串轉化為數字 age =int(input("age:"))
py2 的raw_input = py3 的input
eg:
name = input("name:")
age = input("age:")
job = input("job:")
salary = input("salary:")

#法一:
info = ‘‘‘
----------------- info of ‘‘‘ +name+ ‘‘‘--------------------
Name:‘‘‘ +name+ ‘‘‘
Age:‘‘‘ +age+ ‘‘‘
Job:‘‘‘ +job+ ‘‘‘
Salary:‘‘‘ + salary+ ‘‘‘
‘‘‘
print (info)

#法二:
info = ‘‘‘
----------------- info of %s -----------------
Name:%s
Age:%s
Job:%s
Salary:%s
‘‘‘ % (name,name,age,salary)

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

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


print (info)

執行結果:
----------------- info of yh--------------------
Name:yh
Age:22
Job:it
Salary:00


密文輸入(在pycharm中不好使)
import getpass
username = input("username:")
password = getpass.getpass("password:")
print(username,password)

if:
--------------------------
_username = ‘yh‘
_password = ‘yanghuan‘

username = input("username:")
password = input("password:")
print(username,password)

if _username == username and _password == password:
print("welcome user {name} login...".format(name = username))
else:
print("invalid username or password!")

執行結果:
username:yh
password:yanghuan
yh yanghuan
welcome user yh login...
--------------------------
猜年齡腳本:(if-elif-else while錯了猜三次)
age_of_oldboy = 56
count = 0
while True:
if count == 3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:

print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
if count == 3:
print("you have tried too many times..")

優化:

while True:
if count == 3:
break
改為:
while count<3:
------------------
if count == 3:
print("you have tried too many times..")
改為:
else:
print("you have tried too many times..")
---------------------------------------------
註:while-else 中的else代表while不成立走else
猜三次不對自動退出:
age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
else:
print("you have tried too many times..")
猜三次input n 退出,任意鍵繼續:
age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
if count == 3:
continue_guess = input("do u want to keep guess?")
if continue_guess != "n":
count = 0

--------------------------------------------------
for循環:
for-else:
當for正常執行完畢,執行else;
當for break非正常退出,不執行else。
for-break:
break:跳出break所在的整個for循環,包括else。
for-contine:
contine:跳出此次循環,進行下次循環,包括else。

age_of_oldboy = 56
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
else:
print("you have tried too many times..")
------------------------------------------
for i in range(1,5):
if i <3:
print("loop",i)
else:
continue
print("hehe...",i)
執行結果:
loop 1
hehe... 1
loop 2
hehe... 2
------------------------------------------
for i in range(0,10,2):#從0開始,隔一個數打印一次
print("loop",i)

python 筆記 week1