Python Basic concepts using code (Interview questions)

#!/usr/bin/python3.3

#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
print(t1.whatKind())
print(t2.whatKind())
#Check which class is the instance from
print(type(t1))
a = 1
print(“main function”)
print(a)
#condi()
#conditionalExpressions()
#forloop()
#stringsExamples()
#tuplesList()
#dictionary()
#findIndentity()
#whileloop()
#forloopEnumarate()
spliceOperatot()

#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”)
else:
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”
print(s)

#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”
print(a)
i = r”\nIshaan”#Raw string neglect the \n i.e backslash
print(i)
s = “This is a {} string”.format(a)#Format is used to replace a {} with a variable
print(s)
# ”’ or “”” allows you to print a multiline output
#the “\” escapes the new line
j = ”’\
This is a stin
sorry string
”’
print(j)

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

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

#To print in the sorted form
for m in sorted(d):
print(m,d[m])
#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):
print(m,d[m])

#Finding an identity of a variable in Python

def findIndentity():
i = “isd”
#id gives you an immutable id of the Object
print(id(i))
print(id(“isd”))
#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(i,c)
else:
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
print(list1[0:5])
#The range function in Python would print between the range you have given and not the exact number.

#list4[:] = range(100)
#print(list4)

if __name__ == “__main__”: main()

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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