Tips & Tricks for if else control flow
These are some tips and tricks for if else control flow in python. Do keep in mind that these are optional, and the purpose of these tricks is to make the code more readable and maintainable.
Contents of this post:
Ternary Operator
Terinary operator is a shorthand for if else statement. It is used to assign a value to a variable based on a condition. It is also called conditional expression.
# syntax: value_if_true if condition else value_if_false
a = 1
b = 2
c = a if a > b else b
print(c) # 2
Terinary operator can also be used to return a value from a function based on a condition.
def get_max(a, b):
return a if a > b else b
print(get_max(1, 2)) # 2
Terinary operator cannot be used to execute a block of code based on a condition. It can only be used to assign a value to a variable or return a value from a function.
# this will not work
a = 1
b = 2
c = if a > b:
a
else:
b
# error: SyntaxError: invalid syntax
Gaurd Clause
Gaurd clause is a pattern used to reduce the indentation level of a function or a block of code. It is used to check for a condition and return or break from the function if the condition is true. It is also called early return. It helps to avoid nested if else statements.
# case without gaurd clause
def get_max(a, b):
if a > b:
return a
else:
return b
# case with gaurd clause
def get_max(a, b):
if a <= b:
return b
return a
# case with gaurd clause and ternary operator
def get_max(a, b):
return b if a <= b else a
Another example of gaurd clause, in authentication we can check if the user is authenticated and return if the user is authenticated, otherwise continue with the rest of the code.
# case without gaurd clause
def any_function():
if wifi_is_connected():
if logged_in():
if user_is_admin():
see_admin_dashboard()
else:
see_user_dashboard()
else:
login()
else:
connect_to_wifi()
# case with gaurd clause
def any_function():
if not wifi_is_connected():
connect_to_wifi()
return
if not logged_in():
login()
return
if user_is_admin():
see_admin_dashboard()
else:
see_user_dashboard()
Multiple Conditions as a dictionary
Sometimes we need to check multiple conditions and execute different code for each condition. We can use a dictionary to store the conditions and the code to execute for each condition. The dictionary key is the condition and the value is the code to execute if the condition is true, and this value can generally be a function or lambda expression.
current_state = None
if current_state == condition1:
function1(current_state)
elif current_state == condition2:
function2(current_state)
elif current_state == condition3:
function3(current_state)
else:
# default function to be executed in case of no condition is true
function4(current_state)
# this can be reduced to a dictionary
# map conditions to functions, do not specify the default function here
# specify the default function in the get method
actions = {
condition1: function1,
condition2: function2,
condition3: function3,
}
# syntax: dict.get(key, default=None)
# the default function will be specified in the get method
action = actions.get(current_state, default=function4)