python實戰筆記(一)

2021-10-11 17:52:56 字數 4032 閱讀 6325

[python注釋]

[python變數]

[python運算子]

[python輸入輸出]

[分支]

[迴圈]

單行注釋

# 這是乙個單行注釋

print("test")

多行注釋
'''

這裡就是python的多行注釋方式

可以直接分行進行注釋操作

本質上是字串

'''import this

print("hello world")

特殊注釋變數使用

使用之前不用宣告;做引數傳遞時,所有的變數都是傳遞引用

定義變數

# arg = val

a = 1

# arg1, arg2 = 1, 2

a, b = 1, 2

# arg1 = arg2 = val

a = b = 3

使用變數的好處

節省空間,便於修改,可讀性強

使用變數注意事項

使用變數之前一定要賦值

常用資料型別

型別轉換

num = "6"

print(4 + int(num)) # 10

print(str(4) + num) # 46

算術運算子
# + 加法運算子

print(1 + 2) # 3

print("1" + "2") # 12

print([1, 2] + [3, 4]) # [1, 2, 3, 4]

# - 減法運算子

print(1 - 2) # -1

# * 乘法運算子

print(2 * 3) # 6

# ** 冪運算子

m, n = 2, 3

print(m ** n) # 2*2*2/8

# / 除法運算子

print(5 / 2) # 2.5

# // 整除運算

print(5 // 2) # 2

print(5.0 // 2) # 2.0

print(5.99 // 2) # 2.0

# % 求模運算

print(5 % 2) # 1

復合運算子
num = 2

num = num + 3

# 與下面這種方式等價,且效率更高

num += 3

比較運算子
# 布林型別是一種單獨的型別

res = 10 != 2

print(res) #true

# a, b指向同一塊記憶體

a = 1

b = 1

print(a == b) #true

print(a is b) #true

#a, b指向不同的記憶體

a = [1, 2]

b = [1, 2]

print(a == b) #true

print(a is b) #false

#可以使用id(arg)來檢視變數所在的記憶體區域

# 鏈式比較

print(1 <= 2 < 4) # true

# 邏輯運算

print(1 <= 2 and 2 < 4) #true

print(true or false) #true

print(not false) #true

# 擴充套件的true和false

# 0和空表示假

# bool(0), bool(""), bool()

print(0 and true) # 0

print(1 or false) # 1

print(true and 0) # 0

print(1 and 3) # 3

in操作符
li = [1, 2, 3, 4]

print(1 in li) # true

print("x" not in li) # true

str = "aabbccd"

print("ab" in str) # true

print("aabc" not in str) # true

輸入函式

使用內建(不用匯入其他的包)的函式input(),python2.xpython3.x中有些不同

python3.x

輸出函式

完整函式:print(values, sep, end, file, flush)

#輸出乙個值

print(123)

#輸出乙個變數

val = 123

print(val)

#輸出多個變數

name, year = rity, 1997

print(name, year) #rity 1997

#格式化輸出

print("my name is %s, age is %d" % (name, age))

print("my name is , age is ".format(name, age))

#輸出到檔案中

f = open("test.txt", "w")

print("******", file = f)

#輸出不自動換行

print("123", end="-") # 123-

#使用分隔符

print(1, 2, 3, sep="+") # 1+2+3

格式化輸出

%[(name)][flags][width][.precision]typecode

# 單分支判斷

age = 17

if age >= 18:

print("you are an adult")

print("end")

# 雙分支判斷

if age >= 18:

print("you are an adult")

else:

print("you are not an adult")

print("end")

# 多分支判斷

score = 59

if 90 <= score <= 100:

print("a")

elif 80 <= score:

print("b")

elif 60 <= score:

print("c")

else:

print("d")

# 常用的三目運算

a, b = 1, 3

x = 2 if a > 2 else 4 # x=4

y = 3 if b < 5 else 2 # y=3

# while迴圈,看重迴圈的條件

num = 0

while num < 4:

print(num)

num += 1

#for迴圈,多用於遍歷

li = [1, 2, 3, 4]

for x in li:

print(x) # 1 2 3 4

# 使用range()函式進行計數迭代

for i in range(1, 10): #[1, 10)

if i % 2: #奇數

print("%d: 奇數" % i)

else:

print("%d: 偶數" % i)

# pass語句,用來佔位,沒有實際邏輯

if true:

# todo

else:

pass

python實戰筆記(三)

資料型別 補充 上一部分 函式 集合無序的,不可隨機訪問的,不可重複的元素集合 可變集合的表示 直接表示 s print s,type s set iterable s1 set abc s2 set 1,2,3 print s1,s2 集合推導 參考列表推導 s print s 不可變集合 fro...

機器學習實戰筆記一 Python3

程式清單2 1 k近鄰演算法 本筆記將主要注意力放在理解 上,所以大家看 中的注釋即可 個人程式注釋 python3 部分 改造 針對iteritems只支援python2的情況 from numpy import import operator 運算子模組 def createdataset gr...

TensorFlow 實戰學習筆記(一)

1.windows 上安裝 tensorflow 流程 對於有英文基礎的朋友,建議直接閱讀官網安裝教程。本答案翻譯自 tensorflow 官網。系統環境要求 windows 上安裝 tensorflow 步驟 安裝 python 開發環境 檢查系統是否已安裝 python 開發環境。如果已安裝,則...