80 Python Interview Practice Questions
80 Python Interview Practice Questions
80 Python Interview Practice Questions
1. What is Python?
The benefits of pythons are that it is simple and easy, portable, extensible,
build-in data structure and it is open-source.
6. What is pep 8?
PEP stands for Python Enhancement Proposal. It is a set of rules that specify
how to format Python code for maximum readability.
Multi-line comments appear in more than one line. All the lines to be
commented are to be prefixed by a #. You can also a very good shortcut method
to comment on multiple lines. All you need to do is hold the ctrl key and left-click
in every place wherever you want to include a # character and type a # just once.
This will comment on all the lines where you introduced your cursor.
Docstrings are not actually comments, but, they are documentation strings.
These docstrings are within triple quotes. They are not assigned to any variable
and therefore, at times, serve the purpose of comments as well.
"""
This is Docstring example
It is useful for documentation
purposes """
All programming languages have some way of defining the scope and extent
of the block of codes; in Python, it is indentation. Indentation provides better
readability to the code, which is probably why Python has made it compulsory.
17. What are negative indexes and why are they used?
When we use the index to access elements from the end of a list, it’s called
reverse indexing. In reverse indexing, the indexing of elements starts from the last
element with the index number −1. The second last element has index ‘−2’, and
so on. These indexes used in reverse indexing are called negative indexes.
#Example usage of index
list_ex = [1, 2, 'Test']
print(list_ex[-1])
test=sample()
test.name="test1"
print(test.name)
38. Will the do-while loop work if you don’t end it with a semicolon?
This is a Trick question! Python does not support an intrinsic do-while loop.
Secondly, to terminate do-while loops is a necessity for languages like C++.
44. What is the difference if range() function takes one argument, two
arguments, and three arguments?
When we pass only one argument, it takes it as the stop value. Here, the
start value is 0, and the step value is +1. The iteration with a range would always
stop 1 value before the stop value.
for i in range(5):
print(i)
When we pass two arguments, the first one is the start value, and the second is
the stop value.
for i in range(1,5):
print(i)
Using three arguments, the first argument is the start value, the second is the
stop value, and the third is the step value.
for i in range(1,10,2):
print(i)
45. What is the best code you can write to swap two numbers?
You can perform the swap with a single line of code.
a = 1
b = 2
#Swap number
a, b = b, a
46. How can you declare multiple assignments in one line of code?
There are two ways to do this. First is by separately declare the variable in
the same line.
a, b, c = 1,2,3
Another way is by declaring the variable in the same line with only one value.
a=b=c=1
57. How do you get the current working directory using Python?
Working with Python, you may need to read and write files from various
directories. To find out which directory we’re presently working under, we can
use the getcwd() method from the os module.
import os
os.getcwd()
58. What do you see below? What would happen if we execute it?
a= '1'
b = '2'
c = '3'
s = a + ‘[‘ + b + ‘:’ + c + ‘]’ print(s)
This is string concatenation. If even one of the variables isn’t a string, this would
raise a TypeError. What would happen is that we get an output of the string
concatenation.
extend(another-list): Adds the elements of another list at the end of the list
66. What does this mean: *args, **kwargs? And why would we use it?
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 identifiers args and kwargs are optional, as you could
change it to another name such as *example **another but it is better to just use
the default name.
#Example of *args
def sample(*args):
print(args)
sample('time', 1, True)
#Example of **kwargs
def sample(**kwargs):
print(kwargs)
sample(a = 'time', b = 1)
Python doesn’t have variable declarations, so it has to figure out the scope
of variables itself. If there is an invitation to a variable inside a function, that
variable is considered local. The counter variable above is a global variable and
thus, the line of code above would raise an error.
76. What is the purpose of the single underscore (‘_’) variable in Python?
It is to hold the result of the last executed expression(/statement) in an
interactive interpreter session. This precedent was set by the standard CPython
interpreter, and other interpreters have followed this.