Found 10400 Articles for Python

How to check if a substring is contained in another string in Python?

Sarika Singh
Updated on 22-May-2025 16:46:11

645 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

How to catch KeyError Exception in Python?

Sarika Singh
Updated on 15-May-2025 13:54:35

921 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

What is the use of \"assert\" statement in Python?

Sarika Singh
Updated on 15-May-2025 13:57:00

310 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

Explain try, except and finally statements in Python.

Sarika Singh
Updated on 15-May-2025 13:07:58

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

What is unexpected indent in Python?\\\

Sarika Singh
Updated on 14-May-2025 15:00:39

558 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

What is the difference between \'except Exception as e\' and \'except Exception, e\' in Python?

Sarika Singh
Updated on 15-May-2025 14:07:20

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

How to ignore an exception and proceed in Python?

Sarika Singh
Updated on 14-May-2025 17:07:46

34K+ 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

How to handle a python exception within a loop?

Sarika Singh
Updated on 14-May-2025 17:11:18

14K+ Views

The looping technique in Python transforms complex problems into simple ones. It allows us to change the flow of the program so that instead of writing the same code over and over, we can repeat it a limited number of times until a certain condition is satisfied. For example, if we need to display the first ten natural numbers, we can do it inside a loop that runs up to ten iterations rather than using the print command ten times. Python offers three ways to loop a block of code in a program: using for loops, while loops and ... Read More

How to catch a python exception in a list comprehension?

Sarika Singh
Updated on 14-May-2025 17:48:47

2K+ Views

Before we understand how to catch a Python exception in a list comprehension, let us first learn what a list comprehension is.Python List Comprehension List comprehension is a statement that allows you to create a list and execute a for loop, all in a single sentence.This also allows the lists to be created from other iterables like tuples, strings, arrays, lists, and so on. The syntax of list comprehension is given below - List = [expression(element) for element in list if condition] The Python list and list comprehension capability, which may be used inside a single line of code ... Read More

Where\'s the standard python exception list for programmers to raise?

Sarika Singh
Updated on 14-May-2025 15:21:35

133 Views

In Python, an exception is an error that occurs at the time of execution. These will terminate the program abruptly. If you are a programmer looking to raise meaningful exceptions, it is important to know the list of standard exceptions Python provides. What Are Python Exceptions? Exceptions in Python are built-in classes that inherit from the base class BaseException. They help you to manage errors like missing files, wrong data types, division by zero, or custom business logic violations. Where Can You Find the Full List? The complete list of built-in exceptions is available in the official Python documentation. You ... Read More

Advertisements