Categories
Top 50 Python Interview Questions and Answers
Python is one of the most popular languages for beginners and professionals a like — it is easy to pick up, and there are a lot of applications in a variety of fields. You’ll often see it in the best programming languages to learn in 2023, and that doesn’t look like it’s going to change anytime soon.
1. What are some distinct features of Python?
a) Structured and functional programming is supported.
b) It can be compiled to byte code to create larger applications.
c) Supports high-level dynamic data types.
d) Supports checking of dynamic data types.
e) It could be used effectively along with Java, COBRA, C, C++, ActiveX, and COM.
2. What is the Pythoncaseok environment variable?
3. Briefly explain some characteristics of Python.
4. What is Pythonpath?
5. Why do we use the Pythonstartup environment variable?
6. What is the major difference between tuples and lists in Python?
Tuples | List |
Tuples are similar to a list, but they are enclosed within parentheses, unlike the list | The list is used to create a sequence |
The element and size can be changed | The element and size cannot be changed |
They cannot be updated | They can be updated |
They act as read-only lists | They act as a changeable list |
Tuples use parentheses | Lists use square brackets |
Example: tup = (1, "a", "string", 1+2) | Example: L = [1, "a" , "string" , 1+2] |
7. What does the method object() do?
8. What are positive and negative indices?
9. What is the permitted length of the identifier?
10. What is namespace in Python?
11. What is pep 8?
12. Define self in Python.
13. Is indentation necessary in Python?
14. Define a function in Python.
15. Do runtime errors exist in Python? Give an example.
16. What are the limitations of Python?
a) It has design restrictions.
b) It is slower when compared with C and C++ or Java.
c) It is inefficient for mobile computing.
d) It consists of an underdeveloped database access layer.
17. What is the Pass statement?
18. Can we use a break and continue together in Python? How?
19. Why do we need a continue?
20. Why do we need a break?
21. In how many ways can we apply reverse strings?
a) Loops
b) Recursions
c) Stacks
d) Extended slice syntax
e) Reversed function
22. Does Python support an intrinsic do-while loop?
23. Define slicing in Python.
24. What are the different stages of the life cycle of a thread?
Stage 1:
Creating a class where we can override the run method of the Thread class.Stage 2:
We make a call to start() on the new thread. The thread is taken forward for scheduling purposes.Stage 3:
Execution takes place wherein the thread starts execution, and it reaches the running state.Stage 4:
Thread waits until the calls to methods including join() and sleep() take place.Stage 5:
After the waiting or execution of the thread, the waiting thread is sent for scheduling.Stage 6:
Running thread is done by executing the terminates and reaches the dead state.25. What is docstring?
26. What are relational operators, assignment operators, and membership operators?
b) The assignment operators in Python can help in combining all the arithmetic operators with the assignment symbol.
c) Membership operators in Python with the purpose to validate the membership of a value in a sequence.
27. Differentiate between list and tuple.
28. What are Python decorators?
29. How are identity operators different from membership operators?
30. Draw a comparison between the range and xrange in Python.
Xrange is not able to generate a static list at runtime the way range does. On the contrary, it creates values along with the requirements via a special technique called yielding. It is used with a type of object known as generators.
If you have an enormous range for which you need to generate a list, then xrange is the function to opt for. This is especially relevant for scenarios dealing with a memory-sensitive system, such as a smartphone.
The range is a memory hog. Using it requires much more memory, especially if the requirement is gigantic. Hence, in creating an array of integers to suit the needs, it can result in a memory error and ultimately lead to a crash.
31. Describe multithreading in Python.
The package has the GIL or Global Interpreter Lock, which is a construct. It ensures that only one of the threads executes at any given time. A thread acquires the GIL and then performs work before passing it to the next thread.
This happens so fast that to a user it seems that threads are executing in parallel. Obviously, this is not the case as they are just taking turns while using the same CPU core. GIL passing adds to the overall overhead to the execution.
As such, if you intend to use the threading package for speeding up the execution, using the package is not recommended.
32. How do you capitalize the first letter of string?
33. Explain how to acquire the Google cache age of any URL or webpage using Python.
http://webcache.googleusercontent.com/search?q=cache:URLGOESHERE
Simply replace URLGOESHERE with the web address of the website or webpage whose cache you need to retrieve and see in Python.
34. Can we reverse a list in Python?
def reverse(s):
str = ""
for i in s:
str = i + str
return str
35. How is a file deleted in Python?
os.remove(filename)
os.unlink(filename)
36. Explain how to acquire the Google cache age of any URL or webpage using Python.
Simply replace URLGOESHERE with the web address of the website or webpage whose cache you need to retrieve and see in Python.
Python Advanced Interview Questions
37.What is the output of the following code?
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = sorted([i for i in A1 if i in A0])
A3 = [i for i in A1 if i in A3]
A4 = [i for i in A1 if i in A3]
A5 =
A6 = [[i, i*i] for i in A1]
print(A0,A1,A2,A3,A4,A5,A6)
ans
A1 = range(0, 10)
A2 = []
A3 = [1, 2, 3, 4, 5]
A4 = [1, 2, 3, 4, 5]
A5 =
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
38. Suppose you need to collect and print data from IMDb top 250 Movies page. Write a program in Python for doing so. (NOTE: You can limit the displayed information for 3 fields; namely movie name, release year, and rating.)
from bs4 import Beautifulsoup
import requests
import sys
url = 'http://www.imdp.com/chart/top'
response = requests.get(url)
soup = Beautifulsoup(response.text)
tr = soup.findchildren("tr")
tr = iter(tr)
for movie in tr:
title = movie.find('td' , {'class': 'titlecolum'}.find('a').contents[0]
year = movie.find('td', {'class: 'titlecolumn'}).find('span',{'class':'secondaryinfo'}).contents[0]
rating = movie.find('td',{'class': 'ratingcolumn imdbrating'} ).find('strong').contents[0]
row = title + ' - ' + ' ' + rating
print(row)
39. Python supports negative indexes. What are they and why are they used?
Unlike positive numbers, index numbering for the negative numbers starts from -1 and it represents the last index in the sequence. Likewise, -2 represents the penultimate index. These are known as negative indexes. Negative indexes are used for:
a.) Removing any new-line spaces from the string, thus allowing the string to except the last character, represented as S[:-1]
b.) Showing the index to representing the string in the correct order
40. Explain dictionaries with an example.
Let us take an example with three keys, namely website, language, and offering. Their corresponding values are hackr.io, Python, and Tutorials. The code for would be
41. What is the output of the following code?
try: if '1' !=1;
raise "someerror"
else: print("someError has not occured")
expect "someError":pr
int ("someError has occured")
42. Explain the process of compilation and linking.
In the case of dynamic loading, the process of compilation and linking depends on the style that is provided with the concerned system. In order to provide dynamic loading of the configuration setup files and rebuilding the interpreter, the Python interpreter is used.
43. What is Flask and what are the benefits of using it?
a) Flask has little to no dependencies on external libraries.
b) Because there is a little external dependency to update and fewer security bugs, the web microframework is lightweight.
c) It has an inbuilt development server and a fast debugger.
44. What is monkey patching in Python?
class myclass:
def f(self):
print "f()"
we can monkey-patch the program something like this
import m
def monkey_f(self):
print "monkey_f()"
m.Myclass.f = monkey_f
obj = m.Myclass()
obj.f()
45. What is the map() function used for?
Typically, the given function is the first argument and the iterable is available as the second argument to a map() function. Several tables are given if the function takes in more than one argument.
46. Whenever Python exits, not all the memory is deallocated. Why is it so?
This is because it is not possible to deallocate those portions of the memory that are reserved by the C library.