Programming Articles

Page 118 of 2547

What is unexpected indent in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 864 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 ...

Read More

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

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 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 ...

Read More

How to ignore an exception and proceed in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 35K+ Views

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 More

How to handle a python exception within a loop?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 15K+ 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 ...

Read More

How to catch a python exception in a list comprehension?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 3K+ 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 ...

Read More

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

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 240 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 ...

Read More

How do I check if a Python variable exists?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 24K+ Views

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 More

How to print the Python Exception/Error Hierarchy?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 4K+ Views

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

How to raise Python exception from a C extension?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 415 Views

When writing a C extension for Python, you might need to raise an exception if something goes wrong in your C code. Python provides a special C API that helps you to raise errors in a way that works just like regular Python exceptions. This is helpful because even if your code is written in C for better performance, users can still handle errors using Python's normal try...except blocks. It makes your extension feel just like any other Python module. Using Python C API to Raise Exceptions In C extensions for Python, you can raise exceptions using ...

Read More

How can a Python function return a function?

Sarika Singh
Sarika Singh
Updated on 18-Mar-2026 19K+ Views

In Python, functions are treated as first-class objects, which means all functions in Python are treated like any other object or variable. You can assign a function to a variable, pass it as an argument to another function, return it from a function, or even store it in data structures like lists. One very useful feature in Python is that a function can return another function. This means you can define a function inside another function and return it as a result. This is used in advanced programming concepts like closures, decorators, and higher-order functions. Syntax Following ...

Read More
Showing 1171–1180 of 25,469 articles
« Prev 1 116 117 118 119 120 2547 Next »
Advertisements