Python Interview Questions Beginner

1. Know how to write code on a whiteboard or paper

2. Know basic python control flow

  • Know how to use if/else statements

  • Know how to use for loops

  • Know how to use while loops

for loops:

for i in range(1,11):
	print i

while loops:

i = 1
while i <= 10:
	print i
	i += 1

if/else statements:

a = 10
b = 20
if a < b:
	print "{} is less then {}".format(a, b)
elif a == 20:
	print "{} is equal to {}".format(a, b)
else:
	print "{} is grather then {}".format(a, b)

3. Be able to discuss how you've used python in the past

  • Spamcannon

  • Webscraper BVB

  • Template Generator

  • To do webapp using flask

4. Know how to solve common interview problems

Fizz Buzz - i.e Bolt

for num in xrange(1, 101):
	if num % 5 == 0 and num % 3 == 0:
		print "FizzBuzz"
	elif num % 3 == 0:
		print "Fizz"
	elif num % 5 == 0:
		print "Buzz"
	else:
		print num

Fibonacci Sequence

You print out a number and each next number is the previous 2 numbers added

a, b = 0, 1
for i in xrange(0, 10):
	print a
	a, b = b, a+b

5. Know basic python data types and when to use them

  • Strings

  • Lists

  • Tuples

  • Dicts

  • Sets - A List with no repeated values

NOTE: AND HOW TO ITTERATE THROUGH THEM!

List vs tuple, when to use each?

Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

Using this distinction makes code more explicit and understandable.

One example would be pairs of page and line number to reference locations in a book, e.g.:

my_location = (42, 11)  # page number, line number

6. Know how to use list comprehansion

my_list = [1,2,3,4,5,6,7,8,9,10]

# Give me each number in a list squared
squares = [num*num for num in my_list]
print squares

7. Know how to use Generators

Fibonacci Generator:

def fib(num):
	a,b = 0,1
	for i in xrange(0, num):
		yield "{}: {}".format(i_1, a)
		a, b = b, a + b

for item in fib(10):
	print item

range vs xrange

  • xrange gives 1 result at a time

  • range gives the entire range of numbers in memory at once

8. Know the basics of OOP

# base class
class Person(object):
	def __init__(self, name):
		self.name = name
	
	def reveal_identity(self):
		print "My name is {}".format(self.name)
		

class SuperHero(Person): # SuperHero Class that inheritace from the Person Class
	def __init__(self, name, hero_name):
		super(SuperHero, self).__init__(name)
		self.hero_name = hero_name
	
	def reveal_identity(self):
		super(SuperHero, self).reveal_identity()
		print "...And I am {}".format(self.hero_name)	
		
## Class instance
corey = Person('Corey')
corey.reveal_identity()

What is self? - The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class. It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class

  • Do you guys use python2 or python3?

  • What modules are you using to interact with databases?

10. Know the basics of other Technologies

  • Version control like GIT

  • Linux CLI

  • Databases and SQL

Last updated