Six Python functions

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.