Python Conditional Assignment
When you want to assign a value to a variable based on some condition, like if the condition is true then assign a value to the variable, else assign some other value to the variable, then you can use the conditional assignment operator.
In this tutorial, we will look at different ways to assign values to a variable based on some condition.
1. Using Ternary Operator
The ternary operator is very special operator in Python, it is used to assign a value to a variable based on some condition.
It goes like this:
variable = condition ? value_if_true : value_if_false
Here, the value of variable will be value_if_true if the condition is true, else it will be value_if_false.
Let's see a code snippet to understand it better.
a = 10
b = 20
# assigning value to variable c based on condition
c = a if a > b else b
print(c)
# output: 20
You can see we have conditionally assigned a value to variable c based on the condition a > b
.
2. Using if-else statement
if-else statements are the core part of any programming language, they are used to execute a block of code based on some condition.
Using an if-else statement, we can assign a value to a variable based on the condition we provide.
Here is an example of replacing the above code snippet with the if-else statement.
a = 10
b = 20
# assigning value to variable c based on condition
if a > b:
c = a
else:
c = b
print(c)
# output: 20
3. Using Logical Short Circuit Evaluation
Logical short circuit evaluation is another way using which you can assign a value to a variable conditionally.
The format of logical short circuit evaluation is:
variable = condition and value_if_true or value_if_false
It looks similar to ternary operator, but it is not. Here the condition and value_if_true performs logical AND
operation, if both are true then the value of variable will be value_if_true, or else it will be value_if_false.
Let's see an example:
a = 10
b = 20
# using logical short circuit evaluation
c = a < b and a or b
# c = True and 10 or 20
# c = 10
print(c)
# output: 10
But if we make condition True
but value_if_true False
(or 0 or None), then the value of variable will be value_if_false.
a = 0
b = 20
# using logical short circuit evaluation
c = a < b and a or b
# c = True and 0 or 20
# c = 20
print(c)
# output: 20
So, you can see that the value of c is 20
even though the condition a < b
is True
.
So, you should be careful while using logical short circuit evaluation.
Example 1
While working with lists, we often need to check if a list is empty or not, and if it is empty then we need to assign some default value to it.
Let's see how we can do it using conditional assignment.
my_list = []
# assigning default value to my_list if it is empty
my_list = my_list or [1, 2, 3]
print(my_list)
# output: [1, 2, 3]
Here, we have assigned a default value to my_list if it is empty.
Example 2
Assign a value to a variable conditionally based on the presence of an element in a list.
my_list = [1, 2, 3, 4, 5]
# assigning value to variable x based on condition
x = 10 if 10 in my_list else 20
print(x)
# output: 20
Conclusion
Now you know 3 different ways to assign a value to a variable conditionally. Any of these methods can be used to assign a value when there is a condition.
The cleanest and fastest way to conditional value assignment is the ternary operator.
if-else statement is recommended to use when you have to execute a block of code based on some condition.
Happy coding! 😊