Top 100 Python Interview Questions and Answers

 Top 100 Python interview questions and answers that you must know answers to these questions.

Take a Look: Complete Python Topics to be a professional Python Developer

1. What type of language is python? Interpreted or Compiled?

Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. This means that source files can be run directly without explicitly creating an executable which is then run.

2. What do you mean by python being an “interpreted language”?

An interpreted language is a programming language for which most of its implementations execute instructions directly, without previously compiling a program into machine­languageinstructions. In the context of Python, it means that the Python program runs directly from the source code.

3. What is python’s standard way of identifying a block of code?

Please provide an example implementation of a function called “my_func” that returns the square of a given variable “x”. (Continues from the previous question)

Indentation.

def my_func(x):

return x ** 2

4. Is python statically typed or dynamically typed?

Dynamic. In a statically typed language, the type of variables must be known (and usually declared) at the point at which it is used. Attempting to use it will be an error. In a dynamically typed language, objects still have a type, but it is determined at runtime. You are free to bind names (variables) to different objects with a different type. 

5. Is python strongly typed or weakly typed language?

Strong. In a weakly typed language a compiler/interpreter will sometimes change the type of a variable. For example, in some languages (like JavaScript) you can add strings to numbers ‘x’ + 3 becomes ‘x3′. This can be a problem because if you have made a mistake in your program, instead of raising an exception execution will continue but your variables now have wrong and unexpected values. In a strongly typed language (like Python) you can’t perform operations inappropriate to the type of the object ­ attempting to add numbers to strings will fail. Problems like these are easier to diagnose because the exception is raised at the point where the error occurs rather than at some other, potentially far removed, place.
Create a Unicode string in python with the string “This is a test string”? Ans.some_variable =u’This is a test string’ (or)
some_variable =u” This is a test string”

6. What is the python syntax for switch-case statements?

Python doesn’t support switch­case statements. You can use if­else statements for this purpose.

7. What is a lambda statement? Provide an example.


A lambda statement is used to create new function objects and then return them at runtime.

Example:

my_func =lambda x:x**2

Creates a function called my_func that returns the square of the argument passed.

8. What are the rules for local and global variables in Python?

If a variable is defined outside function then it is implicitly global. If the variable is assigned new value inside the function means it is local. If we want to make it global we need to explicitly define it as global. The variable referenced inside the function is implicitly global.

9. What is the output of the following program?

#!/usr/bin/python

deffun1(a):

print(‘a:’,)a

a=33;

print’ local a: ‘, a

=100 fun1(a)

Print(‘a outside fun1:’,a)

Output: 

a:100   local a:33

an outside fun1:100

10.What is the output of the following program?


deffoo(x,y):

global a=42 x,y =y,x b =33 b =17 c =100

print(a,b,x,y)

a,b,x,y =1,15,3,4

foo(17,4)

print(a,b,x,y)

Output:

42 17 4 17

42 15 3 4

11. What is the output of the following program?

#!/usr/bin/python

deffoo(x=[]):

x.append(1)

return x

foo()

foo()

Output:    [1]    [1,1]

12. What is the purpose of #!/usr/bin/python on the first line in the above code? Is there any advantage?

By specifying #!/usr/bin/python you specify exactly which interpreter will be used to run the script on a particular system. This is the hardcoded path to the python interpreter for that particular system. The advantage of this line is that you can use a specific python version to run your code.

13. What is the output of the following program?

list =[‘a’,’b’,'c’, ’d’,’e’]
printlist[10]

Output:

IndexError. Or Error.

14. What is the output of the following program?

list =[‘a’,’b’,’c’,’d’,’e’]
print(list[10:])

Output:

[]
The above code will output [], and will not result in an Index Error.

15. What does this list comprehension do:

[x**2 for x in range(10 ) if x%2==0]
Creates the following list:
[0,4,16,36,64]

16. Do sets, dictionaries and tuples also support comprehensions? Ans. Sets and dictionaries support it. However, tuples are immutable and have generators but not comprehensions.

Set Comprehension:

r={x for x inrange(2,101)
if not any(x %y ==0 for y in range(2,x))}

Dictionary Comprehension:

{i:j for i,j in{1:’a’,2:’b’}.items()}
since
{1:’a’,2:’b’}.items() returns a list of 2-Tuple.i is the first element of tuple j isthe second.

17. What are some mutable and immutable data­types / data­structures in python?  

 Mutable Types Immutable Types
  • Dictionary number
  • List boolean
  • string
  • tuple
18. What are the generators in python?

A generator is simply a function that returns an object on which you can call next, such that for every call it returns some value until it raises a Stop. Iteration exception, signaling that all values have been generated. In Python, however, there is an alternative, called yield. Using yield anywhere in a function makes it a generator.

19. What can you use Python generator functions for?

One of the reasons to use a generator is to make the solution clearer for some kind of solutions.
The other is to treat results one at a time, avoiding building huge lists of results that you would process separated anyway.

20. When is it not a good time to use python generators?
  • Use list instead of a generator when:
  • You need to access the data multiple times (i.e. cache the results instead of recomputing them)
  • You need random access (or any access other than forwarding sequential order):
  • You need to join strings (which requires two passes over the data)
  • You are using PyPy which sometimes can’t optimize generator code as much as it can with normal function calls and list manipulations


To know more interview questions and answers please refer to this link: Top 100 Interview Questions and Answers

Comments

Popular posts from this blog

Top 100 Data Science Interview Questions and Answers

Top 100 Machine Learning Interview Questions and Answers