范例程序主要来自於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()
<<: Day 3 情报收集 - Information Gathering
>>: 从 IT 技术面细说 Search Console 的 27 组数字 KPI (18) :结构化资料(其他)
这篇主要是讲到静态文件,静态文件就是 CSS 、 JavaScript 与图片档之类的档案(因为在 ...
修饰符(Modifier) 上一篇有讲到封装的特性,也就是把一个类别要运行操作所需用到的资讯都包...
今晚参与了"MakerPro社群媒体平台"举办的 在MCU 上全面建构AI能力 ...
正文 今天要来安装Kubernetes上的NFS provisioner,主要就是搭配我的DS718...
php中阵列的写法 一般阵列 其索引值形式为从0开始往後累加的数字(0、1、2、3...) $fru...