Day6 Python基础语法四

今天的影片内容为稍微困难的函式与类别
虽然有点小复杂,但弄清楚後对程序的编写将会有很大的帮助/images/emoticon/emoticon37.gif

以下为影片中有使用到的程序码

#定义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)
如果在影片中有说得不太清楚或错误的地方,欢迎留言告诉我,谢谢您的指教。


<<:  04 - Tmux - 终端机管理工具

>>:  Day 8【钱包登入区 - Loading Message】阿嬷为什麽你有感觉?

新新新手阅读 Angular 文件 - Add Service(2) - Day09

学习目标 这一篇是纪录阅读官方文件 Add services 的笔记内容。 本篇的内容是接续 Day...

Day 13 ( 中级 ) 大型数字 ( 图形数字 )

大型数字 ( 图形数字 ) 教学原文参考:大型数字 ( 图形数字 ) 如果要在 Scratch 3 ...

Java学习之路06---逻辑结构陷阱

架构图 悬挂 逻辑判断语句若是只有连接一条语句时,这一条语句会与离它最近的逻辑判断语句相连,例如以下...

JS 运算子 与 优先性及相依性 DAY53

运算子 MDN : https://developer.mozilla.org/zh-TW/docs...

Day 3 - 类神经网路(一)

谈到机器学习, 一定会谈到神经元(英语:neuron) 神经元(neuron) 又名神经原或神经细胞...