
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 10533 Articles for Python

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

668 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

393 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

592 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

902 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

292 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

499 Views
In Python, the try, except, and finally blocks are used to handle exceptions. These blocks allow you to catch errors that occur during the execution of a program and respond accordingly, which helps to prevent your program from crashing. Try and Except Blocks The try block contains code that might raise an exception. If an exception occurs, the except block handles it, preventing the program from crashing. Example: Handling ZeroDivisionError In this example, we are dividing a number by zero, which raises an exception and is handled by the except block - try: result = ... Read More

528 Views
In Python, indentation is used to define the structure and flow of code. Unlike many other programming languages that use braces to define code blocks, Python relies on indentation. If the indentation is incorrect or inconsistent, Python will throw an IndentationError, specifically an unexpected indent error. In this article, we will understand what the unexpected indent error is, how it occurs, and how to fix it. Understanding Indentation in Python In Python, indentation is used to define the boundaries of code blocks, such as loops, conditionals, functions, and classes. Python uses indentation levels (spaces or tabs) to define code blocks, ... Read More

1K+ Views
You can handle errors in Python using the try and except blocks. Sometimes, we also want to store the actual exception in a variable to print it or inspect it. This is done using the as keyword in modern Python. But if you have seen older code, you might find except Exception e or even except Exception, e being used. In this article, we will explore the difference between these syntaxes, understand why older ones no longer work in Python 3, and learn the best practices for clean exception handling. Understanding Basic Exception Handling Python provides try-except blocks to catch ... Read More

33K+ Views
An exception is an unexpected error or event that occurs during the execution of program. The difference between an error and an exception in a program is that, when an exception is encountered, the program deflects from its original course of execution whereas when an error occurs, the program is terminated. Hence, unlike errors, an exception can be handled. Therefore, you won't have a program crash. However, in some cases of Python, the exception might not cause the program to terminate and will not affect the direction of execution hugely. Therefore, it is best to ignore such kind of exceptions. ... Read More