SUMMER TRAINING Free Tutorials  Go To Your University  Placement Preparation 
Project Based Best Summer Training Courses in Jaipur
Join our Telegram Channel To take free Online Courses
0 like 0 dislike
98 views
in Python Programming by Goeduhub's Expert (3.1k points)
recategorized by

Frequently asked Python Interview Questions and Answers in detailed explaination for freshers and experienced.

2 Answers

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

In this article I assume that you know, what is python and all basic topics regarding python. If not then you can visit the page - Python Basics  (Here all the topics of Python are listed, you can click on the topic you want to read)

Question 1:  Why do we study python ?  (Features of Python)

Answer: Features of python language is basically key of attraction to study python. Features of python that make python to study. (Python basic introduction and features)

  1. Easy 
  2. Open Source 
  3. Object Oriented 
  4. Integrated 
  5. Interpreted 
  6. Extensible 
  7. GUI support 
  8. Python Libraries 
  9. Expressive language 
  10. Portable 

Question 2 : How to display current year, date and time in python ?

Answer: In python we have a module called datetime. Official Documentation on python module datetime (Datetime) , In the same way we can also use calendar and time in python.(See the datetime documentation)

Let's see with a practical.

#current time and year 

import datetime

current_time = datetime.datetime.now()

print (current_time.strftime("%Y-%m-%d %H:%M:%S"))

Output: 

2020-11-06 08:35:21


Question 3: What is difference between a Library , Module and Package in Python ? 

Answer : Every time when we start a python program we start with importing some library and module. Now what is difference between these term, are they same ?

Module: Module basically a python file meaning a (.py) file. In short modules are collection of functions and we can use a module in python by importing it.  Here is a simple example of a module.   (Modules in Python)

Q: Write a program to calculate area of circle ? (input - radius)

Ans:  We know that area of circle is = pi*radius*radius 

As we know pi has specific value and radius square is a mathematical operation. For mathematical operations we have math module in python.  

#undersatnding a  module 

from math import pi

r = float (input ("radius of circle = "))

Area = pi*r*r

print("Circle Area",Area)

Output:

radius of circle = 2 Circle Area 12.566370614359172

So, basically here we used pi from math module. In conclusion modules are used to reduce length of code and to avoid writing same code over and over. we can write it once and can use by importing wherever we need it. 

Packages: Collection of modules is basically a package. In simple terms a Package contain a group of module together. A package is basically a directory with Python files (modules) and a file with the name __init__ . py.

Library: Library and packages are nearly same, but the slight difference between them is a library is collection of packages. 

Numpy , Scipy , Pandas etc....


Question 4 : Write a python program to swap cases of a input string ?

Answer:  String - Click here to see the basics of string in python 

#converting case of a characters 

string= str(input("Entre a string= "))

r_string = ""   

for i in string:

  if i.isupper():

    r_string += i.lower()

  else:

    r_string += i.upper()           

print (r_string)

Output:

Entre a string= Python pYTHON


Question 5: What is difference between a comment and docstring in python ?

Answer: 

