Warning: Use of undefined constant REQUEST_URI - assumed 'REQUEST_URI' (this will throw an Error in a future version of PHP) in /home/epmwis5/public_html/wp-content/themes/publisher/functions.php on line 73
Must know python code tips and tricks for interviews and for beginners

11 Must Know Tips for excelling in Python Programming

Python Coding Essential Tips

Understanding the nuances of any coding language is not easy. It comes with lots and lots of experience. Here are the 10 must know python coding tips for you to know if you are starting out new or if you are preparing for your upcoming Python interviews.

1. Python versions

It is extremely important to know the python versions. There has been a major debate on which version to use. Some say python 2 is better while others say python 3 is better. The final version of python 2, python 2.7 was
released in 2010 while python 3.0 came out in 2008. The latest release of python 3, python 3.6 was released in 2016. There are several packages like six that allow you to easily migrate from python 2 to 3 with very few
manipulations. Since all the new standard library improvements will now be available by default on python 3 only, it is better to use python 3 if you can do exactly what you want with python 3. Although python 3 is a consistent
language but it has some downsides too. It is very limited to third party modules and most major frameworks still run on python 2.

2. Libraries

Following are the most used python libraries that you must know of before starting coding in python:

Pandas: pandas is an open source python library that provides easy to use data structures and data analysis tools. It also provides tools for loading data form different file formats and merging and joining of data. Pandas provides us the facility to aggregate and transform data using group by.

pip install pandas
import pandas as pd
df=read_csv(‘data.csv’)
print(df)

Numpy: Numpy or numeric python is used for scientific computation in python. It provides tools for working with multidimensional array. Numpy also provides ways for indexing in arrays.

import numpy as np
a=np.array([[1,2],[4,5]])
b=np.array([[4,3],[9,7]])
print(a+b)
[[5,5]
[13,12]]

BeautifulSoup: BeautifulSoup is a python library that is helpful in scraping data from HTML and XML files. It automatically converts incoming documents to Unicode and outgoing documents to UTF-8.

from bs4 import BeautifulSoup
import requests
url=”www.website.com”
request=requests.get(http://+url)
data=r.text
soup=BeautifulSoup(data)
for val in soup.find_all(‘a’):
print(link.get(‘href’))

matplotlib: matplotlib is a python library that is used to plot interactive graphs. Data visualization is necessary to identify useful pattern in data.

import matplotlib.pyplot asplt
x= [1,2,3,4]
y=[2,4,6,8]
plt.plot(x,y)
plt.show()

3. Lambda Expressions- Lambda expressions are an expression of anonymous function. Python supports functional programming where you can pass functions to other function.

For example –

a=[1,4,8,6,5]
b=list(map(lambda x: x*5, a))

output:
[5,20,40,30,25]

4. Enumerate function- It is a built-in function in python used to keep a count of iterations. Enumerate method adds a counter and returns an object which can then be used in loops.

For example –

a=[“using”, “enumerate”, “in”, “python”]
for counter, val in enumerate(a):
print(counter, val)

output:
1 using
2 enumerate
3 in
4 python

5. Getting user input from keyboard- Python allows you to take input from keyboard using input() function. Although by default the input function allows you to read string, you can explicitly convert string to int.

For example –

File_location=input(“Enter the file location”)
>C://file/data.csv

Output:
C://file/data.csv
a=int(input(“Enter the value of a”))
>20
Output:
20

6. Count- Count Method returns the count of frequency of occurrence of any item in the list. You will often come across this in python.

 

my_list=[1,2,3,2,1,2,1,4,1,3]
for val in set(my_list):
print(val,’:’, my_list.count(val))

Output:
1 : 4
2 : 3
3 : 2
4 : 1

my_list=[[1,2,3],[1,4,5],[1,2,1]]
for val in my_list:
for i in val:
new_list.append(i) //this is how you convert a list of list

//into list

for i in new_list:
print([i,new_list.count(i)])

Output:
[1,4]
[2,2]
[3,1]
[4,1]
[5,1]

7. Slicing- Slicing is the process of taking a subset of the total data. It is commonly applied to classification problems when you want some part of the data to be training data while the other part to be test data.

list[start:end]
start is the start index of the list
end is the last index of the list
For example

A=[10,3,26,23,5,8,92,5,7,35]
B=A[3:6]
print(B)

Output:
[23,5,8,92]

8. Sorting- Sorting is a built-in function in python that allows you to arrange elements in an ordered manner. Python allows you to sort the list according to any element in the list.

For Example-

my_list = [3, 6, 8, 2, 78, 1, 23, 45, 9]
sorted(my_list)
Output:
[1, 2, 3, 6, 8, 9, 23, 45, 78]
a=[[a,1,2],[b,8,6],[c,3,9],[d,2,0]]
a.sort(key=lambda x: x[2], reverse=True)
Output:
[[c,3,9],[b,8,6],[a,1,2],[d,2,0]]

9. Converting list to string-  You can convert list to string using join keyword. In Python coding Join method returns the concatenation of elements in the list.

For example

a=['hello', 'world']
print(' '.join(a))
Output:
hello world

10. Swapping numbers with one line of code- In Python coding Swapping refers to exchanging the values of the variables.

x,y=7,5
x,y=y,x
x,y
Output:
5,7

Python programming also allows you to use arithmetic functions in the following way:
x=x+y
y=x-y

11. Element-wise multiplication of two lists – Element wise multiplication means to multiply two lists together by value.

For Example

x= [4,5,2,8,10]
y= [1,0,3,4,5]
res = [i*j for i,j in zip(x,y)]
print(res)
Output:
[4,0,6,32,50]

Hope these tips will be helpful to you. Do you have any similar tips that you want to share? Please post and let us know.

Comments are closed.