5 Must know Python tools!
Python is a strong, flexible, and user-friendly programming language. It's straightforward to read, learn and write. Compared to other prominent languages like C/C++ and Java, you require fewer lines of code to accomplish the same purpose.
Here I have compiled a list of 5 Must know Python tools that will help you in your development/data science journey.
1. Iterating over Range Objects:
Let's get into the basics and understand the Range() function and how it works.
Range() function returns a sequence of numbers. The most important use of range functions is to iterate through a sequence of numbers using loops etc.
Here the range function returns the sequence of numbers from 0 to 4.
Range(5) simply means to Range(0,5,1)
0 – Refers to the start of the sequence. By default 0 will be the start of the sequence.
5 – Refers to the end of the sequence. Range always includes the first element of the sequence and excludes the end of the sequence. Hence 5 was not included in the Output.
1 – Refers to the step size. By default, 1 will be the step size.
Here is another example of iterating over range objects. “i” is the loop variable or iterator variable and range(11) is iterable. Iterable is any object which can be iterated over, and this also refers to the number of iterations that we desire. In this example, we are iterating over the range of numbers from 0 to 10 and identifying whether the number is even or odd.
for i in range(11):
if i%2 == 0:
print(str(i) + " This is a Even number")
print(str(i) + " This is a Odd number")
The output of the above code looks like this.
Let's see how python produced this Output. range(11) will produce an output sequence 0,1,2,3,4,5,6,7,8,9,10
Iterator variable i = 0
In the next line of code, it checks for a condition i%2 == 0. This means that, if the modulus of i is equal to 0 then i is an even number else it's an odd number.
Iterator variable i = 1
In the next line of code, it checks for a condition i%2 == 0. This means that, if the modulus of i is equal to 0 then i is an even number else it's an odd number. Here the condition is False and prints "This is an odd number".
Iteration continues until it reaches the end i=10.
2. Nested For loops:
Nested loops are one of the best ways to loop a loop. This logic exists in all programming languages. Here we can discuss how nested For loop works in python. If a loop exists within the body of another loop then it is called a Nested loop. Nested loops can be used if we want to execute the inner loop multiple times. The outer loop will control the number of iterations that the inner loop will undergo.
for i in range(2):
for j in range(5):
We need to understand the below points to understand how nested FOR loops work.
In this example, the outer loop can be taken as an i loop and the inner loop as a j loop
i loop will have 2 iterations. i.e one iteration for 0 and one iteration for 1 as range(2) will produce 0,1.
During the first iteration of i loop, i.e for i = 0, inner loop j will be triggered and a sequence of numbers 0 to 4 will be generated.
For the next iteration of i loop, i.e for i= 1 inner loop j will be triggered again and a sequence of numbers from 0 to 4 will be generated.
In simple words, all combinations of i and j have been produced as a result using the nested loops.
The output of the above code would look like this.
3. Triple Nested Loops:
Triple nested loops also work in the same way as above whereas here there are 3 loops.
profit = [1000,1200,1500,1100]
for i in ['Product A','Product B']:
for j in profit:
for k in time_horizon:
Let's understand how this works.
In the above example, We have two products, Product A and Product B. We have stored the Expected Profit of products A and B in a list called "Profit". Let's see how we can calculate the expected profit monthly, quarterly, and annually for products A & B.
For achieving this in Code, Stored the time horizon into a tuple called "time_horizon"
time_horizon = (1,3,12)
We are trying to multiply the j * k (profit * time horizon) to calculate the expected profit of monthly, quarterly, and annual expected profit of products A & B.
The output of the code would look like this. We need to understand the below points to know how triple nested loops work.
We are using 3 loops i, j, k. The number of elements in the output depends on the number of elements in each loop.
During the first iteration, i loop (for product A), j loop will be triggered (the first element in j is 1000) and k loop will be triggered and calculates j * k. ( i.e 1000*1 , 1000 * 3, 1000*12, 1200 * 1, 1200 * 3, 1200 * 12 and so on)
Interesting Isn't it?
4. List Comprehension:
List comprehension is the most widely used feature in Python. List comprehension is used as an alternative to the regular FOR loops. The main advantage of list comprehension is, it requires very fewer lines of code to achieve the same results as using FOR loops with a lengthier code.
Let’s dig deeper to understand how list comprehension works.
The above example shows the comparison between the traditional For loops and list comprehension. In the traditional For loop, we created an empty list called list1, and we wrote a For loop iterating over the range of 0 to 9 sequence and multiplying each element in a sequence into 2, and appending the calculated values as elements into the empty list “List1”. The same is achieved using List comprehension in a single line of code.
Here is another example:
We can also include if conditions in the list comprehension like below. Where we calculated the raise to the power of 3 for all the odd numbers between 0 to 9.
5. Lambda Functions:
Lambda functions are also called anonymous functions.
Features of Lambda functions are
1. Can have one or many parameters but can contain a single expression only.
2. Allows you to write just one line of code to include a simple functionality in a more complex expression.
The above example shows the comparison between traditional functions and lambda functions. Lambda functions allow you to have the expression and arguments in the same line of code, unlike traditional functions.
When to Use Lambda functions:
When the function is expected to be available in your code only for short period.
When a function is being conceded as an argument for a superior function in respect of the order of the function. So this is a scenario where one function picks another function as an argument of it.
These are the 5 tools in python that can be extremely helpful when coding. It’s very important to understand the basics and learn how to iterate over the different objects/sequence of a programming language to code in a better-optimized way. We cannot completely avoid the traditional approach. That being said, there are certain cases where you can use the alternative approach. Thanks for reading!