Python Inline If Else (With 5 Examples)

Python Inline If else statement

You can use Python inline if to write an if else statement on a single line.

It allows you to write a single line of code instead of using a traditional if-else statement block.

Python inline if is also known as the Python Ternary Operator.

Syntax

Syntax of Python inline if else assignment

The syntax of Python inline if else assignment is as follows:

variable = value_if_true if condition else value_if_false

Let’s break down the syntax of an inline if statement.

value_if_true: This value will be returned if the condition is true.

condition: This is a Boolean expression that evaluates to be either true or false.

value_if_false: This value will be returned if the condition is false.

For example, the following code would return the value “Hello” if the variable x is greater than 10, and the value “Goodbye” if it is not:

x = 15
result = "Hello" if x > 10 else "Goodbye"
print(result)

Output

This code would print the following output:

Hello

Here are some additional things to keep in mind when using inline if in Python:

  • The condition must be a Boolean expression.
  • The value_if_true and value_if_false expressions must be of the same type.
  • If the condition is false, the value_if_false expression will be evaluated.
  • If the condition is true, the value_if_true expression will be evaluated.

Uses & Examples

Uses of Python inline if else

You can use Inline ifs in a variety of situations, such as:

  • Assigning values to variables
  • Calling functions
  • Making decisions

They can be a helpful way to make your code more concise and readable.

However, it is important to use them sparingly.

As they can make your code more difficult to understand if you overused the inline if else assignment.

Assigning values to variables using Python inline if

Here’s an example to illustrate how to use the inline if statement to assign values to variables.

age = 20
is_adult = True if age >= 18 else False
print(is_adult)

Output

True

In this example, first of all, we have to evaluate the condition age >= 18.

Since the value of age is 20, the condition is true, and the value True is assigned to the variable is_adult.

You can use the inline if statement as nested inline if else statements as well.

This allows you for more complex assignments.

Here’s an example.

x = 5
y = 10
z = "x is greater" if x > y else ("y is greater" if y > x else "x and y are equal")
print(z)

Output

y is greater

In this example, we checked if x is greater than y.

If true, we assigned “x is greater” to z.

Then we checked If y is greater than x.

If true, the program executes the nested inline if statement, and assigns the value “y is greater” to z. Otherwise, if x and y are equal, we assign the innermost value “x and y are equal”.

Calling functions using Python inline if

Here’s another example that calls a function with the inline if else statement.

def is_even(number):
    return number % 2 == 0

def is_positive(number):
    return number > 0

x = 8
message = "Even and positive" if is_even(x) and is_positive(x) else "Not even or positive"
print(message)

Click on this code and it will be copied automatically.

Then you can run it on our free Online Python Compiler.

Output

The code gives the following output.

Even and positive

In this example, we have two custom functions: is_even() and is_positive().

The is_even() function takes a number as input and checks if it’s an even number, returning True if it is, and False otherwise.

The is_positive() function takes a number as input and checks if it’s a positive number, returning True if it is, and False otherwise.

We then have a variable x set to 8.

Then, we used the inline if else statement to evaluate the conditions is_even(x) and is_positive(x).

If both conditions are true (in this case, x is even and positive), the value assigned to the message variable is “Even and positive”.

If any of the conditions is false (if x is either not even or not positive), the value assigned to the message is “Not even or positive”.

Making decisions using Python inline if

You can make decisions in Python using the inline if else assignment.

Here is an example,

is_error = True
message = "Error occurred" if is_error else "No errors found"
print(message)

Output

Error occurred

In this example, we used the inline if statement to format a message based on the value of is_error.

If is_error is True, we assigned the value “Error occurred” to the message.

Otherwise, if is_error is False, the program assigns the value “No errors found” to the message.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x