Python Basic concepts using code (Interview questions)


#Creating a class in Python

class test:
def __init__(self,kind=1):#Constructor for the Class
self.kind = kind

def whatKind(self):
return self.kind

def main():
#Creating an Object based on the Class Test
t1 = test()
t2 = test(2)
#Instances that uses the methods inside the class
#Check which class is the instance from
a = 1
print(“main function”)

#Assigning values in Main
#Using conditionals in Python

def condi():
a,b = 1,1
if a < b:
print(“A isn less than b”)
elif a == b:
print(“A is equal to b”)
print(“A is not less than b”)

#Conditional Expressions in python
def conditionalExpressions():
a,b = 1,0
s = “Less than” if a < b else “not less than”

#For loop in Python and the Range Funtion

def forloop(a=7): #Default argument if ntn passed will use the default function in Python
for i in range(a,10): #Range is an inbuild function in Python and can an single argument and the Range will be from 0 – the Argument
print(i, end=” “) #or you can give a starting and an ending range #End is used here to tell what to do after you print “i”. Default will be a new line

def stringsExamples():
a = “ishaan”
i = r”\nIshaan”#Raw string neglect the \n i.e backslash
s = “This is a {} string”.format(a)#Format is used to replace a {} with a variable
# ”’ or “”” allows you to print a multiline output
#the “\” escapes the new line
j = ”’\
This is a stin
sorry string

def tuplesList():
#The round brackets “()” makes a tuple which is immutable
x = (1,2,3)
#The square brackets “[]” makes it a list which is immutable
y = [1,2,3]
#Playiny with strings. String is an immutable sequence
x = “string”
#All strings are interable
for i in x:

def dictionary(): #Similar to a hash
#When using a {} it is a dictionary. Key value pair
d = {‘one’:1,’two’:2}
#Itereating through the dictionary. Does not keep the order
for k in d:

#To print in the sorted form
for m in sorted(d):
#another way of defining a dictioanry
d = dict(one = 1,two = 2,five = 5)
#Adding a particular element to the Dictionary
d[‘seven’] = 7
for m in sorted(d):

#Finding an identity of a variable in Python

def findIndentity():
i = “isd”
#id gives you an immutable id of the Object
#the “is” operator compares the id rather than the value
#immutable objects will always have the same id
#Mutable objects will have different id even if they have same values
h = “isd”
i = h
print(i is h)

def whileloop():
#Printing the Fibonacci series using the while loop
a,b = 0,1
while b < 150:
print(b,end=” “)
a,b = b,a+b

#Using the enumaration function in for loops for using the index which is not available in Python
#break when used in a loop exits out of the loop
#continue restarts the loop
def forloopEnumarate():
s = ‘ishaan’
for i,c in enumerate(s):
print(“After everything gets printed and for loop has nothing to print this gets executed”)

#Working with the Splice operator
def spliceOperatot():
list1 = [1,3,4,3,5,6,7,8,9]
#Prints from the range 0 to 5
#The range function in Python would print between the range you have given and not the exact number.

#list4[:] = range(100)

if __name__ == “__main__”: main()


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s