exception handling in python

Exception Handling in Python |Episode #15|

Hello and Welcome to Python programming for beginners, where we teach you python from scratch. Till now, we have seen, many topics in Python, our last topic was Inheritance in Python. If you haven’t checked that out, CLICK HERE.

Now, we are heading towards a somewhat advanced python or intermediate one. Personally, I don’t think, there is something like advance and all but still, many people consider it advance so be it. So, today’s topic is Exception Handling in python and many more stuff. So. Let’s get started.

Exception Handling in Python

exception handling

There are many built-in exceptions that are raised in python when something goes wrong.

Exceptions or Exception Handling in python can be handled using a try statement. The code that handles the exception is written in the except clause.

exception handling

exception handling answer

Here we see a while loop which will run forever until we hit “q”. then we use the try to execute some code to enter a number and check if it is greater than 6 or not. Then we use the except clause as e to print the error. Suppose, you have a GUI app and the user does something stupid and it crashes. With lots of error on screen, you don’t want that. But if you use except then it will throw the error or whatever statement we like but the program ends smoothly. The except won’t execute if the try executes first.

Raising Exceptions – We can raise our custom exceptions using the raise keyword in python.

raise exceptions

raise exception ans

Try with else clause – Sometimes we have to run a piece of code when the try is successful. It could be to check if the code inside try is working properly or not.

try with else

try with else answer

Try with finally – Python offers a finally clause that ensures execution of a piece of code irrespective of the exception.

finally clause

finally clause answer

Now on that, you can say, we can also use a print statement after the exception clause, what’s new in that? Well, there is. Suppose, you type the statement exit() in exception which exits out of the program. Then what, your print statement will never get executed. But this is not the case in finally.

Here we input a number and divide 1 by that number. If successful then okay but if not, it will throw the error and exit. When we try 0 here, it gives a Zero division error but irrespective of quitting the program, it prints the finally statement and then exit but it didn’t print the last print statement.

If__name__ == ‘main’ in python

name evaluates to the name of the module in Python from where the program is running. If the module is being run directly from the command line, the name is set to string “main”. Thus this behavior is used to check whether the module is run directly or imported to another file. Let’s see an example:


main1 ans


main2 ans

Too much info here, I know. Let’s dissect it piece by piece. First, we make a simple function greet which takes a name and greets the user. Then we take the name as input and call the function greet with that name. The output you know, “Good morning Kevin”.

Next, we make another file and import file main1.py to it then we use the greet function of main1 in it but when we try to run the program it says enter name but it didn’t use any name input in main2.py so why’s that, it should have print Good morning mark. But no, because when we import something from a file it includes everything. Not a single function. The single function we are accessing but that doesn’t mean it has not copied all the code from main1. So what to do now.

main1 re

main2 answer

So then we use if name == ‘main’_ to the name input and calling. And now we try to run the program it says a good morning mark. Now what happens here, is that when you try to run main1 with if__name_ == ‘main’, then name get replace with main1 which is true as this is the original main file. So the code gets executed without any problem. But when we import main1 to main2 and run. So now, the name is now main2 which is not true as this is not an original file and hence, the code is not executed.

Global Keyword

global keyword

The global keyword is used to modify the variable outside of the current scope. Ex:


global1 answer

global 2

global 2 answer

Here first we initialize the value of a which was 60 till now a is a global variable and its scope is global that means it can access anywhere in the program but now we define a function var which changes the value of a =15 when we try to run the program, we get 60 and 15. But a value was 15 so why 60 because outside function var a is still 60 but inside a is 15. a=15 is a local variable and the two are different. But if we want to access the outside a which was 60 then we use global keyword in function to do that.

Enumerate function in Python

The enumerate function adds a counter to an iterable and returns it. For example, you want to print an even number of items in the list you can do it simply with a for loop like this:


enumerate answer

We can also do it easily using the enumerate function. Let me show you how:


enumerate1 answer

List comprehensions

List comprehension is an elegant way to create lists based on existing lists.

list comprehension

list comprehension answer

The problem has a list l1 and we want another list with elements greater than 8 so one way of doing it was using for loop to iterate with a condition and appending the values in another list l2 but instead we use list comprehension and the same thing is done in one line only. We type list name which is l2 then we say item for item in l1, that means for every item in l1. Then we specify if item > 8. That’s our condition and we are done with that.

So, that’s it for today’s class guys. Today we have learned quite some concepts and their use. First, we saw Exception handling with different approaches. Then, if __name == ‘main’ clause, then global keyword followed by enumerate function and in the last list comprehensions. Now go on and practice this stuff. If you want to check out more about this, CLICK HERE. This is Dolores Haze, signing off, I will meet you in the next one, till then “Happy Coding” and bye.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top