
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10397 Articles for Python

2K+ Views
In Python, the except clause is used to handle exceptions that may occur inside a try block. But what happens if no exceptions are raised? The except block is simply skipped. When No Exceptions Occur If the code inside the try block executes without raising any exceptions, the except block is ignored, and the program continues normally. Example: No exceptions raised In this example, we are performing a simple division that doesn't raise an exception, so the except block does not run - try: result = 10 / 2 print("Division successful:", result) except ZeroDivisionError: ... Read More

576 Views
This can be done by following codestry: x, y =7, 0 z = x/y except: passORtry: x, y =7, 0 z = x/y except Exception: passThese codes bypass the exception in the try statement and ignore the except clause and don’t raise any exception.The difference in the above codes is that the first one will also catch KeyboardInterrupt, SystemExit etc, which are derived directly from exceptions.BaseException, not exceptions.Exception.It is known that the last thrown exception is remembered in Python, some of the objects involved in the exception-throwing statement are kept live until the next exception. We might want to do ... Read More

3K+ Views
Using "except" Clause with Multiple Exceptions It is possible to define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it will execute the code written under the except clause. Syntax In general, the syntax for multiple exceptions is as follows - Except(Exception1, Exception2, …ExceptionN) as e: When we define the except clause in this way, we expect the same code to throw different exceptions. Also, we want to take action in each case. Example In this example, we are trying to add an integer and a string, which is not ... Read More

2K+ Views
To pass a variable to an exception in Python, provide the variable as an argument when raising the exception. For custom exceptions, store the variable in an attribute. You can pass variables like strings or numbers directly into built-in exceptions to include dynamic data in the error message. Example: Passing a variable to a ValueError In this example, we are passing a variable containing an invalid input message to a ValueError - value = "abc123" try: raise ValueError(f"Invalid input: {value}") except ValueError as e: print("Caught exception:", e) We get the following output - ... Read More

346 Views
In Python, you can create your own custom exception classes to represent specific types of errors in your program. When you raise these custom exceptions, you can also pass an object (like a string or other data) to explain more about what went wrong. This helps make your error messages more useful and detailed. Creating a Custom Exception Class In Python, you can pass an object or any extra information with a custom exception by defining a class that inherits from the built-in Exception class. Inside the custom class, you override the __init__() method to accept additional arguments, and ... Read More

760 Views
RuntimeErrors in Python are a type of built-in exception that occurs during the execution of a program. They usually indicate a problem that arises during runtime and is not necessarily syntax-related or caused by external factors.When an error is detected, and that error doesn't fall into any other specific category of exceptions (or errors), Python throws a runtime error. Raising a RuntimeError manuallyTypically, a Runtime Error will be generated implicitly. But we can raise a custom runtime error manually, using the raise statement. ExampleIn this example, we are purposely raising a RuntimeError using the raise statement to indicate an unexpected condition in ... Read More

414 Views
In Python, instead of writing separate except blocks for each exception, you can handle multiple exceptions together in a single except block by specifying them as a tuple. In this example, we are catching both ValueError and TypeError using a single except block - try: x = int("abc") # Raises ValueError y = x + "5" # Would raise TypeError if above line did not error except (ValueError, TypeError) as e: print("Caught an exception:", e) The above program will generate the following error ... Read More

692 Views
In Python, you can check whether a substring exists within another string using Python in operator, or string methods like find(), index(), and __contains__(). A string in Python is a sequence of characters that is enclosed in quotes. You can use either single quotes '...' or double quotes "..." to write a string, like this - "Hello" //double quotes 'Python' //single quote A substring in Python simply means a part of a string. For example, text = "Python" part = "tho" Here, "tho" is a substring of the string "Python". Using the in operator We can check ... Read More

931 Views
In Python, a KeyError exception occurs when a dictionary key that does not exist is accessed. This can be avoided by using proper error handling with the try and except blocks, which can catch the error and allow the program to continue running. Understanding KeyError in Python A KeyError is raised when you try to access a key that doesn't exist in a dictionary. It is one of the built-in exceptions that can be handled by Python's try and except blocks. Example: Accessing a non-existent key In this example, we are attempting to access a key that does not exist ... Read More

327 Views
In Python, the assert statement is used for debugging purposes. It tests whether a condition in your program returns True, and if not, it raises an AssertionError. This helps you catch bugs early by verifying that specific conditions are met while the code is executing. Understanding the Assert Statement The assert statement is used to verify that a given condition is true during execution. If the condition evaluates to False, the program stops and throws an AssertionError, optionally displaying a message. Example: Assertion without a message In this example, we are asserting that 5 is greater than 3, which is ... Read More