Thursday, August 8, 2019

Python Interview Questions

 


Q.1. What does the following code output?

  1. >>> def extendList(val, list=[]):

  2. list.append(val)

  3. return list

  4. >>> list1 = extendList(10)

  5. >>> list2 = extendList(123,[])

  6. >>> list3 = extendList('a')

  7. >>> list1,list2,list3

Ans. ([10, ‘a’], [123], [10, ‘a’])

You’d expect the output to be something like this:

([10],[123],[‘a’])

Well, this is because the list argument does not initialize to its default value ([]) every time we make a call to the function. Once we define the function, it creates a new list. Then, whenever we call it again without a list argument, it uses the same list. This is because it calculates the expressions in the default arguments when we define the function, not when we call it.

 

 


Q.2. What is a decorator?

Ans. A decorator is a function that adds functionality to another function without modifying it. It wraps another function to add functionality to it. Take an example.

  1. >>> def decor(func):

  2. def wrap():

  3. print("$$$$$$$$$$$$$$$$$")

  4. func()

  5. print("$$$$$$$$$$$$$$$$$")

return wrap

  1. >>> @decor

  2. def sayhi():

  3. print("Hi")

  1. >>> sayhi()

$$$$$$$$$$$$$$$$$

Hi

$$$$$$$$$$$$$$$$$

Decorators are an example of metaprogramming, where one part of the code tries to change another. For more on decorators, read Python

Decorators.

 

 

Q.3. Write a regular expression that will accept an email id. Use the re module.

Ans.

  1. >>> import re

  2. >>> e=re.search(r'[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$','ayushiwashere@gmail.com')

  3. >>> e.group()

‘ayushiwashere@gmail.com’

To brush up on regular expressions, check Regular Expressions in Python.

 

 

 

Q.4. How many arguments can the range() function take?

Ans. The range() function in Python can take up to 3 arguments. Let’s see this one by one.

a. One argument

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.

  1. >>> list(range(5))

[0, 1, 2, 3, 4]

  1. >>> list(range(-5))

[]

  1. >>> list(range(0))

[] b. Two arguments

When we pass two arguments, the first one is the start value, and the second is the stop value.

  1. >>> list(range(2,7))

[2, 3, 4, 5, 6]

  1. >>> list(range(7,2))

[]

  1. >>> list(range(-3,4))

[-3, -2, -1, 0, 1, 2, 3]

c. Three arguments

Here, the first argument is the start value, the second is the stop value, and the third is the step value.

  1. >>> list(range(2,9,2))

[2, 4, 6, 8]

  1. >>> list(range(9,2,-1))

[9, 8, 7, 6, 5, 4, 3]

 

 

 

Q.5. Does Python have a switch-case statement?

Ans. In languages like C++, we have something like this:

  1. switch(name)

  2. {

  3. case ‘Ayushi’:

  4. cout<<”Monday”;

  5. break;

  6. case ‘Megha’:

  7. cout<<”Tuesday”;

  8. break;

  9. default:

  10. cout<<”Hi, user”;

  11. }

But in Python, we do not have a switch-case statement. Here, you may write a switch function to use. Else, you may use a set of if-elif-else statements. To implement a function for this, we may use a dictionary.

  1. >>> def switch(choice):

  2. switcher={

  3. 'Ayushi':'Monday',

  4. 'Megha':'Tuesday',

  5. print(switcher.get(choice,'Hi, user'))

return

  1. >>> switch('Megha')

Tuesday

  1. >>> switch('Ayushi')

Monday

  1. >>> switch('Ruchi')


No comments:

Post a Comment

Which Python course is best for beginners?

Level Up Your Python Prowess: Newbie Ninjas: Don't fret, little grasshoppers! Courses like "Learn Python 3" on Codecade...