# Defining a function
def myFunction():
a = 1 + 2
print(f"sum is {a}")
myFunction() # Calling a function
上面我们定义了一个名为myFunction的函数,函数没有接受参数,所以括号为空,紧接着的是函数的功能代码。前4行只是定义了函数,并没有执行函数。之后我们输入myFunction()调用函数,注意这里调用函数的括号不能省略。调用函数后输出结果是 sum is 3。如果以后想要再调用此函数,只需要再调用myFunction() 就好了。
参数传递和返回值
def function_name(parameters):
expressions
parameters的位置就是函数的参数,在调用的时候传入即可。
# Positional Arguments
def describe_person(name, age):
print(f'{name} is {age} years old.')
describe_person('enoch', 25)
# Multiple Function Calls
describe_person('enoch', 23)
describe_person('enoch', 26)
# Order matters in positional arguments
describe_person('enoch', 25)
# Order doesn't matter if you specify keyword arguments
describe_person(age=25, name='enoch')
# Default values
def describe_pet(pet_name, animal_type='dog'):
print(f'{pet_name} is a {animal_type}')
describe_pet('doudou')
describe_pet('duoduo', 'cat')
除了基本的数据类型,我们也可以将列表等其他数据类型当做参数传入:
fruits = ["a", "b", "c"]
def myFun(food):
for x in food:
print(food)
# Modifying a List in a Function
def set_to_one(input):
for i in range(0, len(input)):
input[i] = 1
number_list = [1, 2, 3, 4, 5]
set_to_one(number_list)
print(number_list)
# Preventing a Function from modifying a list
number_list2 = [1, 2, 3, 4, 5]
set_to_one(number_list2[:])
print(number_list2)
# Returning a simple value
def get_formatted_name(first_name, last_name):
return f'{last_name.title()} {first_name.title()}'
print(get_formatted_name('hao', 'zheng'))
使用条件判断,我们可以根据不同参数返回不同的内容:
# Making an argument optional
def proposal(name, age):
if age >= 18:
return f'{name}, I want to marry you.'
else:
return f'{name}, I can\'t marry you right now'
print(proposal('Emily', 16))
print(proposal('Emily', 20))
返回一个字典:
# Returning a dictionary
def build_person(first_name, last_name):
person = {'first': first_name, 'last': last_name}
return person
musician = build_person('hao', 'zheng')
print(musician['first'] + ' ' + musician['last'])
def make_pizza(size, type):
print(f"A {size} inch pizza with {type} is ready for you.")
如果我们想在其他文件调用pizza.py中的函数时,使用import语句就好了:
# Importing an Entire Module (pizza.py)
import pizza
pizza.make_pizza(16, 'pepperoni')
# Using as to Give a Module an Alias
import pizza as p
p.make_pizza(16, 'cheese')
# Importing Specific Functions
from pizza import make_pizza
make_pizza(16, 'cheese')
# Using as to Give a Function an Alias
from pizza import make_pizza as mp
mp(16, 'cheese')
# Importing All Functions in a Module
from pizza import *
make_pizza(16, 'green peppers')
This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.
Recent Tweets
Newsletter
Sign-up to get the latest news and update information. Don't worry, we won't send spam!
[contact-form-7 id="3145" title="Sliding Bar Form"]