Python asterisk * And ** Usage Analysis, and python asterisk Usage Analysis

Source: Internet
Author: User

Python asterisk * And ** Usage Analysis, and python asterisk Usage Analysis

This article analyzes the usage of Python asterisks * and. We will share this with you for your reference. The details are as follows:

1. Variable names with asterisks (*) will store all unnamed variable parameters and cannot store dict. Otherwise, an error is returned.

For example:

Def multiple (arg, * args): print "arg:", arg # print the variable length parameter for value in args: print "other args :", valueif _ name _ = '_ main _': multiple (1, 'A', True)

Output:

2. Variable names with asterisks (**) are used to store all unnamed variable parameters.

Def multiple2 (** args): # print the variable length parameter for key in args: print key + ":" + bytes (args [key]) if _ name _ = '_ main _': multiple2 (name = 'amy ', age = 12, single = True)

Output

3. There are * args and ** dictargs:

Def multiple (arg, * args, ** dictargs): print "arg:", arg # print args for value in args: print "other args :", value # print the variable length parameter args for key in dictargs: print "dictargs:" + key + ":" + bytes (dictargs [key]) of the dict type if _ name _ = '_ main _': multiple (1, 'A', True, name = 'amy ', age = 12 ,)

Output:

In addition, * represents multiplication in Python mathematical operations, and ** represents exponential operations.The sample code is as follows:

>>> 2*48>>> 2**416>>> 

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.