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

Using functions or complex expressions in Python

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

Share

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

This article focuses on "using functions or complex expressions in Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "using functions or complex expressions in Python".

Do you want to use complex expressions?

Larry Wall, the original author of the Perl language, once said that great programmers have three advantages: laziness, irritability and conceit. At first glance, none of these three words is a commendatory word, but in the programmer's world, these three words have different meanings. First of all, laziness will prompt programmers to write trouble-saving programs to help themselves or others get the job done better, so that we don't have to do repetitive and tedious work; in the same way, we can solve things with three lines of code. We would never write 10 lines of code. Second, grumpy will let programmers take the initiative to complete some of the work you have not proposed, to optimize their own code to make it more efficient, can be completed in 3 seconds of the task, we can not tolerate a minute of waiting. Finally, conceit motivates programmers to write reliable code, which we write not to accept criticism and criticism, but to be worshipped by others.

Then there is an interesting question worth exploring. We need a program to find the largest of the three numbers entered. This program is a piece of cake for anyone who can program, and even those who can't program can do it after 10 minutes of study. Here is the Python code to solve this problem.

A = int (input ('a =') b = int (input ('b =')) c = int (input ('c =')) if a > b: the_max = aelse: the_max = bif c > the_max: the_max = cprint ('The max is:', the_max)

But as we just said, programmers are lazy, and many programmers use ternary conditional operators to rewrite the above code.

A = int (input ('a =')) b = int (input ('b =')) c = int (input ('c =') the_max = an if a > b else bthe_max = c if c > the_max else the_maxprint ('The max is:', the_max)

It should be noted that before version 2.5, Python did not have the ternary conditional operators used in lines 4 and 5 of the above code, because Guido van Rossum (the father of Python) thought that ternary conditional operators could not help Python become more concise, so those who were accustomed to using ternary conditional operators in Java + or Java (in these languages, ternary conditional operators are also called "Elvis operators"). Because programmers who put it together look like the big back of the famous rock singer Elvis Elvis tried to simulate ternary operators using the short-circuit features of the and and or operators, so in those days, the above code was written like this.

A = int (input ('a =')) b = int (input ('b =')) c = int (input ('c =')) the_max = a > b and an or bthe_max = c > the_max and c or the_maxprint ('The max is:', the_max)

But this doesn't work in some scenarios, so take a look at the code below.

The code below a = 0b =-10values expected to output the value of a, but ended up with the value of b # because the value of 0 of a will be treated as False when performing logical operations to deal with print (True and an or b) # print (an if True else b)

So ternary conditional operators were introduced after Python 2.5 to avoid the above risk (the last sentence in the above code that was commented out). So, here comes the question, can the above code be written more briefly? The answer is yes.

A = int (input ('a =')) b = int (input ('b =')) c = int (input ('c =')) print ('The max is:', (an if a > b else b) if (an if a > b else b) > c else c)

But is it really good to do so? Does such a complex expression make the code much more obscure? We found that in actual development, many developers like to overuse the features or syntax candy of a certain language, so it is really good to turn simple multiple lines of code into complex single-line expressions. I have asked myself this question more than once, and now the answer I can give is the following code, using helper functions.

Def the_max (x, y): return x if x > y else ya = int (input ('a =')) b = int (input ('b =')) c = int (input ('c =') print ('The max is:', the_max (the_max (a, b), c))

In the above code, I defined an auxiliary function the_max to find the larger of the two values passed in by the parameter, so the following output statement can find the maximum of the three values by calling the the_max function twice. Now the readability of the code is much better. It is really a good choice to replace complex expressions with auxiliary functions. The key is that after transferring the larger logic to this auxiliary function, you can not only call it repeatedly, but also cascade operations.

Of course, large and small functions in many languages don't need to be implemented by themselves (usually built-in functions), and neither does Python. Python's built-in max function takes advantage of Python's support for variable parameters, allowing you to pass in multiple values or an iterator at a time and find out the maximum value, so the problem discussed above is an one-sentence thing in Python, but the idea from complex expressions to using auxiliary functions to simplify complex expressions is very interesting, so share it with you.

A = int (input ('a =') b = int (input ('b =')) c = int (input ('c =')) print ('The max is:', max (a, b, c)) so far, I believe you have a deeper understanding of "using functions or complex expressions in Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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