在學習python的過程中,我積累了不少的筆記以及練習,但是疏於整理正好時至春節放假之際,再加上新型冠狀病毒的肆虐。各個村、小區都已經封鎖,實在無聊對我考完研究生之後學習python到現在的筆記進行記錄一下,這是在家裡學習的一部分,還有在學校的一部分寫在這裡作為提醒寫在這裡,以及進行時不時的回憶
程序對於python不可小覷,以下是我學習程序的**,以後再做補充
# -*- coding: utf-8 -*-
"""spyder editor
this is a temporary script file.
"""#程序
import multiprocessing
import time
defaction
(a,b)
:for i in
range(30
):print
(a,' '
,b) time.sleep(
0.1)
if __name__==
'__main__'
: jc1=multiprocessing.process(target=action,args=
('程序一',0
))jc2=multiprocessing.process(target=action,args=
('程序二',1
))jc1.start(
) jc2.start(
) jc1.join(
) jc2.join(
)print
('jc1,jc2任務都已執行完畢'
) jc1.close(
) jc2.close(
)
'''
檔案路徑不能用反斜槓『\』。舉個例子,如果我傳入的檔案路徑是這樣的:
則會報錯syntaxerror: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: tr
原因分析:在windows系統當中讀取檔案路徑可以使用\,但是在python字串中\有轉義的含義,如\t可代表tab,\n代表換行,所以我們需要採取一些方式使得\不被解讀為轉義字元。目前有3個解決方案
1、在路徑前面加r,即保持字元原始值的意思。
2、替換為雙反斜槓
3、替換為正斜槓
'''fpath = r'c:\windows\system.ini'
with
open
(fpath,
'r')
as f:
s = f.read(
)print
(s)
#錯誤的捕獲並且記錄
import logging
from functools import
reduce
import json
defstr2num
(s):
return json.loads(s)
#json.loads 用於解碼 json 資料,json 型別轉換到 python 的型別
defcalc
(exp)
: ss = exp.split(
'+')
ns =
map(str2num, ss)
return
reduce
(lambda acc, x: acc + x, ns)
defmain()
:try
: r = calc(
'100 + 200 + 345'
)print
('100 + 200 + 345 ='
, r)
r = calc(
'99 + 88 + 7.6'
)print
('99 + 88 + 7.6 ='
, r)
except exception as e:
logging.exception(e)
main(
)
#reduce
'''reduce把乙個函式作用在乙個序列[x1, x2, x3, ...]上,
這個函式必須接收兩個引數,
'''reduce
(f,[x1, x2, x3, x4]
)= f(f(f(x1, x2)
, x3)
, x4)
#列舉型別不可例項化,不可更改。
'''
#練習 @property
class
screen
(object):
@property
defwidth
(self)
:return self._width
@width.setter
defwidth
(self,width)
: self.__width=width
@property
defheight
(self)
:return self._height
@height.setter
defheight
(self,height)
: self.__height=height
@property
defresolution
(self)
:return self.__width*self.__height
# 測試:
s = screen(
)s.width =
1024
s.height =
768print
('resolution ='
, s.resolution)
if s.resolution ==
786432
:print
('測試通過!'
)else
:print
('測試失敗!'
)
#方法一
class
student
(object):
count =
0def
__init__
(self, name)
: self.name = name
student.count=student.count+
1#方法二
class
student
(object):
"""student class
function:
add_count
__init__ -- instantiate object
_add_count -- add count
"""count =
0def
_add_count
(func)
:def
(*args,
**kw)
: student.count +=
1return func(
*args,
**kw)
@_add_count
def__init__
(self, name)
: self.name = name
# 測試:
if student.count !=0:
print
('測試失敗!'
)else
: bart = student(
'bart'
)if student.count !=1:
print
('測試失敗!'
)else
: lisa = student(
'bart'
)if student.count !=2:
print
('測試失敗!'
)else
:print
('students:'
, student.count)
print
('測試通過!'
)
上面的絕大多數**都是我在廖雪峰的教程**上進行學習,並摘抄參考下來的
廖雪峰老師的**
[1]:
Python 一些小技巧
insert index,value 方法是在列表中間增加元素,同樣的,如果傳入的是乙個列表或元組,則也會被巢狀插入。序列封包 sequence packing 和序列解包 sequence unpacking vals 10,20,30 print vals 10,20,30 a b,c vals...
python一些小知識
1 python連線mssql資料庫編碼問題 python一直對中文支援的不好,最近老遇到編碼問題,而且幾乎沒有通用的方案來解決這個問題,但是對常見的方法都試過之後,發現還是可以解決的,下面總結了常用的支援中文的編碼問題 這些方法中可能其中乙個就能解決問題,也可能是多個組合 1 首先,要保證檔案的開...
Python學習之路上的幾個經典問題
語法如下 on true if expression else on false 如果 expression 為true,則表示式的值為 on true 否則為 on false 示例如下 使用not判斷list是否為空,是相當pythonic的方法。示例如下 使用乙個額外的狀態變數是想當non p...