This is a collection of some Python Tips regarding how to work with If-Else wisely. These tips are extracted from below posts.
“https://medium.com/swlh/5-ways-to-replace-if-else-statements-857c0ff19357 “
https://scotch.io/bar-talk/5-tips-to-write-better-conditionals-in-javascript
https://www.tutorialdocs.com/article/python-conditional-statements-tips.html
1. If/Else in One Line
print("A") if a > b else print("B")
2. Remove unnecessary else blocks

3. Value assignment
If you want to assign a new value to a variable based on some provided input, then stop the If-Else nonsense — there’s a more readable approach.

First off, If-Else is easily replaced with a switch here. But, we can simplify this code even further by removing else if and else altogether.

Take away the else if and else, and we are left with clean, readable code. Notice that I’ve also changed the style to be fast return opposed to single return statement — it simply doesn’t make sense to continue testing a value if the correct one has already been found.
4. Use “x in List” for Multiple OR Criteria


5. Pay attention to the duplicate code under different branches
The duplicate code is the enemy of code quality, and conditional statements are very easy to become the hardest hit areas for duplicate code. Therefore, we need to pay attention not to produce unnecessary duplicate code when writing conditional branch statements.
Let’s look at the following example:
# Create new user profiles for new users, otherwise update the old data.
if user.no_profile_exists:
create_user_profile(
username=user.username,
email=user.email,
age=user.age,
address=user.address,
# For new users, set the users' points to 0.
points=0,
created=now(),
)
else:
update_user_profile(
username=user.username,
email=user.email,
age=user.age,
address=user.address,
updated=now(),
)
In the above code, we can see that under different branches, the program calls different functions and does different things. However, because of the existence of the duplicate code, it is difficult to distinguish the difference between the two easily.
In fact, thanks to the dynamic feature of Python, we can simply rewrite the above code to make the readability significantly improved:
if user.no_profile_exists:
profile_func = create_user_profile
extra_args = {'points': 0, 'created': now()}
else:
profile_func = update_user_profile
extra_args = {'updated': now()}
profile_func(
username=user.username,
email=user.email,
age=user.age,
address=user.address,
**extra_args
)
6. Encapsulate those logical judgments that are too complicated
# If the activity is still active and the remaining quota of the activity is greater than 10, then
# 10,000 coins will be issued for all the active users whose genders are female, or levels are greater than 3.
if activity.is_active and activity.remaining > 10 and \
user.is_active and (user.sex == 'female' or user.level > 3):
user.add_coins(10000)
return
if activity.allow_new_user() and user.match_activity_condition():
user.add_coins(10000)
return