The abstraction of Python's learning notes

Source: Internet
Author: User

First, create a function

>>> Import Math

>>> x=1

>>> y=math.sqrt

>>> callable (x) #显示False

>>> callable (y) #显示True

1, Def, used to create functions

>>> def hello (name)

>>> return ' Hello, ' +name+ '! '

>>> Print (Hello (' World '))

Show: Hello, World

2. Add description for function

>>> def Square (x):

>>> ' calculates the square of the number X. '

>>> return x *x

>>> square.__doc__

Display: ' Calculates the square of the number x. '

View functions via Help

>>> Help (Square)

Second, function parameters

1. Abstract function

>>> def init (data):

>>> data[' first ']={}

>>> data[' Middle ']={}

>>> data[' last ']={}

>>> storage={}

>>> Init (storage)

>>> Storage

Display: {' middle ': {}, ' last ': {}, ' first ': {}}

2. Collect parameters

>>> def print_params (X,y,z=3,*pospar,**keypar)

>>> print (x, y, z)

>>> Print (Pospar)

>>> Print (Keypar)

Display: 1 2 3 (5,6,7) {' foo ': 1, ' Bar ': 2}

Note: ' * ', a single asterisk is used to unite common parameters, ' * * ', two asterisks are used to handle keywords

3. Power Calculation Product

>>> Power (3,2)

Showing: 9

4, Interval, interval

>>> interval (10)

Display: [0,1,2,3,4,5,6,7,8,9]

>>> interval (1,5)

Display: [1,2,3,4]

5, when the parameter name and the global variable name, the global variable will be masked, not directly accessible, if you want to access, you need to use Globals

>>> def Combine (parameter):

>>> print (Parameter+globals () [' parameter '])

>>> parameter= ' Berry '

>>> combine (' shrub ')

Display: Shrubberry

The abstraction of Python's learning notes

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.