Posts

Showing posts with the label Try Except Exception Handling

Python Tutorial 31 --- "If __name__==__main__ usage & necessity"

Python Tutorial 31 --- "If __name__==__main__ usage & necessity" Using  if __name__ == “__main__”  statement is one such concept that may be difficult to grasp for beginners, but once learned, it is very helpful and used quite often afterward. However, it may seem confusing at times. This article aims to provide an understanding of the behavior of the statement and further discusses how to use it. As discussed earlier, a module is just a file containing a python code with a .py extension and can be imported to other files. That's where the keyword  __name__  comes in. Let’s understand __name__ first before moving onto if __name__ == “__main__. What is __name__? “A __name__ is a built-in variable that returns us the name of the module being used.” In simple words, by using  __name__ , we can check whether our module is being imported or run directly. If we run it in the same module that it is created in, then it will print “main” onto the screen; otherwise, i...

Python Tutorial 15 --- “Try Except Exception Handling In Python”

 Python Tutorial 15 ---  “Try Except Exception Handling In Python” This PROGRAM or TUTORIAL is about “Try Except Exception Handling In Python” DOC Before discussing exceptional handling, let us discuss, what an exception is actually. “Exception can be said as an error, that causes a program to crash. Unlike syntax error, it is syntactically correct and occurs mostly due to our negligence” For example, assigning a string value to an int data type variable or dividing a number by zero or also when a name of a variable or a function is not found, an exception occurs. Python has built-in support for dealing with many sorts of exceptions automatically, but we can also define our own.    The solution to exception related problems is simple. We just have to alter the normal flow of the program by a bit of code known as an exception handler. This code will save the state of the program up to the point where the exception occurred and will continue the normal flow fro...