Comment: A comment in python start with hash symbol (#)  . It is a single line comment. 

Docstring: Docstring is nothing but  multiline string written in between triple quotes (Documentation of string).

Note that if we don't put a string into a variable it behaves like a comment in python.

Simple Example 

#write a single line comemnt 

a,b=3,2

var="a string not a comemnt"

"""write a multiline 

comment , it's a comment not string """

c=a+b

#print(c)

 


Question 6:  Write a program to remove duplicates from a list. 

Answer: List in Python -  Click here to see basic concepts of list 

               Python Set - Click here to see the basic concepts of sets 

As we know that a set only consists unique values not duplicate. We can easily solve this problem by converting a list into a set and then list. 

You can solve this problem with other method as well depends on your imagination.

# removing duplicates in list 

duplicates = [1, 2, 3, 1, 2, 5, 6, 7, 8 ,4 ,8 ,9 ,6 ,5 ,3]

new=set(duplicates)

print("a set= ", new)

not_duplicates=list(new)

print("a list= ", not_duplicates)

Output

a set= {1, 2, 3, 4, 5, 6, 7, 8, 9} a list= [1, 2, 3, 4, 5, 6, 7, 8, 9]


Question 7: Difference between sorting and sort in python? 

Answer :  Both method/function used for sorting in python. The work of both is same then what is the difference between these two.

Let's try to understand it with an example

#sort and sorted in pyhton list

lstr=['name','city','country','planet','galaxy']

x=sorted(lstr)

y=lstr.sort(reverse=True)

print(x)

print(y)

Output:

['city', 'country', 'galaxy', 'name', 'planet'] None

#sort function only

lstr=['name','city','country','planet','galaxy']

lstr.sort()

print(lstr)

Output

['city', 'country', 'galaxy', 'name', 'planet']

To understand the difference between sort and sorted see the above two example carefully. As you can see the sort function does not return the list, it just directly change the values in original list rather then creating a new list. In the above examples when we print the list after using sort(). It return none , meaning sort() method just change the original list (see the second example)

On the other hand sorted function create a new list without changing the original one. means it store a copy of original and new sorted. 

Note that by default sorting is done ascending order if you want to change the order pass the parameter in both case reverse= True. 

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by

Question 8: Difference between append and extend in python list ? 

Answer: 

Append: Append method in python list is used add an element at the end of the list. 

Extend: extend() method concatenates the first list with another list (or another iterable). 

iterable: Iterable is anything that can be loop over. (string, tuple, list etc...)

Q: Write a Python program to extend a list without append.

#extend a list 

x = [10, 20, 30]

y = [40, 50, 60]

string="goeduhub"

x.extend(y)

print(x)

x.extend(string)

x

Output

[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'g', 'o', 'e', 'd', 'u', 'h', 'u', 'b']

Example of append in list

#append an element to list

x = [10, 20, 30]

x.append("hello")

x.append(4)

print(x)

Output:

[10, 20, 30, 'hello', 4]

Note that append method add only one element at a time at the end of list and extend method concatenates the other iterable (list, tuple, string, set ) to the first one.


Question 9: How to insert an element into python list at specified position ?

Answer: insert method is used to insert an element at specified position in python list.  As we know append and extend  methods are not use a specified position. 

see the example below

#insert an element to list

x = [10, 20, 30]

x.insert(0, 'red')

x.insert(2, 452)

print(x)

Output:

['red', 10, 452, 20, 30]

Question 10: What is difference between object oriented language  and Procedural  Programming language ?

Answer :  We know that python support both Object  Oriented and Procedural  Programming language. But what is OOP and POP ?

Both approaches used to construct a programming structure.

Procedural oriented programming: As the name suggest,  Procedural oriented programming follows a step-by-step approach to break down a task into a collection of variables and function. Each step follow the previous step so that computer can understand what to do after each step. POP is less secure then OOP. Procedural programming follows top down approach.

Example: Pascal , C etc...

Object oriented language: 

Object oriented programming is based on real world. The concept is similar as we all time surrounded object. 

For example if you are working on a computer you are surrounded by objects like computer , water bottle , pen etc... and the work of particular object is fixed and limited. For example we use pen to write something not to drink. 

The similar concept is apply on the object oriented programming. 

In object oriented programming, program is divided into small parts called objects.

Object oriented programming follows bottom up approach.

Example: C++, Java, Python, C# etc.

Question 11: Write a python program to show keys and values of a dictionary in pair also sort it by keys? 

Answer : Python Dictionaries : Click here to see the basics of python dictionaries 

#python dictionary keys and values in pair

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

print(sorted(d.items()))

Output:

[(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]

Note: This is a very simple dictionary question but  why I choose this question is just to show you the item() function in dictionary. 

You can see that item() in dictionary return the key, value pair. Sorted function sort a dictionary by its keys as you see in the output.

Question 12: Write a python program to merge two dictionaries ?

Answer :  

#merging two dictionaries 

d1 = {'a': 10, 'b': 20}

d2 = {'x': 30, 'y': 40}

d1.update(d2)

print(d1)

Output:

{'a': 10, 'b': 20, 'x': 30, 'y': 40}

Note: Update method is used to entre or insert a new key, value in dictionary. 

Question 13: Difference between pop() and del in python ?

Answer :  pop() - pop remove an item at particular index and return it. 

                del - del keyword just remove an item or an object (dictionary, list etc...).

See the example below (pop() and del on dictionary in python)

#pop method in python 

d1={1:'a',2:'b',3:'c',4: 'd',5:'e'} 

d1.pop(3)

Output:

'c'

#priting d1 after popping key 3

print (d1) 

Output: 

{1: 'a', 2: 'b', 4: 'd', 5: 'e'}

#delete in python

d1={1:'a',2:'b',3:'c',4: 'd',5:'e'} 

del d1[4]

print(d1) 

Output:

{1: 'a', 2: 'b', 3: 'c', 5: 'e'}

Note: You can see in the examples that pop() and del both remove the items form dictionary. But in case of pop(), pop() return the popped item ('c' in this case). 

Question 14:  Write a Python program to remove an empty tuple(s) from a list of tuples.

Answer:  Python tuples : Click here for basic concept of python tuples 

#removing empty tuples from list 

L = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]

t=()

l=[]

for i in L:

  if i is not t:

    l.append(i)

print(l)

Output

[('',), ('a', 'b'), ('a', 'b', 'c'), 'd']

Question 15: Write a python program to add an element  in tuple ?

Answer :  As we know that tuples are immutable object type in python we can not add add new elements means we can't update a tuple. We can create a tuple . Let's see how it works 

#creating a tuple

tup = (1,2,3,4,5,6,6,7,8,9) 

new_tup = tup + (10,)

print("adding elements in tuple",new_tup)

#adding element in a specific index

s_tup = (1,2,3,4,5,6,6,7,8,92,44,67,865,44)

s_tup = s_tup[:4] + (11,34,56) + s_tup[:6]

print("adding elements in tuple at specific index",s_tup)

Output

adding elements in tuple (1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10) 

adding elements in tuple at specific index (1, 2, 3, 4, 11, 34, 56, 1, 2, 3, 4, 5, 6)

Top asked python interview questions and detailed explained answers. (Part2)

Our Mentors(For AI-ML)


Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune

NISHA (IIT BHU)

Ms. Nisha

An alumna of IIT-BHU

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...