How to catch multiple exceptions in one line (except block) in Python?

Sarika Singh
Updated on 16-May-2025 14:39:26

452 Views

In Python, instead of writing separate except blocks for each exception, you can handle multiple exceptions together in a single except block by specifying them as a tuple. In this example, we are catching both ValueError and TypeError using a single except block - try: x = int("abc") # Raises ValueError y = x + "5" # Would raise TypeError if above line did not error except (ValueError, TypeError) as e: print("Caught an exception:", e) The above program will generate the following error ... Read More

What are RuntimeErrors in Python?

Sarika Singh
Updated on 16-May-2025 14:29:26

862 Views

RuntimeErrors in Python are a type of built-in exception that occurs during the execution of a program. They usually indicate a problem that arises during runtime and is not necessarily syntax-related or caused by external factors.When an error is detected, and that error doesn't fall into any other specific category of exceptions (or errors), Python throws a runtime error. Raising a RuntimeError manuallyTypically, a Runtime Error will be generated implicitly. But we can raise a custom runtime error manually, using the raise statement. ExampleIn this example, we are purposely raising a RuntimeError using the raise statement to indicate an unexpected condition in ... Read More

How to print all the keys of a dictionary in Python

Sarika Singh
Updated on 16-May-2025 10:46:37

107K+ Views

A Python dictionary is an unordered collection of data values. It contains a key-value pair, in contrast to other data structures that only include one value per entry. In this article, we are going to see the various ways to print all the keys of a dictionary in Python. Using dict.keys() Method Python's dict.keys() method can be used to retrieve the dictionary keys, which can be printed using the print() function. This method returns a list object, which contains every key in the dictionary. The dictionary elements can be accessed using the dict.keys() method, just like we do with a list ... Read More

What are .pyc files in Python?

Sumana Challa
Updated on 15-May-2025 19:56:59

40K+ Views

We usually write programs in Python and save the file with .py extension. However, there is another file type called .pyc,  which is automatically generated by the Python interpreter while executing the source code. What is a .pyc File? When you execute a Python program, the Python interpreter doesn't directly execute the .py file; instead, it parses the source code, compiles it into bytecode(a low-level representation of the Python source code), and stores it as the .pyc file. Further, this bytecode is executed with the Python Virtual Machine (PVM). A .pyc file is usually created when a Python program is ... Read More

C++ Program to Copy Strings

Nishu Kumari
Updated on 15-May-2025 19:46:46

4K+ Views

In this article, we'll show how to write a C++ program to copy strings. A string in C++ is a sequence of characters, such as letters, numbers, symbols, or anything enclosed in double quotes (e.g., "Hello"). Copying a string means transferring all characters from one string to another or making an exact copy. For example, here's how copying works: Input: "Learning C++ is fun!" Output: "Learning C++ is fun!" (This is the copied string) We can copy a string in C++ using the following methods: Using strcpy() Function ... Read More

C++ Program to Find Factorial of a Number using Iteration

Nishu Kumari
Updated on 15-May-2025 19:46:02

4K+ Views

In this article, we'll show you how to write a C++ program to find the factorial of a number using an iterative approach. The factorial of a number is the result of multiplying all the positive integers from 1 to that number. It is written as n! and is commonly used in mathematics and programming. Let's understand this with a few examples: //Example 1 Input: 5 The factorial of 5 is: 5 * 4 * 3 * 2 * 1 = 120 Output: 120 //Example 2 Input: 6 The factorial of 6 is: 6 * 5 * 4 ... Read More

C++ Program to Access Elements of an Array Using Pointer

Nishu Kumari
Updated on 15-May-2025 19:45:32

14K+ Views

In this article, we'll show you how to write a C++ program to access elements of an array using pointer. A pointer is a variable that holds the memory address of another variable, allowing us to reference it directly. In other words, pointers point to a memory location and obtaining the value stored at that location is called dereferencing the pointer. By using pointers, we can access and work with array elements through their memory addresses. Let's look at a simple example: Input: An array: [10, 20, 30, 40, 50] // We'll use a pointer to access ... Read More

C++ program to Reverse a Sentence Using Recursion

Nishu Kumari
Updated on 15-May-2025 19:44:29

2K+ Views

In this article, we will write a C++ program to reverse a sentence using recursion. Reversing a sentence means changing the order of all its characters in each word, so the first character moves to the end, the last character comes to the front, and the middle characters follow in the opposite order. Let's understand this with an example: // Example 1 Input: Welcome to tutorialspoint! // After reversing the sentence Output: !tniopslairotut ot emocleW // Example 2 Input: Learning C++ with tutorialspoint is fun // After reversing the sentence Output: nuf si tniopslairotut htiw ++C gninraeL ... Read More

C++ Program to Check Armstrong Number

Nishu Kumari
Updated on 15-May-2025 19:43:53

1K+ Views

An armstrong number is a positive integer that is equal to the sum of its digits, each raised to the power of the total number of digits in the number. Our goal here is to check armstrong numbers in C++. In simple terms, for a number with n digits: abcd... = a^n + b^n + c^n + d^n + ... If this condition is satisfied, we can say the number is an armstrong number. Let's understand with examples: 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 ... Read More

C++ Program to Display Armstrong Number Between Two Intervals

Nishu Kumari
Updated on 15-May-2025 19:43:05

2K+ Views

In this article, we will understand how to display all the prime numbers from 1 to N in Java. All possible positive numbers from 1 to infinity are called natural numbers. A number is a prime number if its only factors are 1 and itself and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers. Below is a demonstration of the ... Read More

Advertisements