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 define and call functions in python Foundation

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you how to define and call functions on the basis of python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

# first question # write a function, accept n digits, find the sum of these parameter numbers and def sumFunc (* args): # process the accepted data result=0 for item in args: result+=item pass return result pass# call rs=sumFunc ('rs= {}' .format (rs)) # fill the hole method format format output

# write the function to find out the elements corresponding to the odd bits of the passed list or tuple, and return a new list def processFunc (con): listNew= [] index=1 # sequence index value for i in con: if index%2==1: listNew.append (I) pass index+=1 pass return listNew passrs3=processFunc ([1Med 2LING 3LING 5LING 7LING 8LING 7LING 1]) print (rs3)

# write function to check the length of each value passed into the dictionary. If it is greater than 2, only the first two long contents will be retained. And the value in the # ps: dictionary can only be a string or list def dictFunc (dicParms): # * * kwargs' handles dictionary type data: param dicParms:: return:''result= {} # declare an empty dictionary for key Value in dicParms.items (): # key-value if len (value) > 2: result [key] = value [: 2] # Dictionary appended data pass else: result [key] = value pass pass return result pass# function call dictObj= {"name": "Ouyang", "hobby": ['singing', 'sports'] "pro": 'art'} print (dictFunc (dictObj))

This is how to define and call functions on the basis of python shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report