python 筆記 week1

來源:互聯網
上載者:User

標籤:自動   welcome   單行注釋   變數定義   inf   支援   gbk   做了   password   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.