Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python built-in functions

Python provides a lot of functions to make our work easy. Among these functions some functions are very powerful and can give you effective result and make you work very easy. Let's discuss some python functions which you must know.

zip() function

Zip function is used to concatenate two lists, sets, etc. It will concat according to the index number.
Example: Suppose you have two lists. Now you want to join those lists and make one list. You want to join those list in such a way that first list first index value will be present at first then second list first index value. Then first list second index value then second list second index value. To do this you will use zip function.

Example:
Input
v=[1,3,5]
x=[2,4,6]
c=list(zip(v,x))
Output
[(1,2),(3,4),(5,6)]

lambda() function

Lambda is a Python built-in function. We can write a big function or code just in one line using lambda function. It makes the code shorter. You can use condition or run for loop and also declare variables inside lambda function.

Example:
Suppose you want to do sum of 5 with the user input value using a function. To do this you have to use at least 3 lines of codes. But you can do this in one line using lambda function.

How to write lambda function?

First, write lambda then parameters and expression
Syntax:
lambda parameter:expression

Example:1
Input
X=15
v=(lambda X:X+10)(X)
print(v)
Output
15

In the example, we have a variable X. Now we want to sum of 5 and 10. To do this with the lambda function we have to write lambda X variable then the formula X+10 and in the end give access to the variable to the lambda function. lambda function will take the value from the end where we gave access to the variable. Then the value will pass through the parameter and then the formula and then we got the result 15.

Example:2
Input
a=15
b=15
v=(lambda a,b:a+b)(a,b)
print(v)
Output
30
Example:3
Input
list1=[1,2,3,4,5]
output=lambda a: a%2!=0,list1
print(output)
Output
(< function < lambda> at 0x0000020F9BCDCC10>, [1, 2, 3, 4, 5])

enumerate() function

This function is used in a for loop to track or get the position of items.

Example: Suppose you have a list and we know that the each value of a list has index number. Now using a for loop you want to get each element of the list and also get the index number of that particular element of the list. To do this you will use enumerate function.

Example:
Input
list1=["R","A","F","S","U","N"]
for pos, list2 in enumerate(list1):
  print(f"{pos}----->{list2}")
Output
0----->R
1----->A
2----->F
3----->S
4----->U
5----->N

In the example we have a list. Now we wanted to print all the element of that list one by one and with each element of the list we also want the index number of that element. To do this we used enumerate function.

map() function

This function works with iterable means list, tuple, etc. This function takes two parameters, the first parameter is function and the second parameter is iterable means list, tuple, etc. Map function also returns the output as iterable means list, tuple, etc.

Example:1
Input
list1=[1,2,3,4,5]
def square(x):
  return x*x
output=list(map(square,list1))
print(output)
Output
[1, 4, 9, 16, 25]

Look in the example 1 we have list which contain numbers. Now we want to create a list where the elements will be the square of each number of the first list. To do this we used map function.

Example:2
Input
list1=["1","2","3","4","5"]
output=list(map(int,list1))
print(output)
Output
[1, 2, 3, 4, 5]

Look in the example 2 we have list which contain numbers as string. Now we want to make all then numbers string as integer number. To do this we used map function. After applying map function we got a new list which contains all integer form of the first list number string.

Example:3
Input
list1=["1","2","3","4","5"]
output=list(map(lambda a: a*2,list1))
print(output)
Output
['11', '22', '33', '44', '55']

filter() function

The filter function is used to filter iterable means list, tuple, etc. If we give a condition and those elements which can full fill the condition will stay and others will be removed. This function works only with iterable means list, tuple, etc. This function takes two parameters, the first parameter is function and the second parameter is iterable means list, tuple, etc. Map function also returns the output as iterable means list, tuple, etc.

Example: Suppose you have a list. In that list you have numbers from one to ten. Now you want to create a list from the existing list and want those elements of the existing list which are even numbers. To do this you will use filter function.

Example:1
Input
list1=[1,2,3,4,5]
def odd(x):
  return x%2=0
output=list(filter(square,list1))
print(output)
Output
[1, 2, 3, 4, 5]

In the example 1 we created a list using the existing list value and in the new list we have all those values which are even numbers of the first or existing list.

Example:2
Input
list1=[1,2,3,4,5]
output=list(filter(lambda a: a%2!=0,list1))
print(output)
Output
[1, 3, 5]

In the example 2 we created a list using the existing list value and in the new list we have all those values which are odd numbers of the first or existing list.

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.