4/9/19

Python Interview Questions and Answers Part-1




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 = A
In 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 = 20
Swap 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 b
What is the output?
Output is 10.

6) How do you find the type and identification number of an object in Python?
type() gives the type of the object that variable is pointing to, and id() give the unique identification number of the object that variable is pointing to. Ex:

print(type(b)) #
print(id(b)) #1452987584

7)  a = 0101
    b = 2
    c = a+b

What is the Value of c?


In Python2, any number with leading 0 is interpreted as an octal number. So, variable a points to 65(Equalent in Decimal) then the variable c will be pointing to the value 67 i.e 65+2.In Python3, a=0101  (Doesn’t support syntax)

8) What are the Arithmetic Operators that Python supports?




10)  What are the basic Data Types Supported by Python?
Numeric Data types: int, long, float, NoneType
String: str
Boolean: (True, False)
NoneType: None

11) 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
print(a is c) #Returns true if the two variables are pointing to the same object
print(id(a)) #64450416
print(id(c)) #64450416

12) 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 = "Hello world"
c = a
print(a is c) #Returns true if the two variables are pointing to the same object
print(id(a)) #64450416
print(id(c)) #64450416

The 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 sysprint(sys.version)

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. 

import sys
x = "JohnShekar"
y = xprint(sys.getrefcount(x))

Here, the object ‘JohnShekar’ is referred by x, y 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.

import sys
x = "JohnShekar"
y = xprint(sys.getrefcount(x))
del xprint(sys.getrefcount(x)) #NameError: name 'x' is not defined

16) What is the difference between range() and xrange() functions in Python?
range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python.
In Python 3, there is no xrange , but the range function behaves like xrange in Python 2.
If you want to write code that will run on both Python 2 and Python 3, you should use range().

Example:
# initializing a with range()
a = range(1, 10000)

# initializing a with xrange()
x = xrange(1, 10000)

print("The return type of range() is : ")
print(type(a))

# testing the type of x
print("The return type of xrange() is : ")
print(type(x))

17) What are the ideal naming conventions in Python?
All variables and functions follow lowercase and underscore naming convention.

Examples: is_prime(), test_var = 10 etc

Constants are all either uppercase or camel case.

Example: MAX_VAL = 50, PI = 3.14

None, True, False are predefined constants follow camel case, etc.

Class names are also treated as constants and follow camel case.

Example:    UserNames

18) What happens in the background when you run a Python file?
When we run a .py file, it undergoes two phases. In the first phase it checks the syntax and in the second phase it compiles to bytecode (.pyc file is generated) using Python virtual machine, loads the bytecode into memory and runs.

19) What is a module in Python?
A module is a .py file in Python in which variables, functions, and classes can be defined. It can also have a runnable code.

20) How do you include a module in your Python file?
The keyword “import” is used to import a module into the current file.

Example: import sys  #here sys is a predefined Python module.

21) How do you reload a Python module?
There is a function called reload() in Python, which takes module name as an argument and reloads the module.

22) What is List in Python?
The List is one of the built-in data structures in Python. Lists are used to store an ordered collection of items, which can be of different type.

Elements in a list are separated by a comma and enclosed in square brackets.

Examples of List are:

    A = [1,2,3,4]
    B = [‘a’,’b’,’c’]
    C = [1,’a’,’2’,’b’]

List in Python is sequence type as it stores ordered collection of objects/items. In Python String and tuple are also sequence types.

23)  When do you choose a list over a tuple?
When there is an immutable ordered list of elements, we choose tuple. Because we cannot add/remove an element from the tuple. On the other hand, we can add elements to a list using append () or extend() or insert(), etc., and delete elements from a list using remove() or pop().

Simple tuples are immutable, and lists are not. Based on these properties one can decide what to choose in their programming context.

24) How do you get the last value in a list or a tuple?
When we pass -1 to the index operator of the list or tuple, it returns the last value. If -2 is passed, it returns the last but one value.

Example:

a = [1,2,3,4] #List
print(a[-1])#4
print(a[-2])#3
b = (1,2,3,4) #Tuple

print(b[-1])#4print(b[-2])#3

25) What is Index Out Of Range Error?
When the value passed to the index operator is greater than the actual size of the tuple or list, Index Out of Range error is thrown by Python.

a = [1,2,3,4] #Listprint(a[5])#IndexError: list index out of range 

26) What is slice notation in Python to access elements in an iterator?
In Python, to access more than one element from a list or a tuple we can use ‘:’ operator. Here is the syntax. Say ‘a’ is list

    a[startindex:EndIndex:Step]

Example:

a = [100,200,300,400,500,600,700,800]

print(a[3:]) # Prints the values from index 3 till the end [400, 500, 600, 700, 800]
print(a[3:6])#Prints the values from index 3 to index 6. [400, 500, 600]
print(a[2::2])#Prints the values from index 2 till the end of the list with step count 2. [300, 500, 700]

The above operations are valid for a tuple too.

27) How do you convert a list of integers to a comma separated string?
List elements can be turned into a string using join function.

a = [1,2,3,4,5,6,7,8]
print(a)

numbers = ','.join(str(i) for i in a)
print(numbers)

28) What is the difference between Python append () and extend () functions?
The extend() function takes an iterable (list or tuple or set) and adds each element of the iterable to the list. Whereas append takes a value and adds to the list as a single object.

Example:

a = [1,2,3,4,5]
b = [6,7,8]
a.extend(b)
print(a)#[1, 2, 3, 4, 5, 6, 7, 8]
c = ['a','b']

a.append(c)
print(a) #[1, 2, 3, 4, 5, 6, 7, 8, ['a', 'b']]






1 comment: