https://medium.com/@pranjalgoyal13/best-tips-to-increase-your-productivity-in-python-856fb038a676
Best Tips to Increase Your Productivity in Python | by Pranjal Goyal | Medium
Python provides many inbuilt functionalities which help in writing cleaner code. People are generally unaware of some productive tips that are used in Python to improve the quality and readability of code. π©βπ»
Letβs make your code more Pythonicβ¦
Cool Logo Right?
Best Productive Tips are as follows:
Using List Comprehension:
Suppose you want to add elements to a list from an existing one, the conventional way is to loop through the list and then append the values to the new list.
But Python offers a convenient way to handle List creation as well as putting values in it. It looks cleaner because of the shorter syntax and by using it we can get rid of the complex blocks of code with the utility of a single for loop within a list.
Example:
Conventional Way π:
students = ['Sam', 'Billy', 'John', 'Lucas']
new_students = []
for student in students:
new_students.append(student)
print(new_students)
Python List
Python Way π₯:
students = ['Sam', 'Billy', 'John', 'Lucas']
new_students = [student for student in students]
print(new_students)
Python List
Making strings using F-Strings:
Not the word that you are thinking of π . F-String was introduced in Python to make string interpolation simpler.
This is used to avoid string concatenation and string conversion which is still used by many to print variables in a string.
The reason fstring is better in formatting than the %s or .format() method is the readability and simplicity it provides (when strings get very large with increasing variables) as compared to other known methods.
To create an f-string, just append the string with f. Let us look at an example.
Conventional Wayπ:
name = 'John Doe'
age = 21
gender = 'Male'
print("The name is " + name + " age: " + str(age) + " gender: " + gender)
#or
print("The name is %s age: %s and gender: %s" % (name, age, gender))
Printing string in python conventional way
Python Way π―:
name = 'John Doe'
age = 21
gender = 'Male'
print(f'The name is {name} age: {age} and gender:{gender}')
F String in Python
Enumerate Looping:
To track both index and value while iterating a list, the conventional range() function is used.
But there is a better way in Python to loop a list using enumerate() function.
Example:
Conventional WayπΆ:
students = ['Sam', 'Billy', 'John', 'Lucas']
for i in range(len(students)):
print(f'The index : {i}, Name: {students[i]}')
Looping Index-Based
Python Way π:
students = ['Sam', 'Billy', 'John', 'Lucas']
for index, student in enumerate(students):
print(f'The index : {index}, Name: {student}')
Looping with enumerating
Using Generators:
It is a more memory-efficient way to store and return data only when it is demanded (lazy loading). Let's say you want to sum a large range of numbers, so it will be a better choice to use the generator.
To use a generator use () around the list instead of [].
Example:
Conventional Wayπ:
get_range_sum = [num for num in range(100000)]
print(sum(get_range_sum))
Without Generation
Python Way πΎ:
get_range_sum = (num for num in range(100000))
print(sum(get_range_sum))
With Generation
Below are productive hacks that apply to every language
Exception Handling:
Using Exception handling is very useful as it doesn't stop your code when an error occurs and provides an alternate route for it.
If the exception is not handled the program will abruptly terminate which can lead to various handling issues.
There are mainly two times of exceptions:
- Checked Exception: Occurs at compile time and the code will not compile unless it is handled.
- Unchecked Exception: Occurs at Run-time, the most famous example is DivisonByZeroError.
I have written a dedicated Medium Article on Exception Handling which covers in detail about this.
Finding bugs using Debugging:
To find what went wrong with the code, debugging can help to resolve the issue much faster.
Conventionally people use print() statements to debug the slower code as well will become difficult as the code base grows.
In debugging we use breakpoints to debug applications which will pause the execution of the program at that point and all variables, and stack track is visible in the debugger console.
I have written a dedicated Medium Article on Basics of Debugging.