Python basic functions

Source: Internet
Author: User

function naming rules:

As with variables, refer to the official recommended naming format for Python: function_name. lowercase + underline

Required parameters: Arguments must be passed and can be assigned at call time.

Default parameter: parameter can be passed. Does not pass the parameter, is the default value, passes the parameter to overwrite. The name of the parameter is the same! (Example: Step is a default parameter)

Optional parameters: The brackets are indicated. (e.g. before: [, stop] is optional)

The parameter passed in is a tuple, and no incoming parameter is an empty tuple.

Keyword parameter: When defined, it looks the same as the required parameter. When you invoke a function by using the keyword argument, the order of the arguments can be different from the declaration, and the Python interpreter can match the parameter values with the argument names.

Indefinite Item parameter:

Fun (*args), asterisk * must have, args is the convention idiomatic, generally written *args. Whatever you want, you can pass anything.

If you pass in a list, dictionary ... are packaged into tuples as elements of a tuple.

Fun (*[1,2]) Add a *, is the inside of the shell removed, unpacking. (+)

Fun (*{1,2}), {1,} dictionary, only the key is left.

Fun (**kwargs) Example: Fun (A=1, b=2), {"A": 1, "B": 2}. Packaged into a dictionary. To follow the rules of variable naming!

This situation: Fun (**{' a ': 1}), {' A ': 1} keyword must be a character type, or an error.

Python basic functions

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.