Interview Preparation Python-Part1

Abhishek Suryavanshi
4 min readJun 9, 2021

Usage of __name__ in python

# Suppose we have 2 python files in same directory namely first_module.py,second_module.py

#Inside first_module.py

#Inside second_module.py

The first line of output is the statement present in first_module.py,and the second line of O/P comes from second_module.py.In which the value of __name__ is main.
The first line of output is the statement present in first_module.py, and the second line of O/P comes from second_module.py, In which the value of __name__ is main.

Append, Insert, pop, sum for list

mylist=[1,53,12,13,90]

mylist.append(3)

mylist

>>>[1, 53, 12, 13, 90, 3]

mylist.insert(1,32)

mylist

>>>[1, 32, 53, 12, 13, 90, 3]

[4]+mylist

>>>[4, 21, 1, 32, 53, 12, 13, 90, 3]

mylist

>>>[21, 1, 32, 53, 12, 13, 90, 3]

sum(mylist)

>>>229

mylist.append([4,5])

mylist

>>>[4, 21, 1, 32, 53, 12, 13, 90, 3, [4, 5]]

mylist.pop()

>>>[4, 5]

mylist

>>>[4, 21, 1, 32, 53, 12, 13, 90, 3]

mylist.pop(0)

4

mylist

>>>[21, 1, 32, 53, 12, 13, 90, 3]

Zip

O/P

#%d for numbers

x=12

print(“%d is a number”%x)

>>> a=[12,32,31]

>>> a.extend(2)

>>>a

[12,32,31,2]

>>> a.extend([5,6])

>>> a

[12, 32, 31, 5, 6]

Copy and Remove

.remove() removes the first instance of a matching object.

.pop() removes an object by its index.

round1 = ['chuck norris', 'bruce lee', 'sonny chiba']round2 = round1.copy()round2.remove('sonny chiba')print(round1) #=> ['chuck norris', 'bruce lee', 'sonny chiba']print(round2) #=> ['chuck norris', 'bruce lee']

Sorting a list in descending order

li = [10,1,9,2,8,3,7,4,6,5]li.sort(reverse=True)

You cannot sort a list with None in it because comparison operators

sort() modifies the list in place. sorted() returns a new list in reverse order.

Reverse the order of a list using the slice syntax

li = [‘a’,’b’,3,4]
li[::-1]

About Python Interpreter

Python is interpreter language. Compiler takes high level code->low level

So within python, compilation happens, but it’s just not into a machine language. It is into byte code (.pyc or .pyo) and this byte code can’t be understood by the CPU. So we need an interpreter called the python virtual machine to execute the byte codes.

· If there is no error, i.e. if the python instruction or source code is well-formatted then the compiler translates it into its equivalent form in an intermediate language called “Byte code”.

· Byte code is then sent to the Python Virtual Machine(PVM) which is the python interpreter. PVM converts the python byte code into machine-executable code. If an error occurs during this interpretation then the conversion is halted with an error message.

Map, Filter, Reduce

nums=[1,2,3,4,5,6]

def isEven(n):

return n%2==0

k=list(filter(isEven,nums))

print(k)

>>>[2,4,6]

mulList=list(map(lambda a,b:a*b,[1,2,3,4,5],[6,7,8,9,10]))

print(mulList)

>>>[6,14,24,36,40]

k=list(filter(lambda x:x%2==0,nums))

print(k)

>>>[2,4,6]

a=[1,2,3,4,5]

c=list(map(lambda val:val*5,a))

print(c)

map()->applies function to all iterables

filter()->Applies function to only those iterables for which function is applied

reduce()->returns a single value

k=list(map(lambda x:x*x,filter(lambda x:x>=3,[1,2,3,4])))

print(k)

#If we want to print any \ inside string

print(“C:\\Hello”)->C:\Hello

def fun_name(arg1,**arg2):

#Dummy code

fun_name(32,age=21,city=”Mumbai”,mob=1231414)

#We can send as many args as possible with **arg2

Global Keyword

To modify global variables we need to use global keyword

Random 6 digit number

Self keyword

Self refers to the current instance of class

We can use any keyword instead of self, in this case I have used abc instead of self

Calling Parent class constructor from child class

PS:-These are the contents which I found on the internet quite useful, hence I’m combining them into a single doc for anyone who is preparing for Python interviews. There were many references to articles from which I found the info, but I couldn’t add/tag/quote them all.

--

--