python的一些碎碎念

2021-10-25 20:25:52 字數 3472 閱讀 6641

# 將資料輸出檔案中

fp =

open

('d:/text.txt'

,'a+'

)# a+表示 如果檔案不存在就建立,存在就在檔案內容後面繼續追加

print

('hello world !'

,file

=fp)

fp.close(

)# 轉義字元: \n \t \r \b

# 若不希望字串中的轉義字元起作用,就在字串之前加上r,或者r

print

(r'hello\nworld'

)#想輸出引號中內容裡的'' 需要在每個『之前加入\

print()

# 注意事項: 最後乙個字元不能是反斜槓

print

('hello world'

)

#浮點數加減的注意事項

from decimal import decimal

print

(decimal(

'1.1'

)+decimal(

'2.2'))

#型別轉換

name=

'張三'

age =

20#當將str型別與int型別進行連線時,報錯

#解決方案: 型別轉換

print

('我叫'

+name+

',今年'

+str

(age)

+'歲'

)

繼承和重寫

class

person

(object):

def__init__

(self,name,age)

: self.name = name

self.age = age

definfo

(self)

:print

(self.name,self.age)

class

student

(person)

:def

__init__

(self,name,age,stu_no)

:super()

.__init__(name,age)

self.stu_no = stu_no

definfo

(self)

:super()

.info(

)print

(self.stu_no)

class

teacher

(person)

:def

__init__

(self,name,age,teachofyear)

:super()

.__init__(name,age)

self.teachofyear = teachofyear

definfo

(self)

:super()

.info(

)print

(self.teachofyear)

stu = student(

'gt',22

,'20181214109'

)teacher = teacher(

'wlh'

,'46',15

)stu.info(

)teacher.info(

)

ifname== 『main』: #以主程式方式執行

def

add(a,b)

:return a+b

if __name__ ==

'__main__'

:#以主程式方式執行

print

(add(10,

20))

讀取磁碟內容

file

=open

('a.txt'

,'r'

,encoding=

'utf-8'

)print

(file

.readlines())

file

.close(

)

二進位制方法開啟檔案

)with上下文管理器

))對比:

)

year =[82

,89,88

,86,85

,00,99

]print

('原列表'

,year)

for index,value in

enumerate

(year):if

str(value)

!='0'

: year[index]

=int

('19'

+str

(value)

)else

: year[index]

=int

('200'

+str

(value)

)print

('修改之後的列表:'

,year)

year.sort(

)print

('排序之後的列表為:'

,year)

一些碎碎念

一般分兩步,第一步,一般會在middleware 中統一進行,會和登陸耦合在一起 具體是使用者首先通過使用者名稱密碼證明自己的身份,然後獲得token 一類的儲存在cookie 裡的東西,然後使用者後面訪問時把這個東西放在請求中 這裡會有乙個問題,比如使用者剛開始是某個組的成員,但是之後被踢出組,有...

Spark on YARN 的一些碎碎念

adventage speed,tasks can start up very quickly in memory worker yarn 中是沒有這個概念的 因為 executor runs in container memory of container executor memory 客戶端提...

python碎碎念 一

python中的型別為序列型或者類的變數,在傳遞的過程中都是傳引用。如何複製物件的值而不是其引用呢?工廠方法 list dict 等 dict copy 方法 list slice copy 以上為淺拷貝 shallow copy 一般來說已經夠用。但是當物件內部還有其他類似的變數引用時,所生成的拷...