Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to expand the application of python return values and function parameters

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to expand the application of python return values and function parameters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Return value

Enter:

#! / usr/bin/python

# Filename: func_return.py

Def maximum (x, y):

If x > y:

Return x

Else:

Return y

Print (maximum (2,3))

Output:

three

Explanation:

The function is still to give two parameters, after comparing the size, return a larger number.

Here return x; return y returns the value referred to by the parameter.

Extended Application of function parameters

Enter:

#! / usr/bin/python

# Filename: total.py

Def total (initial=5, * numbers, * * keywords):

Count = initial

For number in numbers:

Count + = number

For key in keywords:

Count + = keywords [key]

Return count

Print (total (10,1,2,3, vegetables=50, fruits=100))

Output:

one hundred and sixty six

Explanation:

The function defined in this example takes three parameters, declares an initial with a default value, and declares an asterisk parameter (such as * param), indicating that all positional parameters from that point to the end are collected as a list named "param". Double star parameters (such as * * param) are declared, indicating that all keyword parameters from this point to the end are collected into a dictionary named "param".

During the call, total (10,1Magne2Magne3, vegetables=50, fruits=100), the first parameter is assigned to initial, changing its default value, initial=10, and the latter 1meme2 is included in * numbers as a position parameter, and the next two are included in * * keywords's dictionary as keyword parameters.

Final execution result

10 + 1 + 2 + 3 + 50 + 100 = 166

This is the end of the content of "how to expand the application of python return values and function parameters". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report