# if value of x is given as zero the zero divison error generates and program terminates
x=int(raw_input('Enter the number of students failed:'))
y=100/x
print y
# If x is not defined itself the program not terminated instead it goes to except loop
try:
x=int(raw_input('Enter the number of students failed:'))
y=100/x
print y
except ZeroDivisionError:
x=1
y=100/x
print y
Solve Problems by Coding Solutions - A Complete solution for python programming
Exceptional handling for Name Error
# If x is not defined itself the program not terminated
try:
y=50*x
except NameError:
x=1
y=50*x
print y
try:
y=50*x
except NameError:
x=1
y=50*x
print y
Exceptional handling for Key Error
#Without Exceptional handling program terminates if key b not found
s={'d':2,'k':4}
if b in s.keys():
s['b'].append[9]
else:
s['b']= 8
#Exceptional handling for key error and program run without termination
s={'d':2,'k':4}
try:
s['b'].append[9]
except KeyError:
s['b']= 8
s={'d':2,'k':4}
if b in s.keys():
s['b'].append[9]
else:
s['b']= 8
#Exceptional handling for key error and program run without termination
s={'d':2,'k':4}
try:
s['b'].append[9]
except KeyError:
s['b']= 8
Reverse of a number using functions
def intreverse(Number):
Reverse = 0 # Initialize the reverse value to overcome garbage value storage
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number//10
return (Reverse)
Reverse = 0 # Initialize the reverse value to overcome garbage value storage
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number//10
return (Reverse)
What is the type of each of the following expressions (within the type function)?
print type(5) <type 'int'>
print type("abc") <type 'str'>
print type(True) <type 'bool'>
print type(5.5) <type 'float'>
print type(12/27) <type 'int'>
print type(2.0/1) <type 'float'>
print type(12 ** 3) <type 'int'>
print type(5 == "5") <type 'bool'>
a = str((-4 + abs(-5) / 2 ** 3) + 321 -((64 / 16) % 4) ** 2)
print a
Ans: 317
print type("abc") <type 'str'>
print type(True) <type 'bool'>
print type(5.5) <type 'float'>
print type(12/27) <type 'int'>
print type(2.0/1) <type 'float'>
print type(12 ** 3) <type 'int'>
print type(5 == "5") <type 'bool'>
a = str((-4 + abs(-5) / 2 ** 3) + 321 -((64 / 16) % 4) ** 2)
print a
Ans: 317
New Dictionary Creation and updation
Transpose of a matrix (nested list) in python
row1 = [13,25,73]
row2 = [54,95,36]
row3 = [27,98,19]
matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix], [row[2] for row in matrix]]
print'Transpose of a matrix',trmatrix
source: http://stackoverflow.com
row2 = [54,95,36]
row3 = [27,98,19]
matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix], [row[2] for row in matrix]]
print'Transpose of a matrix',trmatrix
source: http://stackoverflow.com
Manipulation the Dictionary
d={10: 'iprg' , 22: 'Nan', 33:'Kool',8: 'Jool','y': 89,'tt':'toy',7:90 }
for i in d:
print i,d[i] # printing all the values in Dictionary
print '\n'
print d[8] #printing values of the individual keys
print '\n'
print d['y'] #printing values of the individual keys
print '\n'
print d[7] #printing values of the individual keys
print '\n'
d[33]='hello world' #updating the key 33 with new value
d[22]='pythonforengineers' #updating the key 22 with new value
for i in d:
print i,d[i] # printing all the updated values in Dictionary
del d[10] # deleting key:value pair using key value -> 10
print'\n Dictionary after deleting a value:\n', d
d.clear() #clearing all values
print '\n Dictionary after clearing all values:', d
del d # Removing the Dictionary
try:
print d
except:
print 'Some error has occurred'
for i in d:
print i,d[i] # printing all the values in Dictionary
print '\n'
print d[8] #printing values of the individual keys
print '\n'
print d['y'] #printing values of the individual keys
print '\n'
print d[7] #printing values of the individual keys
print '\n'
d[33]='hello world' #updating the key 33 with new value
d[22]='pythonforengineers' #updating the key 22 with new value
for i in d:
print i,d[i] # printing all the updated values in Dictionary
del d[10] # deleting key:value pair using key value -> 10
print'\n Dictionary after deleting a value:\n', d
d.clear() #clearing all values
print '\n Dictionary after clearing all values:', d
del d # Removing the Dictionary
try:
print d
except:
print 'Some error has occurred'
Creating a dictionary and traversing
d={10: 'iprg' , 22: 'Nan', 33:'Kool',8: 'Jool' } # Creating a dictionary with key: value pair
for i in d: # Here key and the vaule can be any type.
print i,d[i] # Accessing elements in the dictionary
# Order in which they display is also different in output
print d.keys() # print all keys in a dictionary
print d.values() # print all values in a dictionary
for i in d: # Here key and the vaule can be any type.
print i,d[i] # Accessing elements in the dictionary
# Order in which they display is also different in output
print d.keys() # print all keys in a dictionary
print d.values() # print all values in a dictionary
Tuples Manipulation
try:
l=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
print 'List :',l
tup1 = tuple(l) #Converts a list into tuple
tup2=(1,2,3,4,5,6,7)
#Adding two Tuples
tup=tup1+tup2
print 'New added tuple :',tup
ntup=tup2*2
print 'New multiplyed tuple:',ntup
p=max(tup1)
print 'The tuple with max value:',p
p1=min(tup1)
print 'The tuple with min value:',p1
#Compares elements of both tuples and same return 0 otherwise -1
k=cmp(tup2,ntup)
print k
#Delete Tuple
del tup
print tup
except:
print 'Tuple does not exist'
l=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
print 'List :',l
tup1 = tuple(l) #Converts a list into tuple
tup2=(1,2,3,4,5,6,7)
#Adding two Tuples
tup=tup1+tup2
print 'New added tuple :',tup
ntup=tup2*2
print 'New multiplyed tuple:',ntup
p=max(tup1)
print 'The tuple with max value:',p
p1=min(tup1)
print 'The tuple with min value:',p1
#Compares elements of both tuples and same return 0 otherwise -1
k=cmp(tup2,ntup)
print k
#Delete Tuple
del tup
print tup
except:
print 'Tuple does not exist'
Subscribe to:
Posts (Atom)