0% found this document useful (0 votes)
11 views

Python Interview Question Solutions

interview questions of python actually faced by students

Uploaded by

shantanuspathak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Interview Question Solutions

interview questions of python actually faced by students

Uploaded by

shantanuspathak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Interview Questions

(Actually faced by students)

Q Explain Difference between python and R


Ans :

Python

- Python's major application is in automation of tasks

- Python is used for Big Data processing like in pySpark tool

- Python Supports parallel processing

- Python has large library support for Machine Learning, Deep Learning and WebApp development

- Python supports Object Oriented Programming

- Python supports many file formats like xml, JSON, txt, csv, excel, etc

- Python is easy to learn

- Python has consistent behavior accross updates

- Python can easily integrate with databases SQL / NoSQL

- Python is scripting language

R programming

- R's major application is statistical analysis

- R has wonderful vizualization libraries

- R is not ideal for Big Data because all data needs to be together.

- R supports limited file formats like txt, csv, excel

- R has very minimum support for parallel processing

- R support limitedly for Machine Learning and Deep Learning

- R mostly creates desktop applications and WebApp development is very limited

- R is difficult to learn

- R version updates sometimes doesnt come with backword compatibility of codes, so version
updates may break R scripts very easily

- R has limited support for database integration

- R is scripting language
Q Explain Difference between list and tuple

Python Lists Python Tuples


List are mutable Tuples are immutable
Iterations are time-consuming Iterations are comparatively Faster
Read-only access to elements is best accomplished
Inserting and deleting items is easier with a list.
with a tuple data type.
Lists consume more memory Tuple consumes less memory than the list
Tuples tend to be faster because of immutable static
Lists are slower because they are mutable and dynamic
nature
Lists are NOT Hashable because they are mutable Tuples are Hashable
A tuple does not have many built-in methods because
Lists have several built-in methods.
of immutability
A unexpected change or error is more likely to occur in In a tuple, changes and errors don't usually occur
a list. because of immutability.
Similarity in Lists and Tuples
Both can have +ve and –ve indexing
Both can be iterated in for loop
In both, elements can be read using index directly, index starts from 0
Both can store elements of any data type in them

Q Explain exceptions handling in python


Exception is un-expected event occurring at run time. Such even may cause program to stop
working. So, we need to handle them for smooth functioning of complete program.

In python exception handling is done using try-except blocks

In try block we write code which can possibly cause exception

In except block we handle the exception when it comes

There is else block. It is optional. It is used when no exception occurs in try block

Also, there is finally block. It is optional. It is used to perform operations which needs to be
completed in both cases, either exception or no exception. For example in finally block we write
code to free resources like files, memory, database connections.

Q For loop and While loop difference?


For loop While loop
While loop is used to repeatedly execute a
For loop is used to iterate over a sequence
block of statements while a condition is
of items / an iterator.
true.
While loop is used when the number of
For loops are designed for iterating over a iterations is not known in advance or when
sequence of items. Eg. list, tuple, etc. we want to repeat a block of code until a
certain condition is met.
While the loop requires an initial condition
For loop require a sequence to iterate over.
that is tested at the beginning of the loop.
For loop is typically used for iterating over While loop is used for more complex
a fixed sequence of items control flow situations.
For loop is more efficient than a while loop
While a loop may be more efficient in
when iterating over sequences, since the
certain situations where the condition being
number of iterations is predetermined and
tested can be evaluated quickly.
the loop can be optimized accordingly.

Q How to select columns in pandas ?


- Select all column names  df.columns()
- Select a given column -> df[‘column_name’] OR df.loc[:,’column_name’]
- Selecting list of columns -> df[[‘col1’,’col3’,’col5’]] OR df.loc[:, [‘col1’,’col3’,’col5’]]
- Selecting column based on index from index 2 to 5-> df.iloc[:,2:5]
- Selecting columns starting from column ‘col1’ to ‘col5’ in sequence -> df.loc[:,’col1’:’col5’]
- Select all columns and all rows  df OR df.loc[:,:] OR df.iloc[:,:]

Q How to import CSV in python ?


- Using Pandas
- In pandas there is read_csv function
- It reads a csv file and returns a pandas dataframe

- Using csv library

# importing csv module


import csv
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open('filename.csv', 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = next(csvreader)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
print("Total no. of rows: %d" % (csvreader.line_num))

Q Explain set operators in Python ( like union, union all, minus, intersect)
A set is a collection of unordered elements. Each element must be distinct and immutable.

However, a set itself is mutable.

In a set element can of type bool, int, float, complex, tuple, frozen-set

We cannot have set as element of another set, because set is mutable

Frozen-set are immutable sets

Set is not-Hashable , because they are mutable

Operations in set

add() : adds an immutable element to the set

update() : adds multiple immutable elements to a set

discard(): removes given element from set. Doesn’t throw exception if element is not present

remove():removes given element from set. Throws exception if element is not present

pop() : remove and return a random item from set

clear() : delete all elements of a set

union() : union of two sets is the set of all the elements, without duplicates, contained in either or
both of the sets. We can do this using | operator

intersection() : intersection of two sets is the set of all the elements that are common to both sets.
We do this using & operator.

difference():difference between the two sets is the set of all the elements present in the first set but
not in the second. We do this using - operator

symmetric_difference(): it selects all items in both sets without common elements in both sets.

Q Explain use of python in your project. Explain code of your project line by line.
Ans : Show your project code

Highlight specially on your contributions in coding

If you have used libraries, then discuss how you have written code to use the libraries.

Preferably discuss user defined functions which you have written yourself

Write many comments in the code for better understanding purpose

You might also like