範例程式主要來自於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))
但若因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))
def my_function():
print("This is my function")
my_function()
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)
def my_function(*args):
for x in args:
print("Input: " + x)
my_function("Apple","Orage","Banana")
在function內部宣告的區域變數,其變數範圍只存在於function內。
在程式本體宣告的全域變數,變數範圍存在整支程式中。
當function出現與全域變數相同命名的變數,這時此變數在該function內會視為區域變數,不影響全域變數的數值。
透過「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))
Python是一種物件導向的程式語言,物件導向的概念已經有很多文章介紹,故以下將直接說明Class和Object相關操作。
# 宣告Class
class Book:
name = "unknown"
author = "unknown"
# 建立物件
book1 = Book()
print(book1.author)
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()
# 宣告父類別
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()
# 宣告父類別
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()