1.語法錯誤和異常錯誤
error message:while
true
print("hello python")
file 「c:\programming\eclipse\project\pythonstudy\exception.py」, line 9
while true print(「hello python」)
^ syntaxerror: invalid syntax
2.exception
error message:(8/0)
traceback (most recent call last):
file 「c:\programming\eclipse\project\pythonstudy\exception.py」, line 12, in
print(8/0)
zerodivisionerror: division by zero
3.not defined
traceback (most recent call last):(hello * 4)
file 「c:\programming\eclipse\project\pythonstudy\exception.py」, line 14, in
print(hello * 4)
nameerror: name 『hello』 is not defined
4.can』t convert 『int』 object to str implicitly
traceback (most recent call last):num = 6
print("hello python!" + num)
file 「c:\programming\eclipse\project\pythonstudy\exception.py」, line 17, in
print(「hello python!」 + num)
typeerror: can』t convert 『int』 object to str implicitly
5.
6.while
true:
try:
x = int(input("please enter a number: "))
break
except valueerror:
print("not vaild input, try again...")
7.物件導向和裝飾器try:
f = open("setence.txt")
s = f.readline()
i = int(s.strip())
except oserror as err:
print("os error : {}".format(err))
except valueerror as err:
print("could not convert data to an integer.")
f = open("setences.txt")
while
true:
try:
s = f.readline()
i = int(s.strip())
print(i)
if i == 5:
break
except oserror as err:
print("os error : {}".format(err))
except valueerror as err:
print("could not convert data to an integer.")
f.close
物件導向程式設計:
類(class):現實中一類食物的封裝(比如:學生)
類:屬性(比如:名字,成績)
類物件
例項物件
引用:通過引用對類的屬性和方法進行操作
例項化:建立乙個類的具體例項物件(比如:張三)
8.裝飾器#object-oriented and decorator
class
student:
def__init__
(self, name, grade):
self.name = name
self.grade = grade
defintroduce
(self):
print("hi, i am " + self.name)
print("my grade is " + str(self.grade))
defimprove
(self,amount):
self.grade = self.grade + amount
print("according to the help from my teacher, my grade is " + str(self.grade)+" now")
jim = student("jim", 86)
jim.introduce()
jim.improve(14)
# decorator
defadd_candles
(cake_func):
definsert_candels
():return cake_func() + " candles"
return insert_candels()
defmake_cake
():return
"cake"
gift_func = add_candles(make_cake)
print(make_cake())
print(gift_func)
def
add_candles
(cake_func):
definsert_candels
():return cake_func() + " candles"
return insert_candels()
defmake_cake
():return
"cake"
make_cake = add_candles(make_cake)
print(make_cake)
def
add_candles
(cake_func):
definsert_candels
():return cake_func() + " candles"
return insert_candels()
@add_candles
defmake_cake
():return
"cake"
#make_cake = add_candles(make_cake)
print(make_cake)
Python學習筆記(十二) Python模組
一 python模組 python 模組 module 是乙個 python 檔案,以 py 結尾,包含了 python 物件定義和python語句。模組讓你能夠有邏輯地組織你的 python 段,把相關的 分配到乙個模組裡能讓你的 更好用,更易懂。模組能定義函式,類和變數,模組裡也能包含可執行的 ...
python學習筆記十二(字典)
字典 1.概述 使用鍵 值 key value 儲存,具有極快的查詢速度 乙個字典裡可以儲存多個鍵 值對 注意 字典是無序的 key的特性 1 字典中的key必須唯一 2 key必須是不可變的物件 3 字串 整數等都是不可變的,可以作為key 4 list是可變的,不能作為key 例 儲存多個學生的...
Python學習筆記(十二) 標準庫
sys標準庫 sys主要負責直譯器與程式的互動,提供了一系列的函式用於和直譯器進行互動,並可以通過該模組訪問直譯器使用或者維護的變數。詳解文章 os標準庫 os模組負責程式與作業系統的互動,提供了訪問作業系統底層的介面。常用方法有 os.remove path filename 刪除檔案 os.re...