程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python basics 2022 latest] lesson 5 functions

編輯:Python

【Python Basics 2022 newest 】 Lesson Five function

  • summary
  • function
    • Nonparametric functions
    • Parametric function
  • Parameters
    • Shape parameter
    • Actual parameters
  • Variable
    • local variable
    • Global variables

summary

Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .

function

function (Function) It is a code segment that can help us realize the functions we want . Functions can be reused , We can also customize functions .

Format :

def Function name ( Parameters 1, Parameters 2, Parameters 3...):
Function main body

Nonparametric functions

Example :

# Define nonparametric functions
def func():
print(" Hello, motherland ")
# Call function
func()

Output results :

 Hello, motherland

Parametric function

Example :

# Defined function
def num_compare(num1, num2):
# conditional , Compare the numbers
if num1 > num2:
print(" The first number is big ")
elif num1 == num2:
print(" The two numbers are the same ")
else:
print(" The second number is big ")
# Call function
num_compare(1, 2)

Output results :

 The second number is big

A better way to write :

# Defined function
def num_compare(num1, num2):
# Judge whether the incoming parameter is a number
if str(num1).isdigit() == False or str(num2).isdigit() == False:
print(" Parameter must be numeric ")
return # Jump out of function
# conditional , Compare the numbers
if num1 > num2:
print(" The first number is big ")
elif num1 == num2:
print(" The two numbers are the same ")
else:
print(" The second number is big ")
# Call function
num_compare("a", 2)
num_compare("c", "d")
num_compare(1, 2)

Output results :

 Parameter must be numeric
Parameter must be numeric
The second number is big

We convert the passed in parameters into strings (String), And then through the function isdigit() Judge whether it is a number , Avoid possible mistakes .

Parameters

Parameters (Parameter)

Python There are two categories of parameters in :

  • Formal parameters
  • The actual parameter

Shape parameter

Shape parameter ( Formal parameters ), Is in the function definition , Named parameters when .

Example :

# Defined function
def sum(num1, num2): # num1, num2 Is the formal parameter
# Return summation
return num1 + num2
# To get the results
total = sum(2, 3) # 2, 3 Is the actual parameter
print(total)

Output results :

5

Actual parameters

Actual parameters ( The actual parameter ) Is actually implemented is , Parameters passed to the function . A formal parameter is equivalent to a copy of an argument .

Example :

# Defined function
def multi(num1, num2): # num1, num2 Is the formal parameter
# Return product
return num1 *num2
# To get the results
total = multi(2, 3) # 2, 3 Is the actual parameter
print(total)

Output results :

6

Variable

local variable

local variable (Local Variable) Can only be used in the function where the variable is located . When we create local variables , A space will be temporarily allocated in memory , When the function finishes executing, the temporary space will be reclaimed .

Example :

# Defined function
def func():
# local variable
a = 10
b = 20
# Debug output
print(" Within the function ")
print(a)
print(b)
# Call function
func()
# Call variables outside the function ( Report errors )
print(" Out of function ")
print(a)
print(b)

Output results :

Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / Lesson Five function / Lesson Five local variable .py", line 17, in <module>
print(a)
NameError: name 'a' is not defined
Within the function
10
20
Out of function

Be careful : Local variables cannot be called outside a function

Global variables

Example :

# The variables defined outside the function are global variables
a = 10
# Defined function
def func():
# Use global Modifiers declare variables in functions
global b
b = 10
# Debug output
print(" Within the function ")
print(a)
print(b)
# Call function
func()
# Output results
print(" Out of function ")
print(a)
print(b)

Output results :

 Within the function
10
10
Out of function
10
10

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved