從Zero到Hero,一文掌握Python關鍵程式碼
# 01基礎篇
# 變數
1 #int 2 one=1 3 some_number=100 4 5 print("one=",one) #print type1 6 print("some_number=",some_number) #python3 print need add () 7 8 """output 9 one= 1 10 some_number= 100 11 """
1 #booleans 2 true_boolean=True 3 false_boolean=False 4 5 print("true_boolean:",true_boolean) 6 print("False_boolean;",false_boolean) 7 8 """output 9 true_boolean: True 10 False_boolean; False 11 """
1 #string 2 my_name="leizeling" 3 4 print("my_name:{}".format(my_name)) #print type2 5 6 """output 7 true_boolean: True 8 False_boolean; False 9 """
#float book_price=15.80 print("book_price:{}".format(book_price)) """output book_price:15.8 """
# 控制流:條件語句
1 #if 2 if True: 3 print("Hollo Python if") 4 if 2>1: 5 print("2 is greater than 1") 6 7 """output 8 Hollo Python if 9 2 is greater than 1 10 """
1 #if……else 2 if 1>2: 3 print("1 is greater than 2") 4 else: 5 print("1 is not greater than 2") 6 7 """output 8 true_boolean: True 9 False_boolean; False 10 """
#if……elif……else if 1>2: print("1 is greater than 2") elif 1<2: print("1 is not greater than 2") else: print("1 is equal to 2") """output 1 is not greater than 2 """
# 迴圈/迭代器
#while num=1 while num<=10: print(num) num+=1 """output 1 2 3 4 5 6 7 8 9 10 """
1 #for 2 for i in range(1,11):#range(start,end,step),not including end 3 print(i) 4 5 """output 6 1 7 2 8 3 9 4 10 5 11 6 12 7 13 8 14 9 15 10 16 """
# 02列表:陣列資料結構
#create integers list my_integers=[1,2,3,4,5] print(my_integers[0]) print(my_integers[1]) print(my_integers[4]) """output 1 2 5 """
#create str list relatives_name=["Toshiaki","Juliana","Yuji","Bruno","Kaio"] print(relatives_name) """output [`Toshiaki`, `Juliana`, `Yuji`, `Bruno`, `Kaio`] """
1 #add item to list 2 bookshelf=[] 3 bookshelf.append("The Effective Engineer") 4 bookshelf.append("The 4 Hour Work Week") 5 print(bookshelf[0]) 6 print(bookshelf[1]) 7 8 """output 9 The Effective Engineer 10 The 4 Hour Work Week 11 """
# 03字典:鍵-值資料結構
#dictionary struct dictionary_example={"key1":"value1","key2":"value2","key3":"value3"} dictionary_example """output {`key1`: `value1`, `key2`: `value2`, `key3`: `value3`} """
#create dictionary dictionary_tk={"name":"Leandro","nickname":"Tk","nationality":"Brazilian"} print("My name is %s"%(dictionary_tk["name"])) print("But you can call me %s"%(dictionary_tk["nickname"])) print("And by the waay I am {}".format(dictionary_tk["nationality"])) """output My name is Leandro But you can call me Tk And by the waay I am Brazilian """
1 #dictionary add item 2 dictionary_tk["age"]=24 3 print(dictionary_tk) 4 5 """output 6 {`nationality`: `Brazilian`, `nickname`: `Tk`, `name`: `Leandro`, `age`: 24} 7 """
#dictionayr del item dictionary_tk.pop("age") print(dictionary_tk) """output {`nationality`: `Brazilian`, `nickname`: `Tk`, `name`: `Leandro`} """
# 04迭代:資料結構中的迴圈
#iteration of list bookshelf=["The Effective Engineer","The 4 hours work week","Zero to One","Lean Startup","Hooked"] for book in bookshelf: print(book) """output The Effective Engineer The 4 hours work week Zero to One Lean Startup Hooked """
1 #雜湊資料結構(iteration of dictionary) 2 dictionary_tk={"name":"Leandro","nickname":"Tk","nationality":"Brazilian"} 3 for key in dictionary_tk: #the attribute can use any name,not only use "key" 4 print("%s:%s"%(key,dictionary_tk[key])) 5 print("===================") 6 for key,val in dictionary_tk.items(): #attention:this is use dictionary_tk.items().not direct use dictionary_tk 7 print("{}:{}".format(key,val)) 8 9 """output 10 nationality:Brazilian 11 nickname:Tk 12 name:Leandro 13 =================== 14 nationality:Brazilian 15 nickname:Tk 16 name:Leandro 17 """
# 05 類和物件
#define class class Vechicle: pass
#create instance car=Vechicle() print(car) ""output <__main__.Vechicle object at 0x000000E3014EED30> """
#define class class Vechicle(object):#object is a base class def __init__(self,number_of_wheels,type_of_tank,seating_capacity,maximum_velocity): self.number_of_wheels=number_of_wheels self.type_of_tank=type_of_tank self.seating_capacity=seating_capacity self.maximum_velocity=maximum_velocity def get_number_of_wheels(self): return self.number_of_wheels def set_number_of_wheels(self,number): self.number_of_wheels=number #create instance(object) tesla_models_s=Vechicle(4,"electric",5,250) tesla_models_s.set_number_of_wheels(8)#方法的呼叫顯得複雜 tesla_models_s.get_number_of_wheels() """output 8 """
#use @property讓方法像屬性一樣使用 #define class class Vechicle_1(object):#object is a base class def __init__(self,number_of_wheels,type_of_tank,seating_capacity,maximum_velocity):#first attr must is self,do not input the attr self._number_of_wheels=number_of_wheels ###屬性名和方法名不要重複 self.type_of_tank=type_of_tank self.seating_capacity=seating_capacity self.maximum_velocity=maximum_velocity @property #讀屬性 def number_of_wheels(self): return self._number_of_wheels @number_of_wheels.setter #寫屬性 def number_of_wheels(self,number): self._number_of_wheels=number def make_noise(self): print("VRUUUUUUUM") tesla_models_s_1=Vechicle_1(4,"electric",5,250) print(tesla_models_s_1.number_of_wheels) tesla_models_s_1.number_of_wheels=2 print(tesla_models_s_1.number_of_wheels) print(tesla_models_s_1.make_noise()) """output 4 2 VRUUUUUUUM None """
# 06封裝:隱藏資訊
1 #公開例項變數 2 class Person: 3 def __init__(self,first_name): 4 self.first_name=first_name 5 tk=Person("TK") 6 print(tk.first_name) 7 tk.first_name="Kaio" 8 print(tk.first_name) 9 10 """output 11 TK 12 Kaio 13 """
#私有例項變數 class Person: def __init__(self,first_name,email): self.first_name=first_name self._email=email def get_email(self): return self._email def update_email(self,email): self._email=email tk=Person("TK","tk@mail.com") tk.update_email("new_tk@mail.com") print(tk.get_email()) """output new_tk@mail.com """
#公有方法 class Person: def __init__(self,first_name,age): self.first_name=first_name self._age=age def show_age(self): return self._age tk=Person("TK",24) print(tk.show_age()) """output 24 """
1 #私有方法 2 class Person: 3 def __init__(self,first_name,age): 4 self.first_name=first_name 5 self._age=age 6 def _show_age(self): 7 return self._age 8 def show_age(self): 9 return self._show_age() #此處的self不要少了 10 tk=Person("TK",24) 11 print(tk.show_age()) 12 13 """output 14 24 15 """
1 #繼承 2 class Car:#父類 3 def __init__(self,number_of_wheels,seating_capacity,maximum_velocity): 4 self.number_of_wheels=number_of_wheels 5 self.seating_capacity=seating_capacity 6 self.maximum_velocity=maximum_velocity 7 class ElectricCar(Car):#派生類 8 def __init(self,number_of_wheels_1,seating_capacity_1,maximum_velocity_1): 9 Car.__init__(self,number_of_wheels_1,seating_capacity_1,maximum_velocity_1) #利用父類進行初始化 10 11 my_electric_car=ElectricCar(4,5,250) 12 print(my_electric_car.number_of_wheels) 13 print(my_electric_car.seating_capacity) 14 print(my_electric_car.maximum_velocity) 15 16 """output 17 4 18 5 19 250 20 """
注:從機器之心轉載
相關文章
- 從 Zero 到 Hero ,一文掌握 PythonPython
- python,從似懂非懂到初步掌握Python
- Attacking organizations with big scopes: from zero to hero -- by Hussein Daher
- @程式設計師,一文讓你掌握Python爬蟲!程式設計師Python爬蟲
- 從函式到包的Python程式碼層次函式Python
- 從零到熟悉,帶你掌握Python len() 函式的使用Python函式
- Git從入門到熟練掌握Git
- 從 Java 程式碼到 Java 堆Java
- Python web開發需要掌握什麼技能?基礎很關鍵!PythonWeb
- 關於Python庫 pygame zero(pgzero)哪些坑PythonGAM
- 從技術到管理:思維轉變是關鍵
- 一文徹底理解邏輯迴歸:從公式推導到程式碼實現邏輯迴歸公式
- 掌握seo關鍵點,輕鬆穩定網站關鍵詞排名網站
- Python 指令碼高階程式設計:從基礎到實踐Python指令碼程式設計
- .NET8 Blazor 從入門到精通:(一)關鍵概念Blazor
- 通過Python掃描程式碼關鍵字並進行預警Python
- Socket相關程式:從Windows移植到LinuxWindowsLinux
- 從基礎到實現:整合學習綜合教程(附Python程式碼)Python
- 開發者思考:從超執刀到Loop Hero,下一屆遊歌的變遷路OOP
- 詳解AlphaGo到AlphaGo Zero!Go
- 【Java併發程式設計】從CPU快取模型到JMM來理解volatile關鍵字Java程式設計快取模型
- 零基礎掌握區塊鏈關鍵概念區塊鏈
- 《Python程式設計:從入門到實踐》Python程式設計
- “全棧”:從AI開發者到AI工業家的首席關鍵詞全棧AI
- 一文讀懂層次聚類(Python程式碼)聚類Python
- 高亮:單關鍵詞、多關鍵詞、多組多關鍵詞,從簡單到複雜實現滿足多方面需求的頁面關鍵詞高亮
- 從錨點到關鍵點,最新的目標檢測方法發展到哪了
- SQL worm sapphire 關鍵程式碼分析 (轉)SQLWormAPP
- 一文快速掌握 Git 用法Git
- 從程式碼到部署微服務實戰(一)微服務
- dlopen程式碼詳解——從ELF格式到mmap
- 從教女友寫程式碼中學到的
- 把成熟的程式碼從.NET移植到MonoMono
- 從碼農到設計者,從單例模式入手設計程式碼單例模式
- 【Python從入門到精通】(十)Python流程控制的關鍵字該怎麼用呢?【收藏下來,常看常新】Python
- 一文掌握Redis主從複製、哨兵、Cluster三種叢集模式Redis模式
- 拒做程式設計師小白?計算機關鍵概念你不得不掌握!程式設計師計算機
- 閉包概念是掌握React.JS的關鍵 - NitsanReactJS