Python Scripting - Week 9 Flashcards

1
Q

Kinds of Exceptions in Python ?

A

TypeError “a”+2
ZeroDivisionError 5/0
ImportError from numpy import tree
ModuleNotFoundError import asd
KeyError a = dict{b=10,c=20}
print(a[25])
IndexError ‘hello’[10]
NameError print(basic)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to catch all exceptions ?

A

def square():
while True:
value=input(“Enter a Value : Must be int - “)
try:
num=int(value)
return f”Square of ‘{num}’ is ‘{num *num}’”
except:
print(“Not an integer type , Please ENTER Again”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to print an error message on certain condition ?

A

x = 10
if x > 5:
raise Exception(‘x should not exceed 5. The value of x was: {}’.format(x))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many types of errors are in python ?

A

In python we have two types of errors,
syntax error or an exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a code to handle multiple exceptions ?

A

try:
“hello”[20]
except ZeroDivisionError:
print(“This is an zero division error”)
except IndexError:
print(“Index Error”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is e in this statement ,
except IndexError as e

A

The “as e” part of the statement is used to assign the error message to a variable (in this case, “e”) so that it can be accessed and used later in the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to catch any type of exception error in variable

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly