Code

Python interview questions with answers part 2

This is a growing list hence, the article has been broken down into multiple parts

  • [1. Python Interview questions part 1](../python-interview-questions-with-answers/)
  • [2. Python Interview questions part 2](../python-interview-questions-with-answers-part2/)

Fibonacci series in python?

Generate a Fibonacci series for a given number

100a,b,n = 0,1,10
101for i in range(0, n):
102    print(a, end=', ')
103    a, b = b, a+b

Check if a string is a palindrome or not

There are many ways of implementing this.

String Reverse

100mystring = 'malayalam'
101reverse_mystring = mystring[::-1]
102reverse_mystring = ''.join(reversed(mystring))
103
104if  mystring == reverse_mystring:
105    print("String is a palindrome")
106else:
107    print("String is NOT a palindrome")

String Reverse

100def isPalindrome(str):
101    for i in range(0, len(str)//2):
102        if str[i] != str[len(str)-i-1]:
103            return False
104    return True

If you have multiple decorators for a function, which would be called first

100def a(f):
101    def wrapperxx(*args, **kwargs):
102        print("a")
103        f(*args, **kwargs)
104    return wrapperxx
105
106def b(f):
107    def wrapperxx(*args, **kwargs):
108        print("b")
109        f(*args, **kwargs)
110    return wrapperxx
111
112def c(f):
113    def wrapperxx(*args, **kwargs):
114        print("c")
115        f(*args, **kwargs)
116    return wrapperxx
117
118@a
119@b
120@c
121def myfunction(a):
122    print(a)
123myfunction("Prabhakar")

The result would be

100a
101b
102c
103Prabhakar

This is a growing list hence, the article has been broken down into multiple parts

  • [1. Python Interview questions part 1](../python-interview-questions-with-answers/)
  • [2. Python Interview questions part 2](../python-interview-questions-with-answers-part2/)

comments powered by Disqus

Related Posts

Javascript interview questions with answers

What are the advantages of using JSON rather than using xml JSON is a lightweight format for storing and transferring data.

Read more

How to maintain multiple versions of django without using using virtual envs.

Most linux come installed with python 2.7. Some even come installed with both 2.7 and 3 as well. django upto version 1.

Read more

Regex Cheatsheet

We have compiled a list of regular expression cheatbook to help in learning regular expressions. This is categorized into multiple sections for easy understanding.

Read more