Python Interview Questions
1 .
How to overload constructors or methods in Python?
Python's constructor : _init__ () is the first method of a class. Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an object. We can't overload constructors or methods in Python. It shows an error if we try to overload.
 
class student:  
    def __init__(self,name):  
        self.name = name  
    def __init__(self, name, email):  
        self.name = name  
        self.email = email  
       
# This line will generate an error  
#st = student("chanti")  
  
# This line will call the second constructor  
st = student("chanti", "chanti@gmail.com")  
print(st.name)  
 
Output :  
chanti
2 .
What is swapcase() function in the Python?
It is a string's function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.
 
string = "IT IS IN LOWERCASE."  
print(string.swapcase())  
  
string = "it is in uppercase."  
print(string.swapcase())  
 
it is in lowercase. 
IT IS IN UPPERCASE.
3 .
Why do we use join() function in Python?
The join() is defined as a string method which returns a string value. It is concatenated with the elements of an iterable. It provides a flexible way to concatenate the strings. See an example below.
 
str = "Chanti"  
str2 = "ab"  
# Calling function    
str2 = str.join(str2)    
# Displaying result    
print(str2)  
 
Output :
aChantib
4 .
How to create a Unicode string in Python?
In Python 3, the old Unicode type has replaced by "str" type, and the string is treated as Unicode by default. We can make a string in Unicode by using art.title.encode("utf-8") function.
5 .
What is the Python decorator?
Decorators are very powerful and a useful tool in Python that allows the programmers to modify the behaviour of any class or function. It allows us to wrap another function to extend the behaviour of the wrapped function, without permanently modifying it.
 
# Decorator example  
def decoratorfun():  
    return another_fun  
 
Functions vs. Decorators : A function is a block of code that performs a specific task whereas a decorator is a function that modifies other functions.
6 .
What is a dictionary in Python?
The Python dictionary is a built-in data type. It defines a one-to-one relationship between keys and values. Dictionaries contain a pair of keys and their corresponding values. It stores elements in key and value pairs. The keys are unique whereas values can be duplicate. The key accesses the dictionary elements.
 
Keys index dictionaries.
 
Example :
 
The following example contains some keys Country Hero & Cartoon. Their corresponding values are India, Sivaji, and Chanti respectively.
 
>>> dict = {'Country': 'India', 'Hero': 'Sivaji', 'Cartoon': 'Chanti'}  
>>>print dict[Country]  
India  
>>>print dict[Hero]  
Sivaji 
>>>print dict[Cartoon]  
Chanti
7 .
What are the differences between Python 2.x and Python 3.x?
Python 2.x is an older version of Python. Python 3.x is newer and latest version. Python 2.x is legacy now. Python 3.x is the present and future of this language.
 
The most visible difference between Python2 and Python3 is in print statement (function). In Python 2, it looks like print "Hello", and in Python 3, it is print ("Hello").
 
String in Python2 is ASCII implicitly, and in Python3 it is Unicode.
 
The xrange() method has removed from Python 3 version. A new keyword as is introduced in Error handling.
8 .
What is the usage of enumerate () function in Python?
The enumerate() function is used to iterate through the sequence and retrieve the index position and its corresponding value at the same time.
 
For i,v in enumerate(['Python','Java','C++']):  
print(i,v)  
0 Python  
1 Java  
2 C++  
# enumerate using an index sequence  
for count, item in enumerate(['Python','Java','C++'], 10):  
9 .
Explain the ternary operator in Python.
Unlike C++, we don’t have ?: in Python, but we have this :
 
[on true] if [expression] else [on false]
 
If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.
 
Below is how you would use it :
 
>>> a,b=2,3
>>> min=a if a<b else b
>>> min
Output : 2
 
>>> print("Hi") if a<b else print("Bye")
Output : Hi
10 .
Is Python case-sensitive?
A language is case-sensitive if it distinguishes between identifiers like myname and Myname. In other words, it cares about case- lowercase or uppercase. Let’s try this with Python.
 
>>> myname='Ramana'
>>> Myname
Traceback (most recent call last) :
 
File “<pyshell#3>”, line 1, in <module>
 
Myname
 
NameError : name ‘Myname’ is not defined
 
As you can see, this raised a NameError. This means that Python is indeed case-sensitive.