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) 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
Example :
# Define nonparametric functions
def func():
print(" Hello, motherland ")
# Call function
func()
Output results :
Hello, motherland
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 (Parameter)
Python There are two categories of parameters in :
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 ( 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
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
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