Day06 - Python基本语法 Part 3,函式与类别

范例程序主要来自於W3Schools

条件式

a = 10
b = 20
if a > b and a >= 0:
  print("a > b")
elif a == b:
  print("a == b")

# 若该条件内暂时没有要执行的程序码,需使用「pass」,不可留空
elif a < 0:
  pass
else:
  print("a < b")

回圈

Python中标准回圈有while和for两种,其中break、continue用法跟大部分程序语言差不多,比较特殊的是如果不符合回圈条件而离开回圈的话,会执行else区块(若有提供):

i = 1
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
else:
  print("Loop end. Now i is " + str(i))

https://ithelp.ithome.com.tw/upload/images/20210918/20141886NAU55QqgXK.png

但若因break跳出,则不会执行else区块:

i = 1
while i < 6:
  i += 1
  if i == 3:
    continue
  if i == 5:
    break
  print(i)
else:
  print("Loop end. Now i is " + str(i))

https://ithelp.ithome.com.tw/upload/images/20210918/20141886nuiC38jG5y.png

函式

  • 函式的宣告与使用:
def my_function():
  print("This is my function")

my_function()
  • 宣告有引数(argument)的函式;
def my_function(name, age):
  x = "My name is {}. My age is {}."
  print(x.format(name, age))

my_function("Alice", 20)

  • 提供引数预设值:
def my_function(name="None", age=99):
  x = "My name is {}. My age is {}."
  print(x.format(name, age))


my_function("Alice", 20)
my_function()
my_function("Bob")
my_function(name= "Cathy", age=15)
my_function(age=0)

https://ithelp.ithome.com.tw/upload/images/20210918/20141886ikjHn5w4mc.png

  • 如需宣告引数数量可变动,则在引数前面加上「*」,呼叫函式时放入的引数将以Tuple的形式传入函式内部:
def my_function(*args):
  for x in args:
    print("Input: " + x)

my_function("Apple","Orage","Banana")

https://ithelp.ithome.com.tw/upload/images/20210918/20141886KIqGlNGWWf.png

  • 变数的值域
  1. 在function内部宣告的区域变数,其变数范围只存在於function内。

  2. 在程序本体宣告的全域变数,变数范围存在整支程序中。

  3. 当function出现与全域变数相同命名的变数,这时此变数在该function内会视为区域变数,不影响全域变数的数值。

  4. 透过「global」宣告,可在函式内部使用全域变数。

x = 300

# 宣告myfunc1,印出其中区域变数x的数值 = 1000
def myfunc1():
  x = 1000
  print("myfunc1: x = " + str(x))

# 宣告myfunc2,印出全域变数x的数值
def myfunc2():
  global x
  print("myfunc2: x = " + str(x))

myfunc1()
myfunc2()
print("global : x = " + str(x))

https://ithelp.ithome.com.tw/upload/images/20210918/20141886FDIxJX7tBV.png

Class

Python是一种物件导向的程序语言,物件导向的概念已经有很多文章介绍,故以下将直接说明Class和Object相关操作。

  • 建立Class和物件
# 宣告Class
class Book:
  name = "unknown"
  author = "unknown"

# 建立物件
book1 = Book()
print(book1.author)
  • 初始化函式:init(arg1, arg2, arg3) 在建立物件时会被呼叫。
    重要:Class内每个函式的第一个引数arg1将作为该Class的执行个体使用,类似this的用法。
class Book:
  def __init__(self, name, author):
    self.name = name
    self.author = author
  
  def print_name(test):
    print("Book name: " + test.name)


book1 = Book("Pride and Prejudice", "Jane Austen")
book1.print_name()
  • 物件内的属性可被删除
class Book:
  def __init__(self, name, author):
    self.name = name
    self.author = author
  
  def print_name(test):
    print("Book name: " + test.name)


book1 = Book("Pride and Prejudice", "Jane Austen")
del book1.name
book1.print_name()

https://ithelp.ithome.com.tw/upload/images/20210918/20141886cCERUZIhMi.png

  • 继承
    宣告子类别Book,继承Product
# 宣告父类别
class Product:
  def __init__(self, name, unitprice, quantity):
    self.name = name
    self.unitprice = unitprice
    self.quantity = quantity

  def print_information(self):
    print("Product Name: " + self.name)
    print("Price: " + str(self.unitprice))
    print("Stock: " + str(self.quantity))

# 宣告子类别Book,继承Product
class Book(Product): 
  pass

prod1 = Product("Apple", 10, 100)
prod2 = Book("Pride and Prejudice", 18, 10)

prod1.print_information()
prod2.print_information()

https://ithelp.ithome.com.tw/upload/images/20210918/20141886o8vKFb1Dee.png

  • 覆写(Override):
  1. 子类别使用相同的命名会覆盖父类别。
  2. 使用super().function()继承父类别的方法(Method)和属性(Property)
# 宣告父类别
class Product:
  def __init__(self, name, unitprice, quantity):
    self.name = name
    self.unitprice = unitprice
    self.quantity = quantity

  def print_information(self):
    print("Product Name: " + self.name)
    print("Price: " + str(self.unitprice))
    print("Stock: " + str(self.quantity))

# 宣告子类别Book,继承Product,并覆写__init__()和print_information()
class Book(Product): 
  def __init__(self, name, unitprice, quantity, author):
    super().__init__(name, unitprice, quantity) 
    self.author = author
  
  def print_information(self):
    super().print_information()
    print("Author: " + self.author)


prod1 = Product("Apple", 10, 100)
prod2 = Book("Pride and Prejudice", 18, 10, "Jane Austen")

prod1.print_information()
prod2.print_information()

https://ithelp.ithome.com.tw/upload/images/20210918/20141886xCCtin6pm0.png


<<:  Day 3 情报收集 - Information Gathering

>>:  从 IT 技术面细说 Search Console 的 27 组数字 KPI (18) :结构化资料(其他)

Day 17 Flask 静态文件

这篇主要是讲到静态文件,静态文件就是 CSS 、 JavaScript 与图片档之类的档案(因为在 ...

IT铁人DAY 4-物件导向基本概念(3)

修饰符(Modifier)   上一篇有讲到封装的特性,也就是把一个类别要运行操作所需用到的资讯都包...

参与"在MCU 上全面建构AI能力" 9/10 心得

今晚参与了"MakerPro社群媒体平台"举办的 在MCU 上全面建构AI能力 ...

Day17,NFS provisioner

正文 今天要来安装Kubernetes上的NFS provisioner,主要就是搭配我的DS718...

[Day 3] php阵列与资料结构

php中阵列的写法 一般阵列 其索引值形式为从0开始往後累加的数字(0、1、2、3...) $fru...