Python Practice on Exception Loop Object

In this class, we do some python practice on exception loop object.

For Complete YouTube Video: Click Here

Exception Example

Before going to practice examples, it would help if you had an idea of exceptions in python. Click here.

The example we discuss here are placement training purpose and improve your coding skills.

Our suggestion is to take the question and try to solve it on your own. Then check for a solution.

These examples provide you with a deeper understanding of the concepts of python.

This deeper understanding will help you a lot in writing the code on your own. We do it in our later classes.

The explanation given for the examples is based on the assumption. The reader knows the basics of python.

Q1) Check the code given below and identify the output displayed by the code.

python practice on exception loop object1
Program 1

Class overpricedexception(exception) in the program. This statement is creating a user-defined exception.

They are creating another user-defined exception named invalid no of bedrooms exception.

They did not mention the body of exception.

They defined a class home as having two instance variables bedrooms and rent.

If the value of the bedroom is less than 1. it is raising an exception.

If the number of bedrooms Multiplied by 3 is less than price. It is Raising an exception.

The basics of exception already discussed in our python course. We have to apply those concepts.

The new point to understand in this example.

They created two objects, and those two objects calling the method rent pricing. These calling statements are placed in the try block.

Inside the method rent pricing, There is a try-except block.

Suppose the called method raises an exception if the exception is not caught in the method.

It will come out and check for the except block written under the calling statement.

The point to understand, we have two levels of except blocks.

Name those levels as outer exception block and inner exception block for understanding purpose.

The except block present in the method is the inner block.

The except block present under the method calling statement is the outer block.

Suppose the exception is not caught in the inner block. It will check in the outer except block.

The point to understand is mentioned in bold.

The following three statements are the output displayed by the above program.

‘The bedroom value is incorrect.’

“The program executed successfully.”

“This finally block”

The below example will help you in a deeper understanding of the point mentioned in bold.

Q2) Check the below code. And identify the output displayed.

python practice on exception loop object2
Program 2

The program mentioned above is almost the same as Q1. A small code change.

The inner level exception block. i.e. in the method is catching only one exception.

The second exception invalid number of bedrooms exception is written in the outer level.

The following two statements are the output displayed by Q2.

“Exception caught in the outer level.”

“This finally block”

Let’s check one more example for a better understanding of the concept.

Q3) Check the below code and identify the output displayed by the code.

python practice on exception loop object3
Program 3

In Q3, we have written zero division error exception in the method. .ie inner level. This exception does not belong to any of our needed exceptions.

Now the first object calling the method and rising the overpriced exception. Because the value passed is 6500.

This exception is not caught in the inner level, i.e. in the method.

It caught in the outer level exception block.

The point to understand here. The second object is not calling the method because we are in except block of outer level. It continues executing the statements present after the exception block in the outer level.

The following statements are the output displayed by Q3.

“Expensive”

“This finally block”

“Continuation after exception.”

Object Example

Q4. Check the below code and identify How many reference variables refer to the object created at line1 at line2.

python practice on exception loop object4
Program 4

From the above program, They are given in comment line1 and line2.

They defined an object at line1.

Asking to identify how many number of reference variables refer to the object defined at line1 at line2.

The discussion about the object and how memory is assigned to the object is made in python.

pm1 is a reference to policemen(“Ramesh”) object.

pm2 is a reference to policemen(“Suresh”) object.

pm3 = pm2. pm3 is a reference to the Suresh object.

pm2=pm1. pm2 is a reference to Ramesh object.

pm3 = pm2. pm3 is a reference to Ramesh object.

All three variables reference Ramesh object.

Loop

Q4) identify the output displayed by the code given below.

python practice on exception loop object5
Program 5

They were given in the above program. list1 has one element.

Variable count1 taking value 1.

The for loop takes the values 4,5,6,7,8.

Each iteration of the loop checks if count1 less than the length of the list.

the value present in variable val is updated to the position count1 in list1

count1 incremented by 1.

else list1 is appended with the value present in variable val.

The output displayed by the program is [0,5,7,8]