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

Case Analysis of the use of Python function and Control statement

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail the "case analysis of the use of Python functions and control statements". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "case analysis of the use of Python functions and control statements" can help you solve your doubts.

Function

"the dirty work is left to the function." first of all, take a look at the way functions are defined in Python.

The name of the def function (argument 1, argument 2...):

Return 'result'

Functions are used to deal with repetitive things, for example, to find the area of a right triangle, and each time we have to define two right edges and a calculation formula. By defining the function, the area function of the right triangle can be calculated by entering only the right edges:

Def function (return b): return '1/2*a*b'# can also write something like this def function (ameme b): print (1/2*a*b)

Don't worry too much about the difference, using return returns a value, while the second one is to call the function to perform the print function. Enter function (2p3) and you can call the function to calculate the area of right triangles with right sides of 2 and 3.

Judge

The format of the judgment statement for Python is as follows:

If condition: doelse: do# Note: don't forget colons and indentation # take a look at the format of multiple conditions if condition: doelif condition: doelse: do

Here, we give an achievement and return the situation of its achievement.

A = 78if a > = 90: print ('excellent') elif a > = 80: print ('good') elif a > = 60: print ('qualified') else: print ('unqualified') cycle

The loop statements for Python include the for loop and the while loop, as shown in the following code.

The # for loop for item in iterable: do#item represents the element, and iterable is the collection for i in range (1Mague 11): print (I) # the result is output from 1 to 10 in turn, keeping in mind that 11 is not output, and range is the Python built-in function. # while Loop while condition: do

For example, design a Mini Program that calculates the sum of 1 to 100:

I = 0sum = 0while I < 100: I = I + 1 sum = sum + iprint (sum) # result 5050

Finally, when using a loop with judgment, you need to learn the use of break and continue. Break terminates the loop, while continue skips the loop and then continues the loop.

For i in range (10): if I = = 5: break print (I) for i in range (10): if I = = 5: continue print (I) here, the article "case Analysis of the use of Python functions and Control statements" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, Welcome to 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