1. 程式人生 > >Python3-筆記-A-001-基本語法

Python3-筆記-A-001-基本語法

a* += 十進制 repr 數據類型 分支 吃飯 不變 lex

def helloworld():
# === Hello World ===
print("Hello World!") # 字符串用`‘`或者`"`包裹
print("中文") # 代碼中包含中文時(包含中文註釋)需要在首行添加該註釋(#coding=utf-8)


def explanatorynote():
# === 註釋 ===
# 單行註釋
‘‘‘ 這是
多行
註釋 ‘‘‘


def variable():
# === 變量 ===
num1 = 123 # 定義變量, Python能夠自動識別數字變量類型
result = 2 * num1 # 賦值變量
# 變量命名規則:
# 允許組成字符: a-z A-Z _ 0-9
# 不能以數字開頭
# 區分大小寫
# 小駝峰命名法: name / userName / userName123
# 大駝峰命名法: Person / UserName
# Python命名規範
# 項目名(大駝峰): MyAge
# / 模塊 (全小寫): myage.py
# (大駝峰): MyAge / __MyAge
# 屬性(全小寫): myage / __myage
# 方法(全小寫): myage / __myage / __init__
# 對象(全小寫): myage
# 形式參數(全小寫): myage
# 異常(大駝峰 + Error): MyAgeError
# > 簡單歸納為: 類、異常、項目名 用大駝峰; 其余全小寫(名字不易識別可加_)


def typeconversion():
# === 數據類型 ===
‘‘‘ 數字 (int / float / comples[復數])
布爾 (True / False)
字符串
列表
元組
字典 ‘‘‘
# Python能夠自動識別數字變量類型
# === 數據類型強轉 ===
int("123") # (對象)轉為整數
float("1.23") # (對象)轉為浮點數
complex(0.1, 0.2) # 創建復數(= 0.1+0.2j)(實部和虛部都是float)
str(123) # (對象)轉為字符串
repr("123") # (對象)轉為表達式字符串(機器)
eval("123*4") # 計算字符串內Python表達式
tuple([1, 2, 3]) # (序列)轉為元組 (:將字典轉為元組會損失數據)
dict([(1, "a"), (2, "b")]) # (序列)轉為字典 (列表 / 字典 均可)
list((1, 2, 3)) # (序列)轉為列表 (:將字典轉為列表會損失數據)
chr(123) # (整數)轉為字符
ord("{") # (字符)轉為整數
hex(123) # (整數)轉為十六進制字符串(‘0x7b‘)
oct(123) # (整數)轉為八進制字符串(‘0o173‘)
bin(123) # (整數)轉為二進制字符串(‘0b1111011‘)
int(‘0o173‘, 8) # 將其他進制數轉為(十進制)整數
def inputoutput():
# === 輸入 ===
inputData = input("輸入內容:") # 獲取輸入值(字符串),阻塞式
print("輸入的內容為:%s"%inputData)

# === 打印值 ===
name = "柳巖"
age = 21
print(name) # 普通的打印
# 拼接打印
print("Name:%s"%name)
print("Name:%s Age:%d"%(name, age)) # %aascii字符串 %c為字符; %s為字符串; %o為八進制整數; %d為有符號十進制整數; %u為無符號十進制整數; %x為十六進制整數; %e為浮點數指數; %f為浮點實數(%.2f:2位浮點數)
print(‘%-10s %s‘%(10, 10)) # %-10s 字符末尾+10個空格 => 10 10

# === 運算符 ===
# 算數運算符(+*可用於字符串拼接:str1+str2; str*3): + - * / % **(:2**3=8) //(取整除:3//2=1)
# 擴展知識: 2**10=1K / 2**20=1M / 2**30=1G / 2**40=1T / 2**50=1P / ...
# 賦值運算符: =
# 復合運算符(自運算): a+=b (a=a+b) / a*=b (a=a*b) / ... / a+=b*c (a=a+(b*c)) / ...
# 關系運算符: > < == >= <= != <>(兩邊不相等為true)
# 邏輯運算符: and or not
# 位運算符: &(0b1001&0b1101=0b1001) |(0b1001|0b1101=0b1101) ^(0b1001^0b1101=0b0100) ~(~0b1001=1... 0110) <<(左移1位等於乘以2:2<<3=16) >>(最高位不變)

 
def judge():
# === 判斷語句 ===
age = 22
beatiful = True

# - if (可嵌套) -
if age > 21: # true ; >為關系運算符
print("The Age is greater than 21.") # 用制表符區分代碼段
print("My thanks to you is beyond measure!")

if age < 21: # false
print("The Age is less than 21.")
print("My thanks to you is beyond measure!")

print("我從來不汙,只是汙的搬運工.")

# - if else -
if age > 18 and age < 25 and beatiful: # and為邏輯運算符
print("I am really into you!")
else:
print("Bye.")

boolean = True if 5 > 10 else False # 三元運算符
print(boolean) # => False

# - if elif else -
if age > 18 and age < 25 and beatiful: # and為邏輯運算符
print("I am really into you!")
elif age < 18 and beatiful:
print("你媽媽喊你回家吃飯啦.")
else:
print("Bye.")

# - ×分支 (Python沒有分支(switch)判斷) -


def loop():
# === 循環語句 ===
number1 = 5

# - while (可嵌套) -
while number1 <= 10:
print("number:%d"%number1)
number1+=1

while True:
print("True")
else:
print("False")

# - for (可嵌套) -
for i in range(5): # 循環5
print("for:range:%d"%i)

lis = ["a", "b", "c"]
for i in lis: # 按列表中的值循環
print("for:ray:%s"%i)

for i in range(10): # 循環10次後沒有課循環元素, 執行else語句
print(i)
else:
print("else")

def control():
# === 控制語句 ===
# - break - # 結束(一層)循環, 作用範圍: for / while (無作用:if)
for i in range(5):
if i == 2:
print("numberB:break")
break
print("numberB:%d"%i)

# - continue - # 跳過本次(一層)循環, 作用範圍: for / while (無作用:if)
for i in range(5):
if i == 2:
print("numberC:continue")
continue
print("numberC:%d"%i)


# === 封裝 ===
# - def 函數(方法) -
def method():
print("method")



# ======= 函數調用 ======
if __name__ == "__main__":
helloworld()
explanatorynote()
variable()
typeconversion()
inputoutput()
judge()
loop()
control()
method()

Python3-筆記-A-001-基本語法