Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 677 of 2109
How to check if a substring is contained in another string in Python?
In Python, you can check whether a substring exists within another string using the in operator, or string methods like find(), index(), and __contains__(). A string in Python is a sequence of characters enclosed in quotes. You can use either single quotes '...' or double quotes "..." ? text1 = "Hello" # double quotes text2 = 'Python' # single quotes print(text1) print(text2) Hello Python A substring is simply a part of a string. For example ? text = "Python" substring = "tho" print(f"'{substring}' is a ...
Read MoreHow to catch KeyError Exception in Python?
In Python, a KeyError exception occurs when trying to access a dictionary key that does not exist. This can be handled using try-except blocks to prevent program crashes and provide graceful error handling. Understanding KeyError in Python A KeyError is raised when you attempt to access a key that doesn't exist in a dictionary. It's one of Python's built-in exceptions that can be caught and handled properly. Example: Accessing a Non-existent Key Here's what happens when trying to access a missing key ? my_dict = {"apple": 1, "banana": 2} print(my_dict["orange"]) The output ...
Read MoreWhat is unexpected indent in Python?
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 ...
Read MoreWhat is the difference between \'except Exception as e\' and \'except Exception, e\' in Python?
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 ...
Read MoreHow to ignore an exception and proceed in Python?
An exception is an unexpected error or event that occurs during program execution. The difference between an error and an exception is that when an exception is encountered, the program deflects from its original course of execution, whereas when an error occurs, the program terminates. Hence, unlike errors, exceptions can be handled to prevent program crashes. In some cases, certain exceptions might not significantly affect program execution and can be safely ignored. Python provides several ways to handle this: Using the try/except block Using the suppress() method Using the try/except Block The try/except block ...
Read MoreHow to handle a python exception within a loop?
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 ...
Read MoreHow to catch a python exception in a list comprehension?
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 ...
Read MoreWhere\'s the standard python exception list for programmers to raise?
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 ...
Read MoreHow do I check if a Python variable exists?
Variables are defined as the containers used to store data in memory. In Python, variables don't need explicit type declaration and are created by simply assigning a value to a name. However, before using a variable, it must be defined first to avoid errors. x = 5 print(x) 5 Here, x is the variable name holding an integer value. Understanding Variable Scope Variable scope determines where an identifier can be accessed within a program. Python has two basic scopes ? Local variables − Defined within a function or block ...
Read MoreHow to print the Python Exception/Error Hierarchy?
We will learn about what an exception is before learning how to print Python exceptions. An exception occurs when a program fails to execute in the direction it was intended to be. Python throws exceptions when unexpected errors or events occur during program execution. Exceptions can be both recoverable and unrecoverable. The exception handling technique can be used when you suspect your code may create an error, preventing the software from crashing. Following are some common exceptions in Python − IOError (Input Output Error) − When the file cannot be opened or ...
Read More