Today, I learned about Python functions. The definition of python functions differs greatly from the definition of language functions I have learned before. next I will go to the topic directly. Today, I learned about Python functions. The definition of python functions differs greatly from the definition of language functions I have learned before. next I will go to the topic directly.
1. function definition
The function definition uses the keyword def. the specific syntax is as follows:
Def function name (parameter 1, parameter 2 ,......) :
Functions to be implemented
2. function call
Function name (parameter 1, parameter 2 ,......)
3. function parameters
This is basically the same as that in other languages. parameters include formal parameters and actual parameters. () (1) keyword parameters
The keyword parameter is to define the parameter, so as to avoid result errors caused by passing parameter errors.
(2) default parameter: a function that defines the default parameter, that is, the default parameter is output without a parameter. if a parameter is set, the default parameter is output.
(3) collection parameters: use
3. instance code
(1) no parameter function
Def MyFirstFunction (): print ("This is the first function I created") print ("My mood is excited") print ("\ n") MyFirstFunction ()
(2) functions with parameters
Def MySecondFunction (name): print ("My name is" + name) MySecondFunction ("YaoXiangxi") print ("\ n") def add (num1, num2 ): return (num1 + num2) print (add (3,4) print ("\ n ")
(3) keyword parameters
Def saySomething (name, word): print (name + "->" + word) saySomething ("turtle", "change programming world ") # print ("\ n") saySomething ("let programming change the world", "Little Turtle ") print ("\ n") # you can use the keyword parameter to avoid the above problem saySomething (word = "let programming change the world", name = "turtle") print ("\ n ")
(4) default parameters
Def saysomething (name = "", word = "let programming change the world"): print (name + "->" + word) saysomething ("YaoXiangxi ") saysomething (word = "programming makes life better") print ("\ n ")
(5) collection parameters
Def test (* parans): # get a new print function skill. if the printed data type does not conflict with each other # you can add a comma to separate the data and continue printing, print a print ("The parameter length is:", len (parans) print ("The second parameter is:", parans [1]) test, 5, 6)
The above is the content of the six Python functions for getting started with python. For more information, see The PHP Chinese website (www.php1.cn )!