Python Basic Operations _ Method (function)

Source: Internet
Author: User

 #函数, Method 
#普通方法
def hello ():
print (' hello ')
Hello ()
#带形参的方法
def hello1 (name):
Print (' hello%s '%name)
Hello1 (' Brad Pitt ')
#多个参数的方法
def Hello2 (name1,name2):
Print (' hello%s and%s '% (name1,name2))
Hello2 (' Brad Pitt ', ' Dirk ')
#带默认值参数的方法
def hello3 (name1,name2,name3= ' Ferrari '):
print (' hello%s%s%s '% (name1, Name2,name3))
Hello3 (' Pete ', ' Dirk ',)
#可变参数的方法
def Hello4 (*args):
Print (args)
Hello4 (1,2,3,4)
# Keyword parameter, key v accepted in is a dictionary
def hello5 (**kwargs):
Print (Kwargs)
Hello5 (name= ' Qiao ', age=18)
a={' name ': ' Q ', ' Age ': '
Hello5 (**a)

#关键字传值
#关键字调用参数不用排序. Because the keyword
def hello6 (name,age) is specified:
Print (name,age)
Hello6 (age=1,name= ' Qiao ')
#函数 The return value of the method
Def plus (NUM1, NUM2):
Return num1,num2,num1+num2# encounters return immediate end, the following code does not go
print (plus (10,2))
A=plus (12,3)
Print (a[2]) # The return value is a tuple
#函数没有返回值时, which returns none
#传什么类型, returns what type

#局部变量, global variable
#如果在函数里修改全局变量, need to declare first, declare with global

Python Basic Operations _ Method (function)

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.