Define a function that asks for the root of the two-yuan one-time equation
# sublime Text import math def EE (a,b,c): Delta =b*b-4*a*c if delta< 0: return
" null " else : M =math.sqrt (delta) NX = (-b+m)/2/
a ny
= (-b-m)/2/a return nx,ny
# Python interpreter from The import ee # declaration calls the function defined in the PY file ee>>> ee (1,0,9)'null '>>> ee (1,0,-9) (3.0,-3.0)
Second, the parameters of the function
There are 5 types of parameters for the function: positional, default, variable, keyword, named keyword
Position parameters:
def POW (x): return x*x # x squared def Pow (x,n) :if n==0 : return 1 if n==1: return x return X*pow (x,n-1) # to find an arbitrary x-square
Default parameters:
# default x Squared def Pow (x,n=2): if n==0: return 1 if n==1 : return x return x*pow (x,n-1) from Import pow>>> pow (5)25>>> pow (5,3)125>>> Pow (5 , 0) 1
#Kindergarten freshman class, default freshman age is 6, city is ZZdefSign_in (name,gender,age=6,city='ZZ'): Print(name,gender,age,city)>>> sign_in ('Jiaxinwei',' Boy') Jiaxinwei Boy6ZZ>>> sign_in ('Jiaxinwei',' Boy', city='NY') Jiaxinwei Boy6 NY
Use default parameters a very large pit:
Explanation: When the Python function is defined, the value of the default parameter L is computed, that is, [] because the default parameter, L, is also a variable, which points to the object [], each time the function is called, if the content of L is changed, the contents of the default parameter will change the next time the function definition is no longer [].
Defining default parameters Keep in mind that the default parameter must point to the immutable object (TUPLE,NONE,STR)!!!
Variable parameters
Variable parameter is the name of the passed parameter is variable, can be any number of
def Calc (*nums): sum=0 for in nums: sum=sum+n* N return sum>>> a=[1,2,3,4]>>> Calc (*a)30>>> calc (1,2,3,4 )30>>> Calc (a)
Keyword parameters
def person (name,age,**kw): print('name:', name,' Age:', age,'other:', kw)# Where Name,age is a required parameter, KW is the keyword parameter. When you call this function, you can pass in only the required parameters, or you can pass in any of the keyword parameters, and the keyword parameters are automatically assembled into a dict inside the function.
>>> fromHelloImport Person>>> person ('Jiaxinwei', 8)#1Name:jiaxinwei Age:8Other : {}>>> person ('Jiaxinwei', 8,city='NY', gender='M')#2Name:jiaxinwei Age:8 Other: {' City':'NY','Gender':'M'}>>> extra={' City':'Beijing','Job':'Student'}#3>>> person ('Jack', 24,**extra) Name:jack Age:Other: {' City':'Beijing','Job':'Student'}
The role of keyword parameters: Is that you are doing a user registration function, in addition to user name and age is required, the other is optional, using the keyword parameters to define this function can meet the requirements of registration.
Named keyword parameters
If you want to restrict the name of the keyword argument, you can use the named keyword argument (not quite understood)
def person (name, age, *, City, Job): print(name, age, City, Job)# * Number preceded by the required parameter, followed by the named keyword parameter
# If there is already a mutable parameter in the function definition, then the named keyword argument will no longer need a delimiter. def person (name,age,*args,city,job): print(name,age,args,city,job)
# A named keyword function can have a default value def person (name,age,*,city='Beijing', job='student' ): print(name,age,city,job)fromimport person >>> person ('jiaxinwei',Beijing student
It's not yet known when the named keyword function is used, first mark, and then the instance.
Parameter combinations
Define functions in Python, you can mix parameters, but the order of the parameters must be: Required, default, variable, named keyword, keyword.
So for any function, it can be called by the form of a Func (*ARGS,**KW), regardless of how its arguments are defined.
Although you can combine up to five parameters, do not use too many combinations at the same time, otherwise the function interfaces are poorly understandable.
Write a program: receive an arbitrary majority and calculate the product
def product (*nums): if len (nums) ==0:sum =0 /span>else : Sum =1 for x in nums:sum *=x return sum >>> from hello import product >>> product () 0 >>> product (5< Span style= "COLOR: #000000" >) 5>>> product (5,6,7 210>> > Product (5,6,7,9 1890
Python basic notes: functions: Calling functions, defining functions, parameters of functions, recursive functions