python-development

6 practical tricks every Python developer should have

The clear, versatile, easy to learn, and it has plenty of useful libraries for different tasks make Python a popular programming language. Python programmers are in high demand due to their versatility in handling web development, cybersecurity, creating software prototypes, using data science, and much more.

Python has extensive features that can help you write code that is elegant, compact, and expandable. We have compiled 6 practical tips every Python developer should have :

List Comprehensions

A developer is expected to know about list comprehensions. List comprehensions can create lists that can replace for loops(Nested),filter(), the map() and reduce() functions.Especially when you need to use Data Science, you need to know a lot like filtering column names, flattening a matrix, or removing vowels from a list.

Let’s take an example which includes an output list that contains squares for all numbers 1 to 10

Output list using loops

output_list = []

for var in range(1, 11):

output_list.append(var ** 2)

print(“Output List using for loop:”, output_list)

Output List using for loop: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Output list using list comprehension

list_using_comp = [var**2 for var in range(1, 11)]

print(“Output List using list comprehension:”, list_using_comp)

Output List using florist comprehension [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Enumerations

To keep track of iterations, Enumerations is another trich which Python developers should be aware.

list1 = [‘A’, ‘B’ ,’C’, ‘D’]

e_list = enumerate(list1,2)

print(list(e_list))

Output: [(2, ‘A’), (3, ‘B’), (4, ‘C’), (5, ‘D’)]

Zipping

Another function that comes in handy with Python is Zip() function. Zip is used to combine two or more lists in a single variable. Have a look at the example

a = (“John”, “Hillary”, “Mike”)

b = (“Jenny”, “Monica”, Christy”)

x = zip(a, b)

print(tuple(x))

Output : ((‘John’,’Jenny’),(‘Hillary’,’Monica’),(‘Mike’, Christy’))

Type annotations

The type annotations method helps to sort out differences between variables and data types. If you mix variables that have different data types, it will resolve the issue or raise the exception. If you are using two integers and multiplying those variables,

a = 4

b = 5

print(mul(a, b))

Output: 20

But the things are different when one of the variables in a list and the other is an integer

a = [1, 2, 3 ,4]

b = 2

print(mul(a, b))

The output will be [1, 2, 3, 4, 1, 2, 3, 4]

The type annotation will help you define data in the documentation itself so that it enables the developer to use the correct data type

def mul(a: float, b: float):

return a * b

Counting items

To know how many times a certain value comes in a list, developers use randint() function.

import random

print(random.randint(3, 8))

You will random number each time you run, And both 3, 8 numbers are included in it.

Parameter expansion

The parameter expansion in Python allows one to expand individual values before submitting the function. You just need to add a * at the beginning of the list’s name and you can directly pass the list to function. By adding * to a list or tuple and ** to a dictionary when calling a function, then elements are passed to arguments

Final thoughts

The blog mentions 6 practical tricks every Python developer should have. Implementing these in the code practices will help generate an elegant, concise, and expandable code.

Leave a Reply