Python Interview Questions
Python Interview Questions
Categories
Top 100 Python Interview Questions You Must
Prepare In 2019
Arti cial 337.2K Views
Intelligence
Visualization
Big
Data
myMock Interview Service for Real Tech Jobs
Blockchain
Cloud
Computing
Cyber
Security
Data
Science
Data
Warehousing
and
ETL
Databases Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
Get interviewed by leading tech experts
Front
End Python Certi cation is the most sought-after skill in programming
Web domain. In this Python Interview Questions blog, I will introduce
Development
you to the most frequently asked questions in Python interviews.
Our Python Interview Questions is the one-stop resource from
Mobile
Development where you can boost your interview preparation. We have 100+
questions on Python Programming basics which will help you with
Operating di erent expertise levels to reap the maximum bene t from our
Systems
blog.
Programming
Edureka 2019 Tech Career Guide is out! Hottest job roles, precise
& learning paths, industry outlook & more in the
Frameworks guide. Download now.
Project We have compiled a list of top Python interview questions which
Management are classi ed into 7 sections, namely:
and
Methodologies
Basic Interview Questions
Subscribe to our Newsletter, and get personalized
×
OOPS Interview Questions
Robotic recommendations.
Basic Python Programs
Process
Automation
Python Libraries Interview Questions
Sign up with Google
Web Scraping Interview Questions
Software Data Analysis Interview Questions
Testing Multiple
Signup Choice Questions (MCQ)
with Facebook
Systems Before moving ahead, you may go through the recording of Python
& Interview QuestionsAlready
wherehave
our an account? has
instructor Sign shared
in. his
Architecture experience and expertise that will help you to crack any Python
Interview:
Python Interview Questions And Answers | Python Training |
Edureka
If you have other doubts regarding Python, feel free to post them in
our QnA Forum. Our expert team will get back to you at the
earliest.
LIST TUPLES
Lists are mutable i.e they Tuples are immutable (tuples are
can be edited. lists which can’t be edited).
Lists are slower than tuples. Tuples are faster than list.
LIST vs TUPLES
Q5.What is pep 8?
Ans: PEP stands for Python Enhancement Proposal. It is a set of
rules that specify how to format Python code for maximum
readability.
os
sys
math
random
data time
JSON
Local Variables:
Example:
Output: 5
When you try to access the local variable outside the function
add(), it will throw an error.
Example:
Output:
Example:
1 def Newfunc():
2 print("Hi, Welcome to Edureka")
3 Newfunc(); #calling the function
Q17.What is __init__?
Ans: __init__ is a method or constructor in Python. This method is
automatically called to allocate memory when a new object/
instance of a class is created. All classes have the __init__ method.
1 class Employee:
2 def __init__(self, name, age,salary):
3 self.name = name
4 self.age = age
5 self.salary = 20000
6 E1 = Employee("XYZ", 23, 20000)
7 # E1 is the instance of class Employee.
8 #__init__ allocates memory for E1.
9 print(E1.name)
10 print(E1.age)
11 print(E1.salary)
Output:
Powered by Edureka
BOOK A SLOT
XYZ
23
20000
Example:
Output: 11
The self variable in the init method refers to the newly created
object while in other methods, it refers to the object whose method
was called.
1 import random
2 random.random
Example:
Example:
1 stg='ABCD'
2 print(stg.lower())
Output: abcd
Example:
1 """
2 Using docstring as a comment.
3 This code divides 2 numbers
4 """
5 x=8
6 y=4
7 z=x/y
8 print(z)
Output: 2.0
is: returns true when 2 operands are true (Example: “a” is ‘a’)
Q35. Whenever Python exits, why isn’t all the memory de-
allocated?
Ans:
The following example contains some keys. Country, Capital & PM.
Their corresponding values are India, Delhi and Modi respectively.
1 dict={'Country':'India','Capital':'Delhi','PM':'
1 print dict[Country]
India
1 print dict[Capital]
Delhi
1 print dict[PM]
Modi
Example:
The expression gets evaluated like if x<y else y, in this case if x<y is
true then the value is returned as big=x and if it is incorrect then
big=y will be sent as a result.
Q38. What does this mean: *args, **kwargs? And why would we
use it?
Ans: We use *args when we aren’t sure how many arguments are
going to be passed to a function, or if we want to pass a stored list
or tuple of arguments to a function. **kwargs is used when we
don’t know how many keyword arguments will be passed to a
function, or it can be used to pass the values of a dictionary as
keyword arguments. The identi ers args and kwargs are a
convention, you could also use *bob and **billy but that would not
be wise.
Example:
1 stg='ABCD'
2 len(stg)
Q41. What are negative indexes and why are they used?
Ans: The sequences in Python are indexed and it consists of the
positive as well as negative numbers. The numbers that are
positive uses ‘0’ that is uses as rst index and ‘1’ as the second
index and the process goes on like that.
The index for the negative number starts from ‘-1’ that represents
the last index in the sequence and ‘-2’ as the penultimate index and
the sequence carries forward like the positive number.
The negative index is used to remove any new-line spaces from the
string and allow the string to except the last character that is given
as S[:-1]. The negative index is also used to show the index to
represent the string in correct order.
Example:
1 import os
2 os.remove("xyz.txt")
Integers
Floating-point
Complex numbers
Strings
Boolean
Built-in functions
Example:
Output:
Example:
Powered by Edureka
SCHEDULE NOW
Output:
4.6
3.1
Deep copy is used to store the values that are already copied. Deep
copy doesn’t copy the reference pointers to the objects. It makes
the reference to an object and the new object that is pointed by
some other object gets stored. The changes made in the original
copy won’t a ect any other copy that uses the object. Deep copy
makes execution of the program slower due to making certain
copies for each object that is been called.
Example:
1 a="edureka python"
2 print(a.split())
Example:
Example:
1 class Employee:
2 def __init__(self, name):
3 self.name = name
4 E1=Employee("abc")
5 print(E1.name)
Output: abc
1 # m.py
2 class MyClass:
3 def f(self):
4 print "f()"
1 import m
2 def monkey_f(self):
3 print "monkey_f()"
4
5 m.MyClass.f = monkey_f
6 obj = m.MyClass()
7 obj.f()
monkey_f()
1 class a:
2 pass
3 obj=a()
4 obj.name="xyz"
5 print("Name = ",obj.name)
Output:
Name = xyz
Output:
*
***
*****
*******
*********
***********
*************
***************
*****************
1 a=int(input("enter number"))
2 if a>1:
3 for x in range(2,a):
4 if(a%x)==0:
5 print("not prime")
6 break
7 else:
8 print("Prime")
9 else:
10 print("not prime")
Output:
enter number 3
Prime
1 a=input("enter sequence")
2 b=a[::-1]
3 if a==b:
4 print("palindrome")
5 else:
6 print("Not a Palindrome")
Output:
Q72. Looking at the below code, write down the nal values of
A0, A1, …An.
1 A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))
2 A1 = range(10)A2 = sorted([i for i in A1 if i in
3 A3 = sorted([A0[s] for s in A0])
4 A4 = [i for i in A1 if i in A3]
5 A5 = {i:i*i for i in A1}
6 A6 = [[i,i*i] for i in A1]
7 print(A0,A1,A2,A3,A4,A5,A6)