Python 基礎歸納整理

2021-07-16 06:13:37 字數 4366 閱讀 5768

可以單獨定義變數

a = 62

b = 48

可以多個變數賦值

a = b = 123

a,b,c = 1,2,3

a,b = b,a

可以單行注釋

#這排是一行注釋

#這排又是一行注釋

a = 62

#這是注釋

b = 48

也可以多行注釋

'''這幾排

都是注釋

'''"""

這也是多排注釋

這也是多排注釋

這也是多排注釋

"""

python 沒有begin end,也沒有括號,而是用縮排作為**段。

if a > 1:

print

"a大於1"

else:

print

"a小於等於1"

python 中最基本的資料型別就是序列。python中內建序列有六種——–列表、元組、字串、unicode字串、buffer物件和xrange物件。最常用的是前三種。

這裡僅常用方法、型別,檢視完整請參考python官方文件。

序列通用方法

a 索引

序列中所有元素都有編號,從0開始,而這個編號就是元素在該序列中的索引。

>>>say_hi = 'hello'

>>>say_hi[1]

e>>>name = ['alex','bob','cady']

>>>name[-1]

'cady'

b 分片

分片是用兩個索引作為邊界值,訪問一定範圍內的元素,第乙個索引包含在分片內,第二個則不包括。

>>>number = [1,2,3,4,5,6,7,8,9]

>>>number[1:3]

[2,3]

>>>number[7:]

[8,9]

>>>number[6:-2]

[7]

分片還可以設定步長。

>>>number = [1,2,3,4,5,6,7,8,9]

>>>number[::3]

[1,4,7]

>>>number[8:1:-2]

[9, 7, 5, 3]

c 序列相加

>>>[1,2,3] + [4,5,6]

[1,2,3,4,5,6]

>>>'hello ' + 'world !'

'hello world !'

d 乘法

>>>'hello'*3

'hellohellohello'

>>>[1,2]*3

[1,2,1,2,1,2]

e 成員資格

>>>'e'

in'hello'

true

>>>'c'

in'python'

false

>>>43

in [1,2,54,43]

true

f 度量

長度

>>>number = [1,2,3]

>>>len(number)

3

最大最小值

>>>number = [1,2,3,4,5]

>>>min(number)

1>>>max(number)

5

列表

列表中可以是字串,數字,列表或者其他型別的物件,如['hello',24,[1,2,3],]

a 列表的基本操作

a 改變列表

>>>number = [1,2,3]

>>>number

[1,2,3]

>>>number[2] = 5

>>>number

[1,2,5]

b 刪除元素

>>>number = [1,2,3,4]

>>>del number[2]

>>>number

[1,2,4]

c 分片賦值

>>>number = [1,2,3]

>>>number[1:] = [4,5]

>>>number

[1,4,5]

b 列表方法

a 追加

>>>number = [1,2,3,4]

>>>number

[1,2,3,4,5]

b 計數

>>>number = [1,2,1,4]

>>>number.count(1)

2>>>number.count(2)

1

c 擴充套件

>>>number_a = [1,2,3]

>>>number_b = [4,5,6]

>>>number_a.extend(b)

>>>number_a

[1,2,3,4,5,6]

d 索引

>>>number = [1,2,3,4,5,6,7]

>>>number.index(3)

2

e 插入

>>>number = [1,2,3,4]

>>>number.insert(2,5)

>>>number

[1,2,5,3,4]

f 彈射出

>>>number = [1,2,3]

>>>number.pop()

3>>>number

[1,2]

>>>number.pop(0)

1

g 移除

>>>number = [1,2,3,4,1]

>>>number.remove(1)

>>>number

[2,3,4,1]

h 反序

>>>number = [1,2,3]

>>>number.reverse()

>>>number

[3,2,1]

i 排序

>>>number = [4,1,3,2]

>>>number.sort()

>>>number

[1,2,3,4]

元組

元組是不可變的序列。

>>>1,2,3

(1,2,3)

字串

a 格式化

字串格式化常用型別

型別標誌

帶符號十進位制整數

d十進位制浮點數

f字串(str)

s

>>>print

'%d 加 %d 等於 %d' %(1,1,2)

1 加 1 等於 2

b字串方法

a 查詢

>>>words = "hello world!"

>>>words.find("o")

4>>>words.find("world")

6

b 轉換成字串或元組

>>>words = 'hello'

>>>list(words)

['h', 'e', 'l', 'l', 'o']

>>>tuple(words)

('h', 'e', 'l', 'l', 'o')

c 連線序列中的元素

>>>words = ['1','2','3','4','5']

>>>words.join('+')

'1+2+3+4+5'

d 拆分字串

>>>words = "a;b;c;d;e"

>>>words.split(";")

['a', 'b', 'c', 'd', 'e']

e 替換

>>>words = "abcdefg"

>>>words.replace("de","z")

'abczfg'

以上命名非官方命名,僅是個人理解

python 各種鎖機制歸納整理

q 為什麼要用鎖?什麼樣的業務場景下需要用鎖?就拿之前的工單系統來說,當審批方式為角色組審批時,代表該角色組內任意一人審批即可,這時,該角色組內成員的系統上都是可以顯示審批按鈕,如果此時a審批員和b審批員都同時審批了同一工單,有可能會出現不一樣的審批結果,即使審批意見是一致的,多人操作也會增加對資料...

VFL歸納整理

全稱是visual format language,翻譯過來是 視覺化格式語言 vfl是蘋果公司為了簡化autolayout的編碼而推出的抽象語言 事例 h cancelbutton 72 12 acceptbutton 50 canelbutton寬72,acceptbutton寬50,它們之間間...

Python基礎知識歸納

python 基礎教程 python 基礎教程 python 簡介 python 環境搭建 python 中文編碼 python 基礎語法 python 變數型別 python 運算子 python 條件語句 python 迴圈語句 python while 迴圈語句 python for 迴圈語句...