[introduction to python to mastery] let you thoroughly understand the functions of python

Posted by ridgedale on Tue, 21 Dec 2021 00:44:50 +0100

🚀 Author: "big data Zen" 🚀 Welcome to praise 👍, Collection ⭐, Leaving a message. 💬

catalogue

Functions and their calls in Python

Understanding of functions:

Functions are organized, reusable snippets of code used to implement a single, or associated function.

Function can improve the modularity of application and the reuse rate of code.

For example, when we calculate the area of a graph, we know that the formula for calculating the area of a circle is  πr²

At this time, we can abstract such a function into a function

We are no stranger to function calls. We've been in contact before print(),hex()These are functions

The function call is very simple, as long as the function name(parameter)

One thing to note here: the parameters must conform to the input parameters in the function definition Python The functions provided in can be used help(function

name)To view the relevant instructions for related calls, such as   help(hex)See some descriptions of the function

stay Python In, everything is an object, which means that the function name is also a reference to a function object. We can assign the function name to a function object

Variable, which is equivalent to giving this function an "alias". It is as follows:

def run():
  pass 
  return (Returns a numeric value that can make a list, number)
run()

Custom functions in python

Describes how to define your own functions

### Defines the basic structure of the function

#### def function name (input parameter):

#### Functional logic

#### return

Examples are as follows:

PI = 3.14
def circle_area(r):
 return PI * r **2
print(circle_area(2))
>>>12.56

a=4
def rum(r):
   return a+r
print(rum(2))
>>>6

Custom empty function

def empty_fun():
pass
print(empty_fun())

Python features allow functions to return multiple values

Introduction: describes how to make a function return multiple values

Directly put multiple return values into list and tuple in a certain order in the function. Examples are as follows:

def my_fun():
 return [1, 2, 3]
print(my_fun())
>>>[1,2,3]

Multiple return values are placed in the dictionary (dict) in the form of key value

def my_fun():
return {"x": 1, "y": 2, "z": 3}
print(my_fun())
>>>"x": 1, "y": 2, "z": 3
 
def my_fun():
 return {"x": 1, "y": 2, "z": 3}
my_fun()
>>>This call returns a null value

def my_fun():
 return 4
print(my_fun()+2)
>>>6

Multiple comma separated values can be directly returned when returning. You can also directly receive multiple variables when returning:

def my_fun():
return val1, val2, val3
x, y, z = my_fun()

#Receive multiple return values
def my_fun():
return 1,2,3
x, y, z = my_fun()
print(x)
print(y)
print(y)

>>>
1
2
3`Insert the code slice here`

def my_fun():
 return 1,2,3
a=my_fun()
i=len(a)   #3
for j in range(i):
    print(a[j])
>>>
1
2
3

Key points:

When multiple parameters are returned, if you want to receive multiple variables at one time, you must receive as many variables as the number of return values.

In fact, multiple values are returned, essentially a tuple:

def my_fun():
return 1, 2, 3
print((my_fun()))
print(type(my_fun()))
>>>(1,2,3)
>>> <class 'tuple'>

Practical part: recursive algorithm of function core knowledge

What is recursion in one sentence?

To understand what recursion is, you must first understand what recursion is

What? incomprehension? Never mind. Let me tell you a story

#Once upon a time, there was a mountain and a temple in the mountain. There was a monk in the temple. The monk was telling a story. The story was - once upon a time, there was a mountain and a temple in the mountain

Temple, there is a monk in the temple. The monk is telling a story. The story is - once upon a time, there was a mountain In the final analysis, recursion is the point you pay special attention to when calling yourself: when you write a recursive function, the first step is to write the function first

The final end condition, such as the following if n==1.

Example part, using recursion to do factorial operation 4! 4*3 *2 *1=24

Recursively, n*f(3)    n*f(3)*f(2)....
def factorial(n):
 if n == 1:
  return 1
 else:
  return n * factorial(n-1)
print(factorial(4))
>>>24

Using recursive implementation to find the value of the nth item of Fibonacci sequence

'''Fibonacci sequence( Fibonacci sequence),It refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21
34,......Mathematically, the Fibonacci sequence is defined recursively as follows: F(1)=1,F(2)=1, F(n)=F(n-1)+F(n2)(n>=3,n∈N*)'''

python has the largest recursive level, which can be obtained by using the following code:

import sys
print(sys.getrecursionlimit())   Take his maximum recursion depth
 Output result: 1000

Find the value of the nth item of Fibonacci sequence using recursive implementation:

def feb(n):
 if n <= 2:
 return 1
else:
 return feb(n - 1) + feb(n - 2)
print(feb(5))
>>>
5
 Derivation process:
f5 | f4+f3 |f3+f2+f2+f1|f2+f1+f2+f2+f1

This is the end of the python function tutorial in this chapter. I hope it will be helpful to you.