How to raise exception in Python

How to raise exception in Python

To raise an exception in Python, use the raise keyword. The raise keyword creates a user-defined exception. We can create our exception using the Python raise keyword. Even though we can handle exceptions in try and except block, there are some errors that python does not handle. In that case, raising exceptions helps us to handle the … Read more

How to Throw Error in Python

How to Throw Error in Python

To throw an error in Python, use the raise keyword. The raise keyword is used to raise an exception. You can define what kind of error to raise and the text to print to the user. The program will be terminated if the error is raised, and to avoid this, we need to handle the error. import … Read more

How to Catch Error in Python

How to Catch Error in Python

To catch an error in Python, use the try and except block. The try block contains the code that may cause an error. If the code inside the try block causes an error, we can catch these errors by using an except block. The except block catches the exception. If the exception caused and the exception … Read more

How to extract integer from string in Python

How to extract integer from string in Python

To extract an integer from a string in Python, use the isdigit() function. The String isdigit() is a built-in Python method that returns True if all the characters are digits. Otherwise, False. The string is a data type in Python that can have alphabets, numbers, and other special characters. Strings are immutable in Python. Hence, we cannot … Read more

How to Find Value in Array in Python

How to Find Value in Array in Python

To find a value in an array in Python, use the list.index() method. The list.index() is a built-in Python method that returns the index of the specified element in the list. In Python, we use the list as the array. A list is a data structure used to store sequential data values. We can store … Read more

How to Use the fromtimestamp() Function in Python

How to Use the fromtimestamp() Function in Python

The fromtimestamp() is a built-in Python function from the date class inside the datetime module that returns the date corresponding to a specified timestamp. To get the date from the timestamp in Python, use the datetime.date() function along with the fromtimestamp() function. What is a timestamp in Python A timestamp in Python is a long … Read more

How to Implement Interface in Python

How to Implement Interface in Python

In Python, we can create an interface class. It is used to specify a behavior that classes must implement. It is defined as the abstract type used to specify the behavior of a class. It is mainly used for achieving abstraction. For example, we can use an interface for performing multiple inheritance. Implement Interface in … Read more

How to Sort 2D Array in Python

How to Sort 2D Array in Python

To sort 2D array in Python, use either sort() or sorted() function. The sort() is a built-in list method that sorts the list ascending by default. It sorts the list by the length of the values. The 2D array is a type of array in which an array consists of several arrays inside it. Generally, … Read more