Python programming language is readable and efficient in writing operations. In this article, the discussion is on inline if-else conditional statements. Python doesn’t have a ternary operator, so we use if-else in a single line that has the same effects as ternary operators. This condition evaluates conditions in a state of being true or false.

As inline if-else statements are logical statements that offer a single line that preserves code quality by replacing the multiple lines of if-else code. Inline if-else statements should be used with the expressions and their execution based on the evaluation conditions.

Syntax of Inline if-else in Python

To write an Inline if-else statement we have to follow this syntax.

<expression1> if <condition> else <expression2>

In this syntax, will be returned or executed if the condition is true, or else will be returned or executed, and these conditions are always executed from left to right.

IndentationError in Inline if-else

Python uses indentation to distinguish which lines of code correspond to others. Because a Python file can contain multiple function definitions. A function must have one line of code to be valid. It also signifies that even if the condition is true, at least a single line of code must be executed; otherwise, exception of IndentationError occurs with the message “expected an intend block”

Example 1:

Let’s start with a simple example of how this inline if-else works. Remember conditions are assessed from left to right. Here we define a variable ‘a’ equals ‘5’, and a_output has an if-else condition that we pass to print() function to show the output. The inline if-else condition here returned ‘20’ if variable ‘a’ equal to ‘20’ else returned ‘10’.

a = 5

a_output = “20” if  a==20 else “10”

print(a_output)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image1-2.jpg" data-lazy- height="80" src="data:image/svg xml,” width=”559″>

So we have ‘a’ equals ‘5’, so the condition is false, and we get 10 after executing this code. The code output is shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image3-2.jpg" data-lazy- height="56" src="data:image/svg xml,” width=”526″>

Example 2:

In this example, we have two variables in this inline statement as. In the code, we define a variable as ‘x’ and assign a value ‘20’. Also, we have another variable as ‘y’ is equal to ‘10’. So the condition is ‘y’ equal to ‘10’ if ‘x’ is less than ‘20’ else ‘0’ will print. Well, pass ‘x’ through the condition, so from left to right if ‘x’ is less than ‘20’, the answer is no, so we expect ‘y’ to be ‘0’. Let’s run the example and have a look at the value of ‘y’.

x=20

y=10  if x<20 else ‘0’

print(y)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image2-2.jpg" data-lazy- height="80" src="data:image/svg xml,” width=”561″>

Here ‘y’ is ‘0’ because the condition is false as ‘x’ is not less than ’20’ it’s equal to ‘20’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image4-2.jpg" data-lazy- height="56" src="data:image/svg xml,” width=”537″>

From the above code, we got the value of ‘y’ as ‘0’.Now, what if ‘x’ is less than ‘20’ by taking the value of ‘x’ as ‘19’. We expected ‘y’ to be ‘10’.We can see the value of ‘y’ by printing the value.

x=19

y=10  if x<20 else ‘0’

print(y)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image14-1.jpg" data-lazy- height="80" src="data:image/svg xml,” width=”561″>

As we have output value ‘y’ as 10’, the condition is true as ‘x’ is less than ‘20’. Displaying the output on the terminal screen.

Example 3:

In this particular example, we have a code of string. By assigning a value ‘red’ to a variable ‘flower’ and applying inline if-else condition that prints “The flower is white” if variable ‘flower’ equals to ‘white’ otherwise else statement “the flower is not white” will print.

flower=‘red’

print(“The flower is white” if flower == ‘white’ else “The flower is not white”)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image5-2.jpg" data-lazy- height="74" src="data:image/svg xml,” width=”602″>

The value of the flower is red; as a result, the condition doesn’t fulfill, and the statement in the else portion is implemented. You can see the output below that prints “The flower is not white”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image8-2.jpg" data-lazy- height="59" src="data:image/svg xml,” width=”531″>

Example 4:

All examples are simply inline if-else conditions, but now we will discuss nested if-else conditions. We are taking an example of car license approval for people having an age equal to 18 or above 18.

We have a variable as ‘age’ that takes input from a user. The conditional inline statement will print “you are not eligible to apply” if the age is below 16.  If the age is between 16 and 18, it will print “have to wait to be turned 18 ”; otherwise, “you are eligible to apply” will be shown.

age = int(input(“Enter your age = “))

message= ‘you are not eligible to apply’ if age<16 else ‘have to wait to be turned 18 ‘ if 16<age<=18 else ‘you are eligible to apply’

print(message)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image7-2.jpg" data-lazy- height="75" src="data:image/svg xml,” width=”962″>

The output of this code shows a message “you are not eligible to apply” as the user enter the age 15, which is less than 16.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image11-1.jpg" data-lazy- height="79" src="data:image/svg xml,” width=”614″>

Now when users enter the age of 17, we get printed “you have to wait to be turned 18 ” as the age is between 16 and 18. The output message is shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image9-2.jpg" data-lazy- height="80" src="data:image/svg xml,” width=”573″>

The console screen printed “you are eligible to apply” as the age is greater than 18.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image10-2.jpg" data-lazy- height="83" src="data:image/svg xml,” width=”568″>

Example 5:

This is an example of an extremely obvious real-time case where we must evaluate multiple conditions. Here we have to allocate the grade from the marks scored; we define a variable “Marks” and initialize it with the value ‘80’.

The condition is stored in a variable named “Result” that will print the grades from “A” to “Fail” on execution. This time Marks are greater than 90, so it is expected to have A as output. Let’s run this code to see what the output of this code is.

Marks = 95

Result = ‘Grade = A ‘ if Marks > 90 else ‘Grade = A’ if Marks > 80 else ‘Grade = B’ if Marks >70 else ‘Grade = C’ if Marks >60 else ‘Grade = D’ if  Marks > 40 else ‘Fail’

print(Result)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image12-1.jpg" data-lazy- height="76" src="data:image/svg xml,” width=”1124″>

Grade A gets printed to the console screen. We can see the output of the snippet below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image13-1.jpg" data-lazy- height="54" src="data:image/svg xml,” width=”543″>

Conclusion

We have deeply gone through the inline if-else in python with multiple valid examples with the help of the Spyder terminal. Inline if-else has a much shorter and neater code that is clear and easy to keep. Hopefully, this will assist you to understand the fundamental concept of inline if-else statements.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content