Code is not standard, two lines of tears to work
function
Functions can be understood as a tool (module), just like the random function, input function and Type function that generate random numbers learned above. Their existence is just like tools. The emergence of functions can solve the problem of code redundancy and repeated code writing, and codes can be used at any time when defined as functions.
- It’s official: a function is an organized, reusable piece of code that implements a single, or related, function.
Definition of a function
- Function names can consist of underscores, letters, and numbers
- Cannot have the same name as the keyword
- Cannot start with a number
- “Def” is the full name of define
- The function is defined bydefAt the beginning of a keyword, for example
Def function name ():
I can define a function
# Function definition
#def ():
Duplicate code is required
# Use of functions
Function name with parentheses ()
# def Say(): defines a function called Say with the def keyword
def Say() :
Print Hello define three times
print('hello define')
print('hello define')
print('hello define')
# Say() means to use this function, usually the function name + a parenthesis
Say()
Copy the code
- After seeing this, some students might ask, why not just use loops?
- A: Although loops can solve the problem of repetition, but not flexible enough, functions have functions, functions only need to be defined once can be reused
# Function definition
# def Say(): defines a function called Say with the def keyword
def Say() :
Print Hello define three times
print('hello define')
print('hello define')
print('hello define')
# Say() means to use this function, usually the function name + a parenthesis
Say()
Say()
Say()
Copy the code
- Functions can not be used first and then defined, generally defined first and then used
# Say() means to use this function, usually the function name + a parenthesis
Say()
# def Say(): defines a function called Say with the def keyword
def Say() :
Print Hello define three times
print('hello define')
print('hello define')
print('hello define')
Copy the code
- NameError: name ‘Say’ is not defined (function named Say is not defined)
Order of function:
- Parameter reception
- Parameter: an unknown quantity similar to mathematics that can receive arbitrary data
- Argument: A specific value
- Define a summation function
- The beauty of functions is that when code is defined as a function, it can be used repeatedly through function calls
The function that defines a Sum by the def keyword is called Sum and takes two parameters, named a and b
def Sum(a,b) :
Output a + b #
print(a+b)
Sum(10,20) = Sum(10,20) = Sum(10,20) = Sum(10,20) = Sum(10,20) = Sum(10,20
Sum(10.20)
Sum(5.20)
Sum(2.20)
Copy the code
- Return of results
- Inside a function there is a keyword called return, which is intended to send the result externally
- Why do I need to return? For example, the Sum function, after the internal calculation, needs to return the result of the second operation
The function that defines a Sum by the def keyword is called Sum and takes two parameters, named a and b
def Sum(a,b) :
Return the sum of a + b
return a + b
Sum1 = Sum(10,20); sum1 = Sum(10,20)
sum1 = Sum(10.20)
sum2 = Sum(5.20)
sum3 = Sum(2.20)
Add or subtract the returned result. This is what return does
sum4 = sum1 + sum2 - sum3
print(sum4)
Copy the code
The placeholder %s
- %d is a placeholder for formatting numbers and %s is a formatting string
- In the same way %d is used, if you forget, refer back to Python Learning Notes 02
Function advanced case: Huaqiang buy melon
- No case can be said, take the previous written huaqiang buy melon case using function optimization
- A definition of multiple use, flexible
- Case knowledge
- Input function, def function definition, %s placeholder (string placeholder)
def sell(Name,Say) :
print("% s: % s"%(Name,Say))
sell("Passers-by"."A man came to buy melons.")
sell("Liu Huaqiang"."Dude, how much is this melon a kilo?")
sell("Melon stall owner.".input("Reply price :"))
sell("Liu Huaqiang"."Is the skin of the melon made of gold or is the seed of the melon made of gold?")
sell("Melon stall owner."."Look where there are melons now, these are greenhouse melons, you detest expensive I detest expensive.")
sell("Liu Huaqiang"."Pick one for me.")
sell("Melon stall owner."."All right, how about this melon?")
sell("Liu Huaqiang"."Is this melon ripe?")
sell("Melon stall owner."."I open fruit stall, can sell you raw melon eggs?")
sell("Liu Huaqiang"."Let me ask you, is this melon ripe?")
sell("Melon stall owner.".'You're picking on me, aren't you, are you?')
sell("Liu Huaqiang"."If your melon is ripe, I will. What if it is not ripe?")
sell("Melon stall owner."."If not, I'll eat it myself. Is that all right?")
sell("Melon stall owner."."15 pounds, 30 bucks.")
sell("Liu Huaqiang"."That's 15 pounds. There's something wrong with that scale.")
sell("Melon stall owner."."You tm deliberately find fault son is not, you don't want, you don't want!")
sell("Liu Huaqiang"."Magnet")
sell("Liu Huaqiang"."And as you said, if this melon is raw, you swallow it.")
sell("Melon stall owner."."You hack Japanese melons, I...")
sell("Passers-by"."Sargeron, Sargeron!)
Copy the code
The module
- Functions can also be called modules
- When a function is in the current file, it is called with parentheses, but when other files use the file, you need to import the function you have written using the import keyword
- For example, importing the name of the file in which your function is located can be used in other files
Module case
- Step 1: Create a New Python file called Calculation and write the following code into it (don’t copy, write it by hand)
# addition
def addition(a,b) :
return a + b
# subtraction
def subtraction(a,b) :
return a - b
# multiplication
def multiplication(a,b) :
return a * b
# division
def division(a,b) :
return a * b
Copy the code
- Step 2: Create a new test file
Import the newly created calculation file through import
import calculation
The file name calculation points to the function you need to call, so you can call the function you wrote in another file
For example, calculation. Addition (10,20) means that the sum function in the calculation file is called through calculation, and 10 and 20 are passed, and a sum variable is defined to receive the value returned by addition
sum = calculation.addition(10.20)
print(sum)
Copy the code