# 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
Subscribe to:
Posts (Atom)