欢迎光临
我们一直在努力

Python global关键字:别让局部变量偷走你的全局梦

函数参数类型默认值_python global关键字_Python函数定义调用

中的函数简介

用于其中的函数, 是被专门设计起来用于去执行特定任务的独立代码块, 它们对代码的可重用性、可读性以及组织性起到增强作用, 把对于函数的理解视为有效施行编程的关键所在。

函数在模块化编程中的作用

程序员借助函数能够把复杂问题划分成更小的、更便于管理的部分。这种采用模块化的方式, 不但让代码更易于被理解以及调试, 还能提升代码的重用性。

定义和调用函数

中的函数使用 def 关键字定义,后跟执行任务的代码块。

函数的语法

def function_name(parameters):
# 代码块

带参数和不带参数的函数调用

# 定义和调用函数的示例
def greet():
print("Hello, Python!")
greet() # 调用函数

调用函数

通过跟在名称之后的括号来对函数进行调用, 相应的参数会被传递到这些括号当中。

# 定义一个简单的函数
def greet(name):
print(f"Hello, {name}!")
# 带有参数的调用函数
greet("Alice") # 输出:Hello, Alice!

返回值的概念及其重要性

函数所产生的输出便是返回值, 在函数里头运用语句, 能够让函数把结果回馈给调用它的一方。

# 带参数和返回值的函数示例
def add(a, b):
return a + b
result = add(5, 3)
print(result) # 输出:8

形式参数和实际参数

重中之重是, 去理解形参跟实参二者之间存在的区别。形参, 它是于函数定义之时, 在括号内部所罗列出来的变量。然而实参, 它乃是于调用函数之际, 传递给函数的那个值。

参数类型

# 位置参数和关键字参数的示例
def describe_pet(animal, name):
print(f"I have a {animal} named {name}.")
describe_pet("hamster", "Harry") # 位置
describe_pet(name="Whiskers", animal="cat") # 关键字

默认参数值

将参数设定为默认值, 可凭借此允许以更少的参数去调用函数, 要是没有传递实际参数, 那么便会采用默认值。

# 带有默认参数值的函数
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
greet("Bob") # 使用默认问候语
greet("Bob", greeting="Hi") # 覆盖默认问候语

更多了解函数参数

让我们探索函数中参数的更多用法。

可变长度参数

# 使用 *args 和 **kwargs 的函数
def make_pizza(*toppings, **preferences):
print("Making a pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
if preferences:
print("Preferences:")
for pref, value in preferences.items():
print(f" {pref}: {value}")
make_pizza("pepperoni", "mushrooms", crust="thick", size="large")

变量的作用域和生命周期

于其中, 变量存有作用范围, 此情形抉择了它们于代码里的可被访问之处所。变量的生存期限所指向的是其在程序运行阶段存在的时间 。

局部变量与全局变量

理解函数内变量作用域

在函数之内所创建的变量, 也就是局部变量, 于该函数的外部它是不能够被访问的。与之相反的是, 成为全局变量的的那么一些变量, 在代码的任何一处地方都是能够被访问的。

# 演示局部和全局作用域的示例
x = "global" # 全局变量
def myFunc():
y = "local" # 局部变量
print(y)
myFunc()
print(x)

函数的示例

数据处理函数

函数可以有效处理和操纵数据,例如计算一系列数字的平均值。

# 计算平均值的函数
def average(numbers):
return sum(numbers) / len(numbers)
print(average([1, 2, 3, 4, 5])) # 输出:3.0

用户交互函数

创建与用户交互的函数,例如一个简单的计算器。

# 基本计算器的函数
def calculator(operation, a, b):
if operation == "add":
return a + b
elif operation == "subtract":
return a – b
# 根据需要添加更多操作
print(calculator("add", 5, 3)) # 输出:8

编写一个函数, 这个函数是(*args), 用以接受任意数量的字符串参数, 并且将这些字符串参数连接起来。

2. 构建一个名为r的函数, 该函数带有一个temp参数以及scale参数且其值为空字符串, 以使此函数能进行摄氏度与华氏度之间的温度转换。

结论

本文之中, 我们对函数展开了探讨, 着重阐述的是参数, 再者是参数类型以及实际应用。函数于有效构建代码这件事情上占据最关键的地位, 能促使代码变得更具可读性, 更加便于维护, 还更能够重新加以使用。

旅程的下一阶段, 我们要做的是, 把这些基本概念 , 运用到, 更具应用性的程序里。请期待!WwW.HoUniAoHaO.CoM/pose/21155.html
WwW.HoUniAoHaO.CoM/pose/21156.html
WwW.HoUniAoHaO.CoM/pose/21157.html
WwW.HoUniAoHaO.CoM/pose/21158.html
WwW.HoUniAoHaO.CoM/pose/21159.html
WwW.HoUniAoHaO.CoM/pose/21160.html
WwW.HoUniAoHaO.CoM/pose/21161.html
WwW.HoUniAoHaO.CoM/pose/21162.html
WwW.HoUniAoHaO.CoM/pose/21163.html
WwW.HoUniAoHaO.CoM/pose/21164.html
WwW.HoUniAoHaO.CoM/pose/21165.html
WwW.HoUniAoHaO.CoM/pose/21166.html
WwW.HoUniAoHaO.CoM/pose/21167.html
WwW.HoUniAoHaO.CoM/pose/21168.html
WwW.HoUniAoHaO.CoM/pose/21169.html
WwW.HoUniAoHaO.CoM/pose/21170.html
WwW.HoUniAoHaO.CoM/pose/21171.html
WwW.HoUniAoHaO.CoM/pose/21172.html
WwW.HoUniAoHaO.CoM/pose/21173.html
WwW.HoUniAoHaO.CoM/pose/21174.html
WwW.HoUniAoHaO.CoM/pose/21175.html
WwW.HoUniAoHaO.CoM/pose/21176.html
WwW.HoUniAoHaO.CoM/pose/21177.html
WwW.HoUniAoHaO.CoM/pose/21178.html
WwW.HoUniAoHaO.CoM/pose/21179.html
WwW.HoUniAoHaO.CoM/pose/21180.html
WwW.HoUniAoHaO.CoM/pose/21181.html
WwW.HoUniAoHaO.CoM/pose/21182.html
WwW.HoUniAoHaO.CoM/pose/21183.html
WwW.HoUniAoHaO.CoM/pose/21184.html
WwW.HoUniAoHaO.CoM/pose/21185.html
WwW.HoUniAoHaO.CoM/pose/21186.html
WwW.HoUniAoHaO.CoM/pose/21187.html
WwW.HoUniAoHaO.CoM/pose/21188.html
WwW.HoUniAoHaO.CoM/pose/21189.html
WwW.HoUniAoHaO.CoM/pose/21190.html
WwW.HoUniAoHaO.CoM/pose/21191.html
WwW.HoUniAoHaO.CoM/pose/21192.html
WwW.HoUniAoHaO.CoM/pose/21193.html
WwW.HoUniAoHaO.CoM/pose/21194.html
WwW.HoUniAoHaO.CoM/pose/21195.html
WwW.HoUniAoHaO.CoM/pose/21196.html
WwW.HoUniAoHaO.CoM/pose/21197.html
WwW.HoUniAoHaO.CoM/pose/21198.html
WwW.HoUniAoHaO.CoM/pose/21199.html
WwW.HoUniAoHaO.CoM/pose/21200.html
WwW.HoUniAoHaO.CoM/pose/21201.html
WwW.HoUniAoHaO.CoM/pose/21202.html
WwW.HoUniAoHaO.CoM/pose/21203.html
WwW.HoUniAoHaO.CoM/pose/21204.html
WwW.HoUniAoHaO.CoM/pose/21205.html
WwW.HoUniAoHaO.CoM/pose/21206.html
WwW.HoUniAoHaO.CoM/pose/21207.html
WwW.HoUniAoHaO.CoM/pose/21208.html
WwW.HoUniAoHaO.CoM/pose/21209.html
WwW.HoUniAoHaO.CoM/pose/21210.html
WwW.HoUniAoHaO.CoM/pose/21211.html
WwW.HoUniAoHaO.CoM/pose/21212.html
WwW.HoUniAoHaO.CoM/pose/21213.html
WwW.HoUniAoHaO.CoM/pose/21214.html
WwW.HoUniAoHaO.CoM/pose/21215.html
WwW.HoUniAoHaO.CoM/pose/21216.html
WwW.HoUniAoHaO.CoM/pose/21217.html
WwW.HoUniAoHaO.CoM/pose/21218.html
WwW.HoUniAoHaO.CoM/pose/21219.html
WwW.HoUniAoHaO.CoM/pose/21220.html
WwW.HoUniAoHaO.CoM/pose/21221.html
WwW.HoUniAoHaO.CoM/pose/21222.html
WwW.HoUniAoHaO.CoM/pose/21223.html
WwW.HoUniAoHaO.CoM/pose/21224.html
WwW.HoUniAoHaO.CoM/pose/21225.html
WwW.HoUniAoHaO.CoM/pose/21226.html
WwW.HoUniAoHaO.CoM/pose/21227.html
WwW.HoUniAoHaO.CoM/pose/21228.html
WwW.HoUniAoHaO.CoM/pose/21229.html
WwW.HoUniAoHaO.CoM/pose/21230.html
WwW.HoUniAoHaO.CoM/pose/21231.html
WwW.HoUniAoHaO.CoM/pose/21232.html
WwW.HoUniAoHaO.CoM/pose/21233.html
WwW.HoUniAoHaO.CoM/pose/21234.html
WwW.HoUniAoHaO.CoM/pose/21235.html
WwW.HoUniAoHaO.CoM/pose/21236.html
WwW.HoUniAoHaO.CoM/pose/21237.html
WwW.HoUniAoHaO.CoM/pose/21238.html
WwW.HoUniAoHaO.CoM/pose/21239.html
WwW.HoUniAoHaO.CoM/pose/21240.html
WwW.HoUniAoHaO.CoM/pose/21241.html
WwW.HoUniAoHaO.CoM/pose/21242.html
WwW.HoUniAoHaO.CoM/pose/21243.html
WwW.HoUniAoHaO.CoM/pose/21244.html
WwW.HoUniAoHaO.CoM/pose/21245.html
WwW.HoUniAoHaO.CoM/pose/21246.html
WwW.HoUniAoHaO.CoM/pose/21247.html
WwW.HoUniAoHaO.CoM/pose/21248.html
WwW.HoUniAoHaO.CoM/pose/21249.html
WwW.HoUniAoHaO.CoM/pose/21250.html
WwW.HoUniAoHaO.CoM/pose/21251.html
WwW.HoUniAoHaO.CoM/pose/21252.html
WwW.HoUniAoHaO.CoM/pose/21253.html
WwW.HoUniAoHaO.CoM/pose/21254.html
WwW.HoUniAoHaO.CoM/pose/21255.html
WwW.HoUniAoHaO.CoM/pose/21256.html
WwW.HoUniAoHaO.CoM/pose/21257.html
WwW.HoUniAoHaO.CoM/pose/21258.html
WwW.HoUniAoHaO.CoM/pose/21259.html
WwW.HoUniAoHaO.CoM/pose/21260.html
WwW.HoUniAoHaO.CoM/pose/21261.html
WwW.HoUniAoHaO.CoM/pose/21262.html
WwW.HoUniAoHaO.CoM/pose/21263.html
WwW.HoUniAoHaO.CoM/pose/21264.html
WwW.HoUniAoHaO.CoM/pose/21265.html
WwW.HoUniAoHaO.CoM/pose/21266.html
WwW.HoUniAoHaO.CoM/pose/21267.html
WwW.HoUniAoHaO.CoM/pose/21268.html
WwW.HoUniAoHaO.CoM/pose/21269.html
WwW.HoUniAoHaO.CoM/pose/21270.html
WwW.HoUniAoHaO.CoM/pose/21271.html
WwW.HoUniAoHaO.CoM/pose/21272.html
WwW.HoUniAoHaO.CoM/pose/21273.html
WwW.HoUniAoHaO.CoM/pose/21274.html
WwW.HoUniAoHaO.CoM/pose/21275.html
WwW.HoUniAoHaO.CoM/pose/21276.html
WwW.HoUniAoHaO.CoM/pose/21277.html
WwW.HoUniAoHaO.CoM/pose/21278.html
WwW.HoUniAoHaO.CoM/pose/21279.html
WwW.HoUniAoHaO.CoM/pose/21280.html
WwW.HoUniAoHaO.CoM/pose/21281.html
WwW.HoUniAoHaO.CoM/pose/21282.html
WwW.HoUniAoHaO.CoM/pose/21283.html
WwW.HoUniAoHaO.CoM/pose/21284.html
WwW.HoUniAoHaO.CoM/pose/21285.html
WwW.HoUniAoHaO.CoM/pose/21286.html
WwW.HoUniAoHaO.CoM/pose/21287.html
WwW.HoUniAoHaO.CoM/pose/21288.html
WwW.HoUniAoHaO.CoM/pose/21289.html
WwW.HoUniAoHaO.CoM/pose/21290.html
WwW.HoUniAoHaO.CoM/pose/21291.html
WwW.HoUniAoHaO.CoM/pose/21292.html
WwW.HoUniAoHaO.CoM/pose/21293.html
WwW.HoUniAoHaO.CoM/pose/21294.html
WwW.HoUniAoHaO.CoM/pose/21295.html
WwW.HoUniAoHaO.CoM/pose/21296.html
WwW.HoUniAoHaO.CoM/pose/21297.html
WwW.HoUniAoHaO.CoM/pose/21298.html
WwW.HoUniAoHaO.CoM/pose/21299.html
WwW.HoUniAoHaO.CoM/pose/21300.html
WwW.HoUniAoHaO.CoM/pose/21301.html
WwW.HoUniAoHaO.CoM/pose/21302.html
WwW.HoUniAoHaO.CoM/pose/21303.html
WwW.HoUniAoHaO.CoM/pose/21304.html
WwW.HoUniAoHaO.CoM/pose/21305.html
WwW.HoUniAoHaO.CoM/pose/21306.html
WwW.HoUniAoHaO.CoM/pose/21307.html
WwW.HoUniAoHaO.CoM/pose/21308.html
WwW.HoUniAoHaO.CoM/pose/21309.html
WwW.HoUniAoHaO.CoM/pose/21310.html
WwW.HoUniAoHaO.CoM/pose/21311.html
WwW.HoUniAoHaO.CoM/pose/21312.html
WwW.HoUniAoHaO.CoM/pose/21313.html
WwW.HoUniAoHaO.CoM/pose/21314.html
WwW.HoUniAoHaO.CoM/pose/21315.html
WwW.HoUniAoHaO.CoM/pose/21316.html
WwW.HoUniAoHaO.CoM/pose/21317.html
WwW.HoUniAoHaO.CoM/pose/21318.html
WwW.HoUniAoHaO.CoM/pose/21319.html
WwW.HoUniAoHaO.CoM/pose/21320.html
WwW.HoUniAoHaO.CoM/pose/21321.html
WwW.HoUniAoHaO.CoM/pose/21322.html
WwW.HoUniAoHaO.CoM/pose/21323.html
WwW.HoUniAoHaO.CoM/pose/21324.html
WwW.HoUniAoHaO.CoM/pose/21325.html
WwW.HoUniAoHaO.CoM/pose/21326.html
WwW.HoUniAoHaO.CoM/pose/21327.html
WwW.HoUniAoHaO.CoM/pose/21328.html
WwW.HoUniAoHaO.CoM/pose/21329.html
WwW.HoUniAoHaO.CoM/pose/21330.html
WwW.HoUniAoHaO.CoM/pose/21331.html
WwW.HoUniAoHaO.CoM/pose/21332.html
WwW.HoUniAoHaO.CoM/pose/21333.html
WwW.HoUniAoHaO.CoM/pose/21334.html
WwW.HoUniAoHaO.CoM/pose/21335.html
WwW.HoUniAoHaO.CoM/pose/21336.html
WwW.HoUniAoHaO.CoM/pose/21337.html
WwW.HoUniAoHaO.CoM/pose/21338.html
WwW.HoUniAoHaO.CoM/pose/21339.html
WwW.HoUniAoHaO.CoM/pose/21340.html
WwW.HoUniAoHaO.CoM/pose/21341.html
WwW.HoUniAoHaO.CoM/pose/21342.html
WwW.HoUniAoHaO.CoM/pose/21343.html
WwW.HoUniAoHaO.CoM/pose/21344.html
WwW.HoUniAoHaO.CoM/pose/21345.html
WwW.HoUniAoHaO.CoM/pose/21346.html
WwW.HoUniAoHaO.CoM/pose/21347.html
WwW.HoUniAoHaO.CoM/pose/21348.html
WwW.HoUniAoHaO.CoM/pose/21349.html
WwW.HoUniAoHaO.CoM/pose/21350.html
WwW.HoUniAoHaO.CoM/pose/21351.html
WwW.HoUniAoHaO.CoM/pose/21352.html
WwW.HoUniAoHaO.CoM/pose/21353.html
WwW.HoUniAoHaO.CoM/pose/21354.html
WwW.HoUniAoHaO.CoM/pose/21355.html
WwW.HoUniAoHaO.CoM/pose/21356.html
WwW.HoUniAoHaO.CoM/pose/21357.html
WwW.HoUniAoHaO.CoM/pose/21358.html
WwW.HoUniAoHaO.CoM/pose/21359.html
WwW.HoUniAoHaO.CoM/pose/21360.html
WwW.HoUniAoHaO.CoM/pose/21361.html
WwW.HoUniAoHaO.CoM/pose/21362.html
WwW.HoUniAoHaO.CoM/pose/21363.html
WwW.HoUniAoHaO.CoM/pose/21364.html
WwW.HoUniAoHaO.CoM/pose/21365.html
WwW.HoUniAoHaO.CoM/pose/21366.html
WwW.HoUniAoHaO.CoM/pose/21367.html
WwW.HoUniAoHaO.CoM/pose/21368.html
WwW.HoUniAoHaO.CoM/pose/21369.html
WwW.HoUniAoHaO.CoM/pose/21370.html
WwW.HoUniAoHaO.CoM/pose/21371.html
WwW.HoUniAoHaO.CoM/pose/21372.html
WwW.HoUniAoHaO.CoM/pose/21373.html
WwW.HoUniAoHaO.CoM/pose/21374.html
WwW.HoUniAoHaO.CoM/pose/21375.html
WwW.HoUniAoHaO.CoM/pose/21376.html
WwW.HoUniAoHaO.CoM/pose/21377.html
WwW.HoUniAoHaO.CoM/pose/21378.html
WwW.HoUniAoHaO.CoM/pose/21379.html
WwW.HoUniAoHaO.CoM/pose/21380.html
WwW.HoUniAoHaO.CoM/pose/21381.html
WwW.HoUniAoHaO.CoM/pose/21382.html
WwW.HoUniAoHaO.CoM/pose/21383.html
WwW.HoUniAoHaO.CoM/pose/21384.html
WwW.HoUniAoHaO.CoM/pose/21385.html
WwW.HoUniAoHaO.CoM/pose/21386.html
WwW.HoUniAoHaO.CoM/pose/21387.html
WwW.HoUniAoHaO.CoM/pose/21388.html
WwW.HoUniAoHaO.CoM/pose/21389.html
WwW.HoUniAoHaO.CoM/pose/21390.html
WwW.HoUniAoHaO.CoM/pose/21391.html
WwW.HoUniAoHaO.CoM/pose/21392.html
WwW.HoUniAoHaO.CoM/pose/21393.html
WwW.HoUniAoHaO.CoM/pose/21394.html
WwW.HoUniAoHaO.CoM/pose/21395.html
WwW.HoUniAoHaO.CoM/pose/21396.html
WwW.HoUniAoHaO.CoM/pose/21397.html
WwW.HoUniAoHaO.CoM/pose/21398.html
WwW.HoUniAoHaO.CoM/pose/21399.html
WwW.HoUniAoHaO.CoM/pose/21400.html
WwW.HoUniAoHaO.CoM/pose/21401.html
WwW.HoUniAoHaO.CoM/pose/21402.html
WwW.HoUniAoHaO.CoM/pose/21403.html
WwW.HoUniAoHaO.CoM/pose/21404.html
WwW.HoUniAoHaO.CoM/pose/21405.html
WwW.HoUniAoHaO.CoM/pose/21406.html
WwW.HoUniAoHaO.CoM/pose/21407.html
WwW.HoUniAoHaO.CoM/pose/21408.html
WwW.HoUniAoHaO.CoM/pose/21409.html
WwW.HoUniAoHaO.CoM/pose/21410.html
WwW.HoUniAoHaO.CoM/pose/21411.html
WwW.HoUniAoHaO.CoM/pose/21412.html
WwW.HoUniAoHaO.CoM/pose/21413.html
WwW.HoUniAoHaO.CoM/pose/21414.html
WwW.HoUniAoHaO.CoM/pose/21415.html
WwW.HoUniAoHaO.CoM/pose/21416.html
WwW.HoUniAoHaO.CoM/pose/21417.html
WwW.HoUniAoHaO.CoM/pose/21418.html
WwW.HoUniAoHaO.CoM/pose/21419.html
WwW.HoUniAoHaO.CoM/pose/21420.html
WwW.HoUniAoHaO.CoM/pose/21421.html
WwW.HoUniAoHaO.CoM/pose/21422.html
WwW.HoUniAoHaO.CoM/pose/21423.html
WwW.HoUniAoHaO.CoM/pose/21424.html
WwW.HoUniAoHaO.CoM/pose/21425.html
WwW.HoUniAoHaO.CoM/pose/21426.html
WwW.HoUniAoHaO.CoM/pose/21427.html
WwW.HoUniAoHaO.CoM/pose/21428.html
WwW.HoUniAoHaO.CoM/pose/21429.html
WwW.HoUniAoHaO.CoM/pose/21430.html
WwW.HoUniAoHaO.CoM/pose/21431.html
WwW.HoUniAoHaO.CoM/pose/21432.html
WwW.HoUniAoHaO.CoM/pose/21433.html
WwW.HoUniAoHaO.CoM/pose/21434.html
WwW.HoUniAoHaO.CoM/pose/21435.html
WwW.HoUniAoHaO.CoM/pose/21436.html
WwW.HoUniAoHaO.CoM/pose/21437.html
WwW.HoUniAoHaO.CoM/pose/21438.html
WwW.HoUniAoHaO.CoM/pose/21439.html
WwW.HoUniAoHaO.CoM/pose/21440.html
WwW.HoUniAoHaO.CoM/pose/21441.html
WwW.HoUniAoHaO.CoM/pose/21442.html
WwW.HoUniAoHaO.CoM/pose/21443.html
WwW.HoUniAoHaO.CoM/pose/21444.html
WwW.HoUniAoHaO.CoM/pose/21445.html
WwW.HoUniAoHaO.CoM/pose/21446.html
WwW.HoUniAoHaO.CoM/pose/21447.html
WwW.HoUniAoHaO.CoM/pose/21448.html
WwW.HoUniAoHaO.CoM/pose/21449.html

赞(0)
未经允许不得转载:171主机测评 » Python global关键字:别让局部变量偷走你的全局梦
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址