Home > Backend Development > Python Tutorial > What are the forms of defining variable parameters in Python?

What are the forms of defining variable parameters in Python?

王林
Release: 2023-05-25 15:25:06
forward
840 people have browsed it

Instructions

1. Add * before the formal parameters in the format of *args.

This means creating an empty tuple named args, which can accept any external non-keyword arguments passed in. Ordinary arguments must be passed as non-keyword arguments, otherwise the Python interpreter will preferentially pass all arguments to variadic arguments.

2. **kwargs means creating an empty dictionary named kwargs, which can accept any number of actual parameters assigned with keyword parameters.

Examples

def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n
    return sum
 
print(calc(10, 9))
Copy after login

What are the advantages of Python

1. Simple and easy to use, compared with traditional languages ​​such as C/C, Java, and C# , Python’s requirements for code format are not so strict;

2. Python is open source, everyone can see the source code, and can be transplanted and used on many platforms;

3 , Python is object-oriented and can support process-oriented programming and object-oriented programming;

4. Python is an interpreted language. Programs written in Python do not need to be compiled into binary code and can be run directly from source code. Program;

5. Python is powerful and has many modules, which can basically realize all common functions.

The above is the detailed content of What are the forms of defining variable parameters in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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