


Python default parameters and variable length parameters (example analysis)
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" );
The above example output result:
Name: miki Age 50 Name: miki Age 35
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]
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 );
The output of the above example is as follows:
输出: 10 输出: 70 60 50
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Using python in Linux terminal...
