Writing conditional statements is a part of every programming language.
Conditional statements, such as if statements, allow you to control the flow of your program, and have code that only runs when a particular condition is (or set of conditions are) met.
In Python, the most common way to write a conditional statement is to use
if. However, the language also offers a way to test for a condition on one-line: the ternary operator.
This tutorial will discuss, with reference to examples, the basics of conditional statements, and how to use the Python ternary operator.
When you’re writing a program, you may only want a line or block of code to be executed when a condition is met. This is where conditional statements are useful.
if statement is used in Python to check whether a condition is met.
Suppose we are building an app that checks whether a customer is eligible for a 10% discount at a movie theater. If the customer is aged 65 or over, they should be given a discount, otherwise, no discount should be given.
We could use the following program to perform this task:
discount = False age = 67 if age >= 65: discount = True print(discount)
Our code returns: True. Let’s break down our code. First, we declare a variable called
discount and assign the value False to the variable. Next, we declare a variable called age and assign the variable the value 67.
On the next line, we use an
if statement to check the age of the customer. If the customer’s age is equal to or greater than 65, then the contents of our
if statement are executed. Otherwise, the
if statement is not executed.
In this case, because our customer is aged 67, the contents of our
if statement are executed. This means that the
discount variable is assigned the value True. Then, we print out the value of the
discount variable to the console.
To learn more about the Python if statement, read our Python if else guide.
In this code, we were able to check if a condition was met, and run a block of code based on whether that condition was met. But there is a more efficient way to perform this action: using the Python ternary operator.
Python Ternary Operator
The ternary operator is a type of conditional expression in Python that evaluates a statement, and performs an action based on whether that statement is true or false.
The syntax for the Python ternary statement is as follows:
[if_true] if [expression] else [if_false]
The ternary conditional operator in Python gets its name from the fact it takes in three parameters: if_true, expression, and if_false. Let’s walk through an example to demonstrate how this operator works.
Suppose we want to give a customer at our movie theater a discount if they are 65 or older. We could do so using the following code:
age = 48 discount = True if age >= 65 else False print(discount)
Our code returns:
Let’s break down our code. On the first line, we declare a variable called
age which is assigned the value 48. Next, we use a ternary operator to calculate whether the customer is eligible for a discount.
Our ternary operator evaluates the expression age >= 65. Because
age is equal to 48, this evaluates to False, so the code that appears after the
else statement is executed.
Then, we print the result of our ternary operator to the console. This returns the value False.
If we compare this example to our last one, you can see that it uses significantly fewer lines of code. Our first program used five lines of code, and this one used three.
In this example, our ternary operator returns a boolean value (True or False). However, we could allow our ternary operator to return any value.
Suppose we want to set a specific discount rate depending on whether the user is eligible for a discount. By default, our movie theater is giving all customers a 5% discount, but seniors (people 65 or older) are eligible for a 10% discount.
The following program allows us to check whether a customer is eligible for the senior discount, and if they are not, the customer is given a 5% discount:
age = 22 discount = 5 if age < 65 else 10 print(discount)
Our code returns: 5. In our code, we assign the value 22 to the variable
Then, we use a ternary operator to check if the value of the
age variable is less than 65. Because our customer is 22, the statement
age < 65 evaluates to True. This means that the customer is given the 5% discount rate. If our customer was 65 or older, they would receive the 10% discount rate.
Then, we print the value of the
discount variable to the console.
Using the ternary operator is useful because it allows you to perform an
if statement without having to write out a full
if statement. In our examples above, we were able to run an
if statement on one line, instead of on the multiple lines it would typically take to create an
However, the ternary operator cannot always be used. If you want to test for multiple expressions, you should write out a full
if statement. This will ensure that your code is readable and easy to understand.
The Python ternary operator is a more efficient way of performing simple
The ternary operator evaluates a condition, then returns a specific value depending on whether that condition is equal to True or False.
This tutorial discussed, with reference to examples, the basics of the Python ternary operator and how it works. Now you have the knowledge you need to use this operator like a Python pro!