Python 0 Basic introductory six Python functions

Source: Internet
Author: User
Today is mainly learning the Python function of the relevant knowledge, Python's function definition and previously learned language function definition there is a big difference, the following directly into the topic.
1. Definition of functions
The definition of a function uses the keyword DEF, the specific syntax:
def function name (parameter 1, parameter 2,...... ):
Functions to be implemented by the function
2. Invocation of functions
Function name (parameter 1, parameter 2,......)
3. Parameters of the function
This python is basically the same as other languages, parameters have formal parameters with the actual parameters, () (1) keyword parameters
The keyword parameter is the next definition of the parameter, which avoids errors due to passing arguments.
(2) Default parameter: A function that defines a default parameter, which is the output of the default parameter without a given parameter, and the parameter is the output parameter.
(3) Collect parameters: Use when you do not know how many parameters are available

3. Instance Code
(1) No parameter function

Def myfirstfunction (): Print ("     This is the first function I created") print ("     my mood is very exciting")     print ("\ n") myfirstfunction ()

(2) function with parameters

def mysecondfunction (name):    print ("My name is" +name) mysecondfunction ("Yaoxiangxi") print ("\ n") def Add (num1,num2):    return (num1+num2) print (3,4) print ("\ n")

(3) Keyword parameters

def saysomething (Name,word):     print (name+ "-+word) saysomething (" Little Turtle "," Let programming Change the World ") #万一函数的参数传递顺序错误, Result output error print ("\ n") saysomething ("Let programming Change the world", "Little Turtle") print ("\ n") #可以使用关键字参数避免上述问题saySomething (word= "Let programming Change the world", name= "Little Turtle") print ("\ n")

(4) Default parameters

def saysomething (name= "Little Turtle", word= "let programming Change the World"):     print (name+ "," +word) saysomething () saysomething ("Yaoxiangxi SaySomething (word= "Programming Makes Life Better") print ("\ n")

(5) Collecting parameters

def Test (* Parans):     #又get了print函数的新技能, if the printed data type does not conflict     #可以添加用逗号隔开继续打印, the equivalent of printing a tuple print     ("The length of the parameter is:", Len ( Parans)     print ("Second parameter is:", parans[1]) test (1,2,3,4,5,6)

The above is the Python 0 basic primer Six Python function content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.