Home > Backend Development > Python Tutorial > Introduction to the usage of * in python (code example)

Introduction to the usage of * in python (code example)

不言
Release: 2019-03-09 13:48:32
forward
2545 people have browsed it

This article brings you an introduction to the usage of * in python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Represents multiplication operation

2. Represents multiples

def T(msg,time=1):
    print((msg+' ')*time)

T('hi',3)
Copy after login

Result: hi hi hi

3. Single*

3.1 The

*parameter that appears in the formal parameter position of a function definition is used to accept any number of parameters and put them in a tuple.

def demo(*p):
    print(p)
    
demo(1,2,3)
Copy after login

Result: (1, 2, 3)

3.2 Appears in the actual parameter position of the function call

When the function calls multiple parameters, it appears in the list or tuple , collections, dictionaries and other iterable objects as actual parameters, and add * in front, the interpreter will automatically unpack and pass to multiple single-variable parameters (the number of parsed parameters must be equal to the number of function parameters) .

a=[1,2,3]
d(*a)
Copy after login

Result: 1 2 3

4. Two ** appear in the formal parameter part of the function definition

For example: **parameter is used to receive keys similar to Put multiple actual parameters in the same assignment form as parameters into the dictionary (that is, convert the parameters of the function into a dictionary).

def demo(**p):
    for i in p.items():
        print(i)
       
demo(x=1,y=2)
Copy after login

Result: ('x', 1) ('y', 2)

The above is the detailed content of Introduction to the usage of * in python (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template