What are the required parameters of python

The required parameters of the function refer to the parameters that must be passed in when the function is called

import math
def cal(n):return n * n
var=cal(2)print(var) # 4

The above function is to calculate the square of a number, when you want to calculate n^3, n^4.... Only two required parameters can be passed in

def cal_update(n,m):returnint(math.pow(n,m))var=cal_update(2,4)print(var) # 16

However, if most of the cases are square calculations, then cal_updtae(n,2) will be troublesome every time, so the default parameters are introduced

Default parameters, the incoming parameters by default

def cal_update2(n,m=2):returnint(math.pow(n,m))var=cal_update2(3) #Just pass in one parameter, 2print is passed in by default(var) # 9

Content expansion:

Python python function parameter: mandatory parameter, default parameter code example:

import math
# Required parameters of the function
''' The required parameters of the function refer to the parameters that must be passed in when the function is called
  '''
def cal(n):return n * n

var=cal(2)print(var) # 4'''The above function is to calculate the square of a number, when you want to calculate n^3, n^4....Only 2 required parameters can be passed in
 '''
def cal_update(n,m):returnint(math.pow(n,m))var=cal_update(2,4)print(var) # 16'''However, if most of the cases are square calculations, then cal will be used every time_updtae(n,2)It will be very troublesome, so the default parameters are introduced
 '''
# Default parameters of the function
''' Default parameters, the incoming parameters by default
 '''

def cal_update2(n,m=2):returnint(math.pow(n,m))var=cal_update2(3) #Just pass in one parameter, 2print is passed in by default(var) # 9

def student(name,sex,city='shanghai',age='20'):print('name:', name)print('sex:', sex)print('city:', city)print('age:', age)student('chris','male')

# name: chris
# sex: male
# city: shanghai
# age:20'''The default parameter must be an immutable object, if it is a variable object, problems may occur
  '''
def count(name=[]):
 name.append('chris')print(name)return name

count(name=['sarah','Tom'])
# [' sarah','Tom','chris']count();
# [' chris']count()
# [' chris','chris']When I call this function again, the default parameter name is not[],But it was not emptied last time['chris']'''The default parameter is a variable. It has been calculated when the function is defined. If there is a change, it will point to the new address
 '''

So far, this article on what are the mandatory parameters of python is introduced. For more related python mandatory parameters, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will get more Support ZaLou.Cn!

Recommended Posts

What are the required parameters of python
What are the advantages of python language
What is the use of Python
​What are the numbers in Python?
What is the scope of python variables
What is the id function of python
What is the prospect of python development
What is the function body of python
What is the function of adb in python
Consolidate the foundation of Python (4)
What are python class attributes
Consolidate the foundation of Python(7)
What is the advantage of python over corporate language
Consolidate the foundation of Python(6)
What are the methods for python to connect to mysql
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
What are the ways to open files in python
The usage of wheel in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
There are several versions of python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
What is the use of python's pip
Learn the basics of python interactive mode
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
Python implements the sum of fractional sequences
Two days of learning the basics of Python
Where is the pip path of python3
The essence of Python language: Itertools library
What are web development frameworks in python
The specific method of python instantiation object
python3 realizes the function of mask drawing
What kind of work can python do
The specific method of python import library
Solve the conflict of multiple versions of python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
7 features of Python3.9
Python realizes the development of student management system
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
What is the difference between python and pycharm
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very