4. 傳遞實參的應用
5. 將函式儲存到模組中
def是用來定義函式的def
hello()
('hello world!'
)hello(
)
'hello』是函式名
'( )'中是用來放引數(形參和實參)的
':'之後的所有縮進行構成了函式體,指出了這個函式需要完成怎樣的工作
呼叫函式的時候,只需要傳遞相應引數和指出函式名即可
def
hello
(string)
(string)
s ='hello world'
hello(s)
def
hello
(string1, string2)
(string1 + string2)
hello(
'hello'
,'world!'
)> 輸出:
helloworld!
def
hello
(string1, string2)
(string1 + string2)
hello(string2=
'hello'
, string1=
'world!'
)> 輸出:
world!hello
def
hello
(string1, string2=
'world!'):
(string1 + string2)
hello(
'hello'
)> 輸出:
helloworld!
def
hello
(string1, string2=
'world!'):
(string1 + string2)
# 你好世界!
hello(
'hello'
)hello(string1=
'hello'
)# 我的手
hello(string1=
'my'
, string2=
'hand'
)hello(string2=
'hand'
, string1=
'my'
)hello(
'my'
,'hand'
)
def
function_name
(list_name[:]
)
def
make_pizza
(*toppings)
(toppings)
make_pizza(
'peperono'
)make_pizza(
'mushrooms'
,'green pepers'
,'extra cheese'
)> 輸出:
('peperono',)
('mushrooms'
,'green pepers'
,'extra cheese'
)
def
concat_name
(**name)
(name)
concat_name(student_0=
'marry'
)concat_name(student_1=
'john'
, student_2=
'mike'
)> 輸出:
匯入# pizza.py
defmake_pizza()
("this is a pizza.py!"
)
使用描述
import pizza
pizza.make_pizza()
匯入整個模組
from pizza import make_pizza
make_pizza()
匯入特定的函式
from pizza import make_pizza as mp
mp()
給匯入的函式指定別名
import pizza as p
p.make_pizza()
給匯入的模組指定別名
from pizza import *
make_pizza()
匯入模組中所有函式
Python學習(六) 函式
函式呼叫 要呼叫乙個函式,需要知道函式的名稱和引數 函式名其實就是指向乙個函式物件的引用,完全可以把函式名賦給乙個變數,相當於給這個函式起了乙個 別名 a abs 變數a指向abs函式 a 1 所以也可以通過a呼叫abs函式 1函式定義 在python中,定義乙個函式要使用def語句,依次寫出函式名...
python基礎學習六 模組
一 模組匯入 模組的匯入方式 最後一種方法,意味著匯入一切非私有的物件,但是會存在名稱衝突的情況,或者如果模組有乙個全域性的 all 變數,其中存放乙個名稱列表,就匯入名稱包含在 all 變數中的所有物件。為了避免衝突,很多指南中規定只能使用import importable方式。通常自定義模組名的...
python基礎學習 函式基礎
author feng lin date 2018 8 27 返回值的三種情況 一.沒有返回 返回none 不加 return 預設返回none return 執行到這裡跳出函式體 return none 不常用,沒意義 defmy return print my defmy return none...