a001 python 基礎知識點

2021-10-10 20:46:10 字數 4173 閱讀 6979

輸出到同一行,在行的末尾加\

a =

'''abcd

abcd

'''print

(a)print

('----------------'

)b =

'''abcd\

abcd

'''print

(b)

輸出結果:

abcd

abcd--

----

----

----

--abcd abcd

a =

'hello python'

# a = ['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']

# 法一

b =set

(a)print

(b)#

c =print

(c)#

# 法二

from collections import counter

d =dict

(counter(a)

)print

(d)#

字典 key 是不可變的,可以是 數值、字串、元組(不能直接或間接包含可變資料型別的)。

元組是不可變資料型別,是指元組的元素(不包括孫子元素)所指向的記憶體位址不能改變。

a =(1

,2,[

3,4]

,(5,

6))# a[1] = 'tom' # 報錯

# a[2] = 'cat' # 報錯a[2

][0]

=30print

(a)# (1, 2, [30, 4], (5, 6))

# a[3][0] = 50 # 報錯

獲取字典 keys 方法很多,下面介紹不常見但很實用的方法。

a =

a['cls']=

'one'

a['ax']=

'two'

print

(a)#

b =list

(a)print

(b)# ['name', 'age', '***', 'cls', 'ax']

a =

a['cls']=

'one'

a['ax']=

'two'

print

(a)#

b =sorted

(a)print

(b)# ['age', 'ax', 'cls', 'name', '***']

例1:

classa(

object):

x =1class

b(a)

:pass

class

c(a)

:pass

>>

> b.x =

2>>

>

print

(a.x, b.x, c.x)

>>

> a.x =

3>>

>

print

(a.x, b.x, c.x)

輸出結果為:

121

323

分析:

b.x 賦值之前,a.x, b.x, c.x 都引用的是 a類的類變數 x。

b.x 賦值之後,b.x 是 b類擁有了自己的類變數 x(而非 a類變數x),a.x, c.x 都引用的是 a類的類變數 x。

例二:

import time

class

item

:def

__init__

(self, name, price)

: self.name = name

self.price = price

# 定義析構函式,物件在銷毀前呼叫

def__del__

(self)

:print

('del 刪除物件'

)if __name__ ==

'__main__'

: obj = item(,10

) x = obj

print(id

(x),

id(obj)

)del obj

print

('----------------'

)print

('end'

) time.sleep(3)

print

(x.name)

# print(obj.name) # 會報錯,因為前面已經刪除了 obj 物件與 item 物件的引用關係

輸出:

2212862269136

2212862269136--

----

----

----

--enddel 刪除物件

del 刪除物件 為什麼最後列印?

分析:因為del() 是在物件銷毀的時候呼叫的。

類的 私有方法和屬性(雙字首下劃線,沒有字尾下劃線):

classa(

):name =

'tom'

_age =

5 __*** =

'f'def

__index__

(self)

:pass

def_fun1

(self)

: self._attr =

1def

__func2

(self)

: self.__attr2 =

2def

func3

(self)

:pass

class

b(a)

:pass

if __name__ ==

'__main__'

: a = a(

) a.name

a._age

# a.__*** # 報錯

b = b(

) b.name

b._age

# b.__*** # 報錯

b._fun1(

)# b.__fun2() # 報錯

b.func3(

)

對列表(可變物件)切片相當於淺拷貝。

a =[1

,2,3

,[4,

5,6]

]b = a[:]

print(id

(a),

id(b))b[

1]=22

print

(a, b)b[3

][0]

=11print

(a, b)

輸出結果:

2354352755840

2354352763584[1

,2,3

,[4,

5,6]

][1,

22,3,

[4,5

,6]]

[1,2

,3,[

11,5,

6]][

1,22,

3,[11

,5,6

]]

在進行大資料的查詢時,如果資料型別是列表,最好先將列表轉換成元組,再進行查詢。因為元組的查詢效率要高於列表。

-t選項來啟動 python 命令列直譯器,會就**中混用空格和跳格的位置進行警告資訊。

-tt選項,會公升級成錯誤資訊。

# yes

python -t demo.py

python -tt demo.py

# no

python demo.py

Python基礎知識點

python2與python3編碼相容性 python2如果沒有指定編碼格式,預設的編碼是ascii,不進行轉換會出現unicodedecodeerror錯誤,為了使得python2與3進行相容,可以用如下 實現 import sys from django.utils import six if ...

python基礎知識點概括

1.1.1 輸出 print 要輸出的內容 加引號,就是按照字串的格式輸出,否則就是計算之後輸出,如果要格式化輸出的話,用 隔開 1.1.2 輸入 input 提示資訊 raw input t提示資訊 都可以,讀取的內容都是字串形式,有些時候,就需要將讀取的字串轉為整數,使用int 來轉換 1.2....

Python基礎知識點(一)

編寫程式需要使用注釋,一般以 開頭 單行注釋以 開頭 多行注釋 內容 中文支援以 conding utf 8 變數 用於儲存資料 變數型別 數字型別 布林型別 true,false 字串型別 列表型別 元組型別 字典型別 檢視型別 type 變數名 1 標示符 由字母,下劃線,數字組成 且數字不能開...