Although it is easy to learn, the python developer has made it much easier through some tricks that most of you don’t know to make your code faster and look more professional. You can make anything with python, from developing web apps to making machine learning models or computer vision applications with OpenCV. This article will help you learn some of these tips & tricks for making you a better developer.
1. Ternary Operator
Most of the python developers, especially new of them, don’t know about this simple hack you can perform with python called Ternary Operator. Some people call it Ternary conditionals which can use in some situations. Let me show you an example:
condition = True
if condition:
x = 5
else:
x = 7
print(x)
You can understand from the previous example that when the condition is True, it will print the value 5. Otherwise, the value 7 will be printed. You can see the code above is simple, but you can make it much more straightforward in just one line of code. Let me show you how:
condition = False
x = 5 if condition else 7
print(x)
2. Multiple Condition
In some examples, before you execute the code block inside the if statement, you need to check multiple conditions, which make not a friendly way to perform this. Let me show you an example:
age = 60
country = "United States"
gender = "Male"
if (age == 60 and country == "United States" and gender == "Male"):
print("You are eligible for a discount")
else:
print("You are eligible for a discount")
The previous example will print that you are eligible for a discount since all the conditions are met. Let’s suppose you have more conditions to check before executing the code block inside the if statement. This will be a daunting task. There is a better version of this code and let me show you the friendly way of doing this:
age = 60
country = "United States"
gender = "Male"
conditions = [age == 60, country == "United States", gender == "Male"]
if all(conditions):
print("You are eligible for a discount")
else:
print("You are not eligible for a discount")
The above example is a much better & clever way to perform these actions. You will need to put all the conditions inside a list, and we’ve called conditions separated by a comma.
In the if statement, we use the keyword all to tell python that all conditions should be met to execute the code inside the if statement. Don’t forget to pass the conditions you set before inside the closing parentheses.
In some situations, you need to fulfill only one condition from many conditions to execute the code inside the if statement. Let me show you an example:
age = 70
country = "United States"
gender = "Female"
conditions = [age == 60, country == "United States", gender == "Male"]
if any(conditions):
print("You are eligible for a discount")
else:
print("You are not eligible for a discount")
When you run the previous code, it will tell you that you are eligible for a discount. We’ve used the keyword any, which means that if only one condition is met, then the full evaluation is True, which will let the if statement execute the code. Notice that I’ve changed the last two variables, the country & gender, and it still shows the same result as the previous one.
3. Enumerate Function
You have to count how many elements you have about something in some situations. Most Python developers use the following syntax to perform this action:
capitals = ["Berlin", "Washington", "Moscow", "Ottawa", "London"]
index = 0
for capital in capitals:
print(index, capital)
index += 1
This previous method will work correctly, but it is not friendly. You can use another easy method, and let me show it to you:
capitals = ["Berlin", "Washington", "Moscow", "Ottawa", "London"]
index = 0
for index, capital in enumerate(capitals):
print(index, capital)
After running this code, you will have the same result as the previous one, which is more accessible and friendly.
You may notice that the enumerate starts counting from 0, not 1, and this is how programming works even when you try to index an item in a list. For example, you start counting from 0. Let me show you how you start counting from 1 if you want to:
capitals = ["Berlin", "Washington", "Moscow", "Ottawa", "London"]
index = 0
for index, capital in enumerate(capitals, start=1):
print(index, capital)
We’ve specified another argument called start and set the value to be 1, and you can start counting from any number you want.
4. Switch Values
Sometimes you need to switch the values of two variables in your program, and most people don’t know the easy way of doing it. Let me show you first how most developers do this technique:
a = 22
b = 33
print(a, b)
c = a
a = b
b = c
print(a, b)
The above example will work correctly, and you need to create another variable, c. So you can use it to switch between the two variables. It was a little bit confusing, but this simple trick will help you do it with ease:
a = 22
b = 33
print(a, b)
a, b = b, a
print(a, b)
Just assign every value of a variable to the other variable value, and it’s done, which is more uncomplicated and straightforward.
Conclusion
Thanks for reading! These few tricks we’ve discussed will help you be more productive in your python development and make you develop your code more professionally. We will discuss more tricks in the following tutorial of this series.