iT邦幫忙

2021 iThome 鐵人賽

DAY 6
0
影片教學

文組生的Python爬蟲之旅系列 第 6

Day6 Python基礎語法四

今天的影片內容為稍微困難的函式與類別
雖然有點小複雜,但弄清楚後對程式的編寫將會有很大的幫助/images/emoticon/emoticon37.gif

Yes

以下為影片中有使用到的程式碼

#定義add()函式

def add(number1, number2):
    print(number1 + number2)
    
add(10,5)

#add()函式另一種寫法

def add(number1, number2):
    return number1 + number2
    
a = add(10,5)
print(a)
#最簡單的類別

class Animals():
    pass

class Animals:
    pass

#用類別來建立物件

Jellyfish = Animals()
Monkey = Animals()

#指派屬性給物件

Jellyfish.color = "white"
Jellyfish.weight = 200
print(Jellyfish.color, Jellyfish.weight)
#類別初始化

class Animals():
    
    
    def __init__(self, color, weight):
        self.color = color  
        self.weight = weight
        

Jellyfish = Animals("white",200)
Monkey = Animals("black",100)
print(Jellyfish.color, Jellyfish.weight)
print(Monkey.color, Monkey.weight)
#在類別中加入方法
class Animals():
    
    
    def __init__(self, color, weight):
        self.color = color  
        self.weight = weight
        
    
    def resources(self):
        print("The color is", self.color)
        print("The weight is", self.weight)        
        print()

Jellyfish = Animals("white",200)
Jellyfish.resources()

Monkey = Animals("black",100)
Monkey.resources()

P.S.影片中講解函式時,程式碼第17行出現的註解,請直接無視它(忘記刪掉了XD)
如果在影片中有說得不太清楚或錯誤的地方,歡迎留言告訴我,謝謝您的指教。


上一篇
Day5 Python基礎語法三
下一篇
Day7 CSV檔處理
系列文
文組生的Python爬蟲之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言