用書 PYTHON王者歸來 作者洪錦魁
def hello(name):
print("Hi",name,"How are you")
hello("nini")
hello("Kiki")
執行結果
參數的傳遞
def math_01(a,b):
result = a - b
print(result)
a = eval(input("x:"))
b = eval(input("y:"))
print("x-y=")
math_01(a,b)
執行結果
def classA(name ,number):
print("My name is:", name)
print("My number is:", number )
print()
a = str(input("Name:"))
b = eval(input("Number:"))
classA(a,b)
執行結果
關鍵字參數
def classA(name ,number):
print(f"My name is: {name}")
print(f"My number is: {number}" )
print()
classA(name="Nini",number=9)
classA(number=9,name="Nini")
執行結果
參數預設值
- 必須放在參數右邊
- 位址不可對調
def classA(name ,number=9):
print(f"My name is: {name}")
print(f"{name},My number is: {number}" )
print()
classA(name="Nini",number=6)
classA(name="Nini")
classA(number=5,name="Nini",)
執行結果
函數傳回值(None)
def classA(name ,number=9):
print(f"My name is: {name}")
return #這邊若省略return結果也是相同
value = classA("nini")
print(value)
print(type(value))
執行結果
Value =None #false
if Value:
print("1")
else:
print("2")
print("1"if Value else"2")
執行結果
def is_none(str,x):
if x is None:
print(f"{str}=None")
elif x:
print(f"{str}=True")
else:
print(f"{str}=False")
is_none("[]",[])
is_none("()",())
is_none("{ }",{})
is_none("空集合",set())
is_none("None",None)
is_none("True",True)
is_none("False",False)
執行結果
return result
def add(x,y):
result = x + y
return result
a = int(input("a="))
b = int(input("b="))
print("a+b=",add(a,b))
執行結果
ans= int(input("加法按1.減法按2.乘法按3.除法按4:"))
a = int(input("a="))
b = int(input("b="))
def add(x,y):
return x + y
def subtract(x,y):
return x - y
def multiply(x,y):
return x * y
def divide(x,y):
return x / y
if ans ==1:
print("a+b=",add(a,b))
elif ans ==2:
print("a-b=",subtract(a,b))
elif ans ==3:
print("a*b=",multiply(a,b))
elif ans ==4:
print("a/b=",divide(a,b))
else:
print("error")
執行結果
return Tuple
def math1(x,y):
add= x + y
subtract= x - y
multiply= x * y
divide= x / y
return add,subtract,multiply,divide
x=6
y=1
add,subtract,multiply,divide = math1(x,y)
print(add)
print(subtract)
print(multiply)
print(divide)
print(type(math1))
print(type(math1(x,y)))
執行結果
回傳字串資料和參數的預設值
def Classmate(fristname,lastname,grade,middlename =""):#沒有中間名
if grade <= 60:
word = fristname +" " + middlename + " " +lastname + " " + "Bad"
else:
word = fristname + " " + middlename + " " + lastname + " " + "Good"
return word
A = Classmate("Kiki","lika",60,"m")
B = Classmate("Nii","lika",100,"k")
C = Classmate("RURU","Bi",80)#沒有中間名
print(A)
print(B)
print(C)
執行結果
函數回傳字典資料
def classnumeber(name,number,grade=""):
classdict ={"name":name,"number":number}
if grade:
classdict["grade"] =grade
return classdict
classmateA = classnumeber("A",6)
classmateB = classnumeber("B",7,100)
print(classmateA)
print(classmateB)
執行結果
迴圈應用在建立字典
def classnumeber(name,number,grade=""):
classdict ={"name":name,"number":number}
if grade:
classdict["grade"] =grade
return classdict
while True:
print("建立班級系統")
name = input("你的名字:")
number = input("學號:")
grade = input("成績(如果沒有成績請按ENTER鍵):")
classmate = classnumeber(name,number,grade)
print(classmate,"\n")
again = input("要再繼續輸入資料請按y,不輸入請按任意鍵離開:")
if again !=("y"):
break
print("再見")
執行結果