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

7K+ Views
In Python, strings can contain letters, numbers, or special characters. To check if a string is alphanumeric (contains only letters and numbers), we can use different methods. This article shows three simple ways to do that: Using the isalnum() function Using regular expressions Using the isalpha() and isdigit() functions Using the isalnum() Function The isalnum() method returns True if all characters in the string are letters or digits; otherwise, it returns False. Example: Basic Alphanumeric Check In the example below, we take two strings and check if they contain only alphabets and numbers using the isalnum() function: str1 ... Read More

271 Views
For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value must be within 1 and 10. Process finished with exit code 0

847 Views
A suffix is a group of letters added at the end of a word. In Python, we can check if a string ends with any one of multiple suffixes using the endswith() method. It takes a tuple of suffixes as an argument and returns True if the string ends with any of them. This is useful for checking file extensions, URL endings, or word patterns. Using endswith() with Multiple Suffixes The endswith() method allows you to check if a string ends with any one of several suffixes by passing them as a tuple. This helps to check for multiple ... Read More

2K+ Views
You cannot directly use an if statement to catch exceptions in Python. Instead, use a try-except block and place if conditions inside the except to respond differently based on the type or message of the exception. This allows you to write conditional logic for exception handling. Using if Inside the except Block You can write if conditions inside the except block to check the type or details of the exception and take appropriate action. Example: Matching Exception Message with "if" In this example, we catch a ValueError and use if statement to inspect its message - def process_input(value): ... Read More

917 Views
Are Python Exceptions Runtime Errors? Yes, Python exceptions are considered runtime errors. Most exceptions occur during runtime.Exceptions in Python are errors that occur when there is an abnormal scenario during the execution of a program, which terminates the execution abruptly, interrupting its normal flow. Examples of exceptions are ZeroDivisionError, IndexError, ValueError, etc..Whereas, errors in Python are detected before the execution of the program, they include syntactical errors and other violations of the program rules. Examples of errors are SyntaxError, IndentationError, etc. Exceptions are Raised at Runtime Exceptions occur when the execution of the program is interrupted because of issues like logical ... Read More

1K+ Views
Custom exceptions in Python allow you to create meaningful error types that fit your application's needs. By adding error codes and error messages to custom exceptions, you can provide structured (organized) information when errors occur. Although Python has many built-in exceptions, custom exceptions are well-suited to explain specific problems in your program. Including error codes and messages, handling errors, and debugging your code. Creating a Custom Exception with Error Code You can create a custom exception by defining a class that inherits from Python's built-in Exception class. This allows you to include additional details, such as a custom error message ... Read More

180 Views
Python uses the try-except block to handle errors during runtime. But as your program grows, handling exceptions can get messy with repeated code or too many nested blocks. Using cleaner methods for exception handling can reduce duplication, and make your code easier to manage. In this article, we explore better ways to handle exceptions in Python. Using Specific Exception Types It is best to catch specific exceptions like ValueError or FileNotFoundError instead of using a general except: block. This prevents hiding unexpected errors and improves debugging This approach avoids catching unrelated exceptions like KeyboardInterrupt or TypeError. Example In this ... Read More

134 Views
In Python, the with statement is used when working with files or network connections. It makes sure that resources (files) are opened, used, and then closed properly, even if an error occurs during the process. If you want to catch any exceptions that happen inside a with block, you can wrap it in a try-except statement. Alternatively, you can use a custom context manager that handles exceptions on its own. Catching Exceptions Inside with Block Using try-except To catch errors that occur inside a with block, we need to place it within a try-except. This helps you to handle any ... Read More

808 Views
Python threads do not automatically pass exceptions to the caller thread. To catch thread exceptions in the main thread, you can use custom thread classes, the ThreadPoolExecutor with futures, or a shared queue. Using a Wrapper with threading.Thread The easiest way to catch exceptions in a thread is to wrap the target function in a try-except block. You can then store the exception in a shared variable or object so it can be checked later from the main thread. Example In this example, we create a custom thread class that stores any exceptions in an instance variable. After the ... Read More

275 Views
Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.Let us write 2 try...except blocks like this:try: try: 1/0 except ArithmeticError as e: if str(e) == "Zero division": print ("thumbs up") else: raise except Exception as err: print ("thumbs down") raise errwe get the following outputthumbs down Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 11, in raise err File "C:/Users/TutorialsPoint1/~.py", line 3, in 1/0 ZeroDivisionError: division by zeroAs per python tutorial there is one and only one ... Read More