Home > Backend Development > Python Tutorial > Python default parameters and variable length parameters (example analysis)

Python default parameters and variable length parameters (example analysis)

乌拉乌拉~
Release: 2018-08-15 13:51:50
Original
2747 people have browsed it

In today's article, we will learn about the parameters in python, and learn about the remaining python default parameters and python variable length parameters. In today's article, we will learn about what are default parameters and variable-length parameters, and know the format and usage of python default parameters and python variable-length parameters to expand our Deepen your knowledge of parameters.

Default parameters

When calling a function, if the value of the default parameter is not passed in, it is considered the default value. The following example will print the default age, if age is not passed in:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
#可写函数说明
def printinfo( name, age = 35 ):
   "打印任何传入的字符串"
   print "Name: ", name;
   print "Age ", age;
   return;
 
#调用printinfo函数
printinfo( age=50, name="miki" );
printinfo( name="miki" );
Copy after login

The above example output result:

Name:  miki
Age  50
Name:  miki
Age  35
Copy after login

Indefinite length parameters

You may A function is required that can handle more arguments than when originally declared. These parameters are called variable-length parameters. Unlike the above two parameters, they will not be named when declared. The basic syntax is as follows:

def functionname([formal_args,] *var_args_tuple ):
   "函数_文档字符串"
   function_suite
   return [expression]
Copy after login

Variable names with an asterisk (*) will store all unnamed variable parameters. Examples of variable-length parameters are as follows:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 可写函数说明
def printinfo( arg1, *vartuple ):
   "打印任何传入的参数"
   print "输出: "
   print arg1
   for var in vartuple:
      print var
   return;
 
# 调用printinfo 函数
printinfo( 10 );
printinfo( 70, 60, 50 );
Copy after login

The output of the above example is as follows:

输出:
10
输出:
70
60
50
Copy after login

The above is what I want to explain today, explaining the default parameters and variable-length parameters in the parameters. Although the above knowledge points give examples, you still need to try it yourself. After all, if you don't do it yourself, it is just like talking on paper. Hands-on practice is the best way and way to verify what you have learned. Finally, I also hope that this article can bring some help to you who are learning python

For more related knowledge, please visit the Python tutorial column of the php Chinese website.

The above is the detailed content of Python default parameters and variable length parameters (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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