Python Interview Questions with Answers for Freshers

1)  A = 10, 20, 30 

In the above assignment operation, what is the data type of ‘A’ that Python appreciates as?Unlike other languages, Python appreciates ‘A’ as a tuple. When you print ‘A’, the output is (10,20,30). This type of assignment is called “Tuple Packing”.2)    >>> A = 1,2,3,4>>> a,b,c,d = AIn the above assignment operations, what is the value assigned to the variable ‘d’?4 is the value assigned to d.  This type of assignment is called ‘Tuple Unpacking’.3) >>> a = 10>>> b = 20Swap these two Variables without using the third temporary variable?a, b = b, a   This kind of assignment is called a parallel assignment.4)  What is a Variable in Python?When we say Name = ‘john’ in Python, the name is not storing the value ‘john’. But, ‘Name’ acts like a tag to refer to the object ‘john’. The object has types in Python but variables do not, all variables are just tags. All identifiers are variables in Python. Variables never store any data in Python.5) >>> a = 10>>> b = a>>> a = 20 >>> print bWhat is the output?In Python, a variable always points to an object and not the variable. So here ‘b’ points to the object 10, so the output is 10.6) How do you find the type and identification number of an object in Python?type(<variableName>) gives the type of the object that variable is pointing to, and id(<variablename>) give the unique identification number of the object that variable is pointing to.7) >>> a = 0101>>> b = 2 >>> c = a+bWhat is the Value of c?In Python, any number with leading 0 is interpreted as an octal number. So, the variable c will be pointing to the value 67.8) How do you represent binary and hexadecimal numbers?If  ‘0b’ is leading then the Python interprets it as a binary number.‘0x’ as hexadecimal.9) What are the Arithmetic Operators that Python supports? ‘+’     : Addition  ‘-’     : Subtraction ‘*’    : Multiplication‘/’     : Division ‘%’    : Modulo division‘**’   : Power Of‘//’   :  floor divPython does not support unary operators like ++ or – operators. On the other hand Python supports “Augmented Assignment Operators”; i.e.,A += 10   Means A = A+10B -= 10     Means B = B – 10Other Operators supported by Python are & , |, ~ , ^ which are and, or, bitwise compliment and bitwise xor operations respectively. 10)  What are the basic Data Types Supported by Python?Numeric Data types: int, long, float, NoneTypeString: strBoolean: (True, False)NoneType: None11) How do you check whether the two variables are pointing to the same object in Python?In Python, we have an operation called ‘is’ operator, which returns true if the two variables are pointing to the same object.Example:>>> a = “Hello world”>>> c = a>>> a is cTrue>>> id(a)46117472>>> id(c)4611747212) What is for-else and while-else in Python?Python provides an interesting way of handling loops by providing a function to write else block in case the loop is not satisfying the condition.Example :a = []for i in a: print “in for loop”else: print “in else block”output: in else blockThe same is true with while-else too.13) How do you programmatically know the version of Python you are using?The version property under sys module will give the version of Python that we are using.>>> import sys>>> sys.version’2.7.12 (v2.7.12:d33e0cf91556, June 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]’>>>14) How do you find the number of references pointing to a particular object?The getrefcount() function in the sys module gives the number of references pointing to a particular object including its own reference.>>> a = “JohnShekar”>>> b = a>>> sys.getrefcount(a)3Here, the object ‘JohnShekar’ is referred by a, b and getrefcount() function itself. So the output is 3.15) How do you dispose a variable in Python?‘del’ is the keyword statement used in Python to delete a reference variable to an object.>>> a = ‘Hello world!!’>>> b = a>>> sys.getrefcount(a)3>>> del a>>> sys.getrefcount(a)Traceback (most recent call last): File “<pyshell#23>”, line 1, in <module>sys.getrefcount(a)NameError: name ‘a’ is not defined>>> sys.getref


Leave a Reply