Python dazzling skills seven ways to write conditional statements

Image for post
Image for post

Some people say that getting started with Python is easy, but mastering is difficult, i agree with this point.

There are many (and more and more) advanced features in the Python language, which are very popular among Python enthusiasts. In the eyes of these people, those who can write advanced features that ordinary developers can’t understand are masters and gods.

But you have to know that in teamwork, why do you say that? Let me talk about my opinion:

I will summarize and list the dazzling skills I have seen. Let’s warm up today and write a very simple conditional judgment statement that makes people want to scold the streets. Here , If you are a Python enthusiast, you can learn some cool code writing skills, but learning is learning, I hope you use it in different scenarios.

Original code

This is a very simple code that judges whether a person is an adult based on age. Due to the number of lines of code, some people are reluctant to write this way, because it does not reflect their years of Python skills.

if age> 18 :

return “Adult”

else :

return “Underage”

Below I have listed five variations of this code. One is 6 more than the other. It is easier to understand if you put it out separately. If you put it in the engineering code, people who have never used these methods will definitely look confused. After understanding, I yelled inadvertently: Damn it, can you write like this? , And then I’m going to start cursing the street: Is this the code for people to see? (Except for the first type)

The first

grammar:

< expression 1 > if < condition > else < expression 2 >

example

> >> age1 = 20

> >> age2 = 17

> >>

> >>

> >> msg1 = “ Adult “ if age1> 18 else “Underage”

> >> print msg1

Adult

> >>

> >> msg2 = “ Adult “ if age2> 18 else “Underage”

> >> print msg2

Minor

> >>

The second

grammar

< expression > and < on_true > or < on_false >

example

> >> msg1 = age1> 18 and “ Adult “ or “Minor”

> >> msg2 = “ Adult “ if age2> 18 else “Underage”

> >>

> >> print (msg1)

Adult

> >>

> >> print (msg2)

Minor

The third

grammar

( “false” , “true” )[condition]

example

>>> msg1 = ( “Minor” , “Minor” )[age1> 18 ]

>>> print(msg1)

Minor

>> >

>>>

>>> msg2 = ( “Minor” , “Minor” )[age2> 18 ]

>>> print(msg2)

Minor

The fourth

grammar

(lambda: < on_false > , lambda: < on_true > )[ <condition> ]

example

>>> msg1 = (lambda:”underage”, lambda:”adult”)[ age1> 18 ]

>>> print(msg1)

Adult

>>>

>>> msg2 = (lambda:”underage”, lambda:”adult”)[ age2> 18 ]

>>> print(msg2)

Minor

The fifth

grammar:

{ True : <on_true>, False: <on_false>} [<condition>]

example:

>>> msg1 = { True: “ Adult “ , False: “Underage” }[age1> 18 ]

>>> print(msg1)

Adult

>> >

>>> msg2 = { True: “ Adult “ , False: “Underage” }[age2> 18 ]

>>> print(msg2)

Minor

Sixth

grammar

(( < condition > ) and ( < on_true > ,) or ( < on_false > ,))[0]

example

>>> msg1 = ((age1> 18 ) and ( “already adult” ,) or ( “underage” ,))[ 0 ]

>>> print(msg1)

Adult

>> >

>>> msg2 = ((age2> 18 ) and ( “ Already an adult” ,) or ( “Underage” ,))[ 0 ]

>>> print(msg2)

Minor

The above code is relatively simple and can be understood by paying attention, so I won’t explain it.

Seeing this, is there any posture? After learning Python for so long, and so many show operations, it is really a long time to see. Among the six ways of writing, I recommend the first one, which I often use, concise and straightforward, with few lines of code. Although other writing methods can be written, they will not be used. I don’t want to meet colleagues who will use these writing methods in public code for the rest of my life.

Written by

Digital Nomad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store