In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the use of Python functions, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1 jump out of the loop-break
Python provides a convenient and quick way to jump out of the loop-break.
The example is as follows to calculate the sum of the number of unknown numbers:
If _ _ name__ = = "_ main__": sum = 0 while True: num = str (input ('number entered (or "done"):') if num = = 'done': break # Pop out of the loop sum = num sum = sum + num print ('summation is:' + str (sum))
In this code, the loop condition of while is True, which means that the code will loop forever until the break is executed, that is, when the value of num is "complete". Compared to the previous use of the while loop alone, there are fewer repetitive input statements, but using a lot of break can also make the code difficult to understand, so it depends on the circumstances. The result of this code execution is as follows: according to development experience, this statement is generally not used unless the use of break makes the code cleaner. Of course, there are other ways to jump out of the loop, such as custom exceptions, encapsulating loop functions (using return more often), but the use of these is generally established after too much use, so we will not elaborate too much here, and we will give examples with the in-depth use of python.
In addition, continue is related to break, and when continue is used in the body of the loop, it jumps to the loop condition and enters the next loop.
If _ name__ = = "_ main__": for i in range (3): for j in range (3): for k in range (3): if I = = j = = k = 2: break else: print (str (I) + "+ str (j) +" + str ( K) else: continue break else: continue break
This code can be simply understood as permutation and combination of all cases except i=j=k=2, but will not enter any combination of i=j=k=3, this is because the code logic contains continue and break, so it is not easy to understand, so generally do not use this in development.
2 python function
Function is very important in the use of every development language. It is a code block, which can be input and output, achieve the required function calls, and can be reused in large quantities. The python we are using already has a large number of functions, which facilitates our learning, development, data analysis, and so on. In the function of Python, we divide it into three modules: built-in function, custom function and main function. Of course, variables and parameters are involved in the process of use, which will be illustrated by examples.
2.1 built-in function
In the previous exposition, we have been using the powerful built-in functions provided by python (Python's powerful syntax support) as examples.
This time, take the pow () function as an example to calculate the third power of 3:
> pow (3pr 3) 27
In the built-in function pow () function, pow is the function name, two 3s are the parameters passed to the function, and the result 27 is the return value.
As shown below:
The figure above outlines the call to the built-in function, and when we call the function with python, we replace the function call with the return value. In the basic function operation, we will notice that the return value of pow (3) is the same as the return value of 3, that is, the two can be said to be equivalent. Friends of the details will find that in python, the value of pow (0) is 1, and again, 0 is zero. Of course, this will hardly be used in actual development, just re-emphasize the comprehensive and powerful python function!
In practice, we will find that not all functions have return values, and in some cases there are no return values. Let's take a simple example as follows: when an is assigned, the return value is normal, but when an is not assigned, the return value is a None, neither a string nor a number. The meaning is to tell you that this code does not have any return value and needs to check BUG. (this is a casual example in a linux environment)
When using a function, be careful not to make your own function the same as the built-in function, to avoid inadvertently letting the built-in function point to other functions or function values, even if you use it, python will not prompt you for this error.
Here we have pow point to the number 520, and later we will get the error shown in the figure above when we use pow () again. So in order to avoid this situation, you need to avoid naming the same name, but if you encounter it, you need to re-enter the python environment, modify the function name and execute it again.
2.2 Custom function
The function of custom function is often used by programmers because it usually implements a logical function according to the specific requirements.
To take a simple example, implement a function that calculates the area:
Import math def round_area (x): return math.pi * x * * 2 if _ _ name__ = "_ _ main__": X = int (input ()) area = round_area (x) print (the area of the circle with "radius" + str (x) + "cm is:" + str (area))
For the use of custom functions, we usually save this function in a Python file, for example, we save it as practice2.py. When using it, you only need to call the name of the round_area function and pass in the parameter x (optional for the parameters in the custom function, use according to your own needs). You can call round_area () like any other built-in function, except that you have absolute control over the custom function (any modification).
As you can see from the above example, the custom function contains several rules:
1. The function begins with the def keyword, followed by the function identifier name and parentheses ()
two。 Any passed parameters and arguments must be placed in the middle of parentheses. Parentheses can be used to define parameters
3. The content of the function starts with a colon and is indented
4.return [expression] ends the function and optionally returns a value to the caller. Return without an expression is equivalent to returning None
The function header is followed by an optional document string. The documentation string briefly describes the functionality of the function and may contain examples and other useful information. Although document strings are optional, you need to use reasonable naming: when you write a large number of functions, it is easy to forget their functions and how they work, and well-written document strings are good for you to use.
The document string is followed by the function body, which is an indented block of code that does the required work. In this code block, you can use the variables in the function header.
In this example, return is used as a keyword to return a value, and when it is executed, Python jumps out of the function and returns to the place where the function was called. When customizing a function, the return statement is usually the last code to be executed in the function, but it is not necessary.
Import mathdef round_area_1 (x): print (math.pi * x * * 2) if _ _ name__ = "_ _ main__": X = int (input ()) area = round_area_1 (x) print (the area of the circle with "radius" + str (x) + "cm is:" + str (area))
The result of the execution is as follows: you can see the output with the area result, but in the end you output a None value. None is used to indicate that a function does not return a value, which is a side effect of a custom function that does not use a return statement, but Python provides powerful support for eliminating this side effect (not described here, try return-style custom functions first), including defining functions in functions and passing functions as values to other functions.
2.3 main function
If you know many languages, you must know that main functions are very common and must be used, such as Java, C, C++ and so on. The same is true of Python. When writing python programs, we should use at least one main function.
For example, the example in Python process control:
If _ _ name__ = = "_ _ main__": while True: scan_user = input ('please input your name:') scan_pass = input ('please input your passwd:') if scan_pass = 'if/else': print (' Logging on current') Else: print ('your passwd is inactive clients') Print ('I'm not in the if/else statement!')
It can be changed to the following. So that all the code is located after the function header main and indented, running the written * .py file in the editor is that you must enter main () to enter the code and start execution.
Its advantage is that it can pass parameters and run the whole program more easily.
Def main (): scan_user = input ('please input your name:') scan_pass = input ('please input your passwd:') if scan_pass = 'if/else': print (' Logging on now') Else: print ('your passwd is inactive clients') Print ('I'm not in the if/else statement!')
The use of functions will involve variables and parameters as well as modules, and the use of these three will be described in the next article. Please experience the use of the python function first if you are interested.
After reading the above, do you have any further understanding of the use of Python functions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.