Skip to main content

Python One Line if Statement Gorakh Gupta The Crazy Programmer

In our daily life whenever we are trying to solve any real life problems then we may have to write some conditional statements very often. A simple if condition takes 2 or 3 lines if we want to process a statement in it. Suppose we have to write more than 10 conditions in a program. We can see it will take approx 40 to 50 lines of code where we are just repeating the things.

So, python allows us to write the if conditions in a single line which is often called one liner if in Python. This will make our code more readable and less redundant. By using this functionality, we can reduce some spaces in our python code.

Now we will see all the types by using which we can write a one liner if condition in python.

Also Read: Python One Line for Loop

First, we will see a general method of if condition and its output.

# General Method for If Condition in Python
num = 6
if num % 2 == 0:
    print("Even")

Output:

PS C: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python - u " c: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
Even

In the above example, we are trying to write a condition to print “Even” if the input number is Even. We can see that if takes 2 lines of code to write this single condition.

Now we will see how we can write the same example in a one liner. There are 2 ways to write it. We will go through both ways one by one.

Python One Line if Statement

Method 1:

# One Liner which is mostly same as general Method
num = 8
if num % 2 == 0: print("Even")

Output:

PS C: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python - u " c: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
Even

Explanation:

In the above one liner we can see that it is the same as general method, the only difference is there that we did not give the indentation in the code.

Method 2:

# One Liner where first we write what we want to do 
# if the conditions are met and after that we write the condition
num = 6
print("Even") if num % 2 == 0 else None

Explanation:

In this type of one liner first, we write the statement we want to perform when the conditions are met and after that, we write the condition. We are also writing the else block as Node because in this type of one liner only if statements are not supported, we have to write else statement also. If we do not want to perform any operation in the else block we simply write None after else.

We can use one liner to assign a value to a variable when certain conditions are met.

Example 1:

# Set salary to 10000 if age is greater than 18
age = int(input("Enter Age : "))
cond = age > 18

salary = 10000 if cond else None
print(salary)

Output:

PS C: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python - u " c: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
42

Example 2:

# Set salary to 10000 if age is greater than 18
age = int(input("Enter Age : "))
cond = age > 18

salary = 10000 if cond else None
print(salary)

Output:

PS C: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python - u " c: \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
Enter Age : 19
10000

If we input age less than 18 then it will print None.

Explanation:

In above example, we can see that we can use one liner to assign a value to the variable if the certain conditions are met. In the example we are setting the salary value to 10000 if the age is greater than 18 otherwise, we are setting the value of salary to None.

Conclusion

In python, we can use one liner if conditions to write some conditions in a better way in our programs. These one liners will helps us to make our code less redundant and more readable.

We can also use one liners to assign a value to a variable if a certain condition is met otherwise we can set it to None value.

The post Python One Line if Statement appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/xZ109VF

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20