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
Articles by Sarika Singh
Page 12 of 15
What 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 MoreHow can a Python function return a function?
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 MoreHow to call a function with argument list in Python?
The purpose of a function is to perform a specific task using code blocks. Functions save time by eliminating unnecessary copying and pasting of code. If you need to make a change, you only update the function in one place rather than searching through your entire program. This follows the DRY (Don't Repeat Yourself) principle in software development. Defining a Function in Python Python functions are created using the following syntax − def function_name(parameters): function body A function is defined using the def keyword followed by the function name and ...
Read MoreHow to expand tabs in string to multiple spaces in Python?
In Python, handling white spaces between strings is easy. Sometimes, we may want to add space in a string, but we are not sure exactly how much. Python provides different ways to manage this, and one useful method is the expandtabs() method. Using the expandtabs() Method The expandtabs() method in Python is used to replace tab characters (\t) in a string with spaces. It returns a new string where each \t is replaced with the number of spaces needed to reach the next tab stop. You can control how many spaces are used by passing a tabsize value ...
Read More