Python Ternary if

In this article, we explore the concept of ternary if in python. We will learn about what it is and how to use it.

Python Ternary if
Python Ternary Operator

Python ternary is a type of conditional statement used in Python. It is similar to the if-else statement, but it has a different syntax. The Python ternary operator allows you to write concise code for conditionals. It evaluates a condition and returns a value based on the result.

To write a ternary if statement, you first need to specify the condition that you want to test. If condition is true, then the first value will be returned. If the condition is false, then the second value will be returned.

Python ternary Syntax

The basic syntax of the Python ternary operator is:

condition? value_if_true : value_if_false

Example1:

Let's check if a number is positive or negative. We can use the following code:

number = -5

result = "positive" if number > 0 else "negative"
print(result)

Returns negative

This program will print "negative" to the console because the number is less than 0. However, if we change the number to 5, it will print "positive" instead.

Example2:

You can also use the ternary if statement to return different values based on multiple conditions. For example, to check if a number is positive, negative, or 0. We can use the following code:

number = 5

result = "positive" if number > 0 else "negative" if number < 0 else "zero"
print(result)

Returns positive

It prints "positive" to the console because the number exceeds 0. If we change the number to -5, it will print "negative" instead. And if we change the number to 0, it will print "zero."


More Examples:

You can use the Python ternary operator to write a simple calculator program:

x = int(input("Enter a number: ")) 
y = int(input("Enter another number: ")) 
result = x + y if x > y else y - x
print("The sum of the two numbers is: ", result)

In this example, we first take two numbers as input from the user. Then, we use the Python ternary operator to determine which number is greater. If x is greater, then it will print the sum of the two numbers. Otherwise, it will print the difference between the two numbers.

The int() function converts the input into an integer. Find out more about the int() function:

Python Convert String to int (geekbits.io)

Example:

You can also use the Python ternary operator to write a program that prints whether a number is even or odd:

x = int(input("Enter a number: ")) 
print("The number is even." if x % 2 == 0 else "The number is odd.")

In this example, we take a number as input from the user and then use the Python ternary operator to determine whether it is even or odd. If the number is divisible by 2, then it will print "The number is even."; otherwise, it will print "The number is odd."

Note:

  1. The value_if_false and value_if_true parts of the statement can be any Python expression, not just simple values like we used in our examples.

That's all about the python ternary if statement!

Conclusion

As you can see, the Python ternary operator writes concise and efficient programs. You should use the Python ternary operator when writing a program that performs one action if a condition is true and another if condition is false. It is helpful when writing a time- or memory-sensitive program.

If you have questions about the Python ternary operator, Put them in the comments. Thanks for reading.

Happy coding :)

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.