python-Custom Functions-parameters

Source: Internet
Author: User

One, custom function parameters

1. Type

(1) Position parameters

"X" is the positional parameter.

# !/usr/bin/env python # -*-coding:utf-8-*- # Author:dingkai # Mtime:2018/4/27 def Power (x):     = x * x    print(Result)

(2) Default parameters

"N" is the default parameter

# !/usr/bin/env python # -*-coding:utf-8-*- # Author:dingkai # Mtime:2018/4/27 def Power (x,n=1):    = 1 while     n > 0:        = n-1 c17/>= S * x    return s

(3) Variable parameters

"Numbers" is a mutable parameter, when the Calc function is called, the argument numbers is a list

def Calc (*numbers):     = 0    for in  numbers:        = sum + N * n    return sum
Print (Calc ([+]))

(4) Keyword parameters

"**every" is the keyword parameter

def person (name,age,**every):
Print (' name: ', Name, ' Age: ', age, ' other: ', every)

Extra = {' City ': ' Beijing ', ' job ': ' OPS '}
#person (' Dingkai ', 25,city = extra[' city '],job = extra[' job '])
#person (' Dingkai ', 25,**extra)

(5) named keyword parameter

"*" followed by the name of the keyword

def person (name,age,*,city, Job):    print(name,age,city,job) #person ('  Dinkai', 26,city= ' Beijing ', job='OPS')

2. Precautions:

(1) is a required parameter before, the default parameter is behind, otherwise the Python interpreter will error (think about why the default parameters can not be placed in front of the required parameters);

(2) is how to set default parameters.

python-Custom Functions-parameters

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.