Articles on Trending Technologies

Technical articles with clear explanations and examples

Can someone help me fix this Python Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Mar-2026 161 Views

This Python program demonstrates fixing common syntax and logical errors in a conversational script. The original code had indentation problems and undefined variable issues that prevented it from running correctly. Common Issues in the Original Code The main problems were: Indentation errors − Missing indentation inside if blocks Undefined variables − Variable 'name' used before assignment Python 2 print syntax − Using print statements instead of print() function Logic flow − Variable defined in one branch but used in another Corrected Version Here's the fixed code with proper indentation, variable handling, and Python ...

Read More

Reply to user text using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Mar-2026 2K+ Views

You can create an interactive text-based response system using Python's if-elif-else statements and while loops. This approach ensures the program continues asking for input until the user provides a valid option. The key is to use int() to convert user input to an integer and validate it against the available options. Example Here's a complete interactive response system − print("Come-on in. Need help with any bags?") while True: # Loop continues until valid option is selected try: bag ...

Read More

Python - How to convert this while loop to for loop?

Pythonista
Pythonista
Updated on 24-Mar-2026 1K+ Views

Converting a while loop to a for loop in Python can be achieved using itertools.count() which creates an infinite iterator. This is particularly useful when you need to iterate indefinitely until a specific condition is met. Understanding itertools.count() The count() function from the itertools module generates an iterator of evenly spaced values. It takes two optional parameters: start − Starting value (default is 0) step − Step size between values (default is 1) Using default parameters creates an infinite iterator, so you must use break to terminate the loop when needed. Example: Converting ...

Read More

How to get signal names from numbers in Python?

Govinda Sai
Govinda Sai
Updated on 24-Mar-2026 616 Views

Python's signal module allows you to work with system signals, but getting signal names from their numeric values requires some introspection. You can create a mapping dictionary by examining the signal module's attributes and filtering for signal constants. Creating a Signal Number to Name Mapping The signal module contains all signal constants as attributes. We can iterate through these attributes to build a mapping ? import signal # Get all signal module attributes and create mapping sig_items = reversed(sorted(signal.__dict__.items())) signal_map = dict((k, v) for v, k in sig_items if v.startswith('SIG') and not v.startswith('SIG_')) print("Signal ...

Read More

How to print Narcissistic(Armstrong) Numbers with Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 597 Views

A narcissistic number (also known as an Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 370 = 33 + 73 + 03 = 27 + 343 + 0 = 370. Algorithm Steps The algorithm to check for an Armstrong number follows these steps ? Determine the number of digits for the mentioned number. Extract each digit and calculate the power of that digit with the exponent equal to the number of digits. ...

Read More

How to clamp floating numbers in Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 7K+ Views

Clamping refers to limiting a number to a specific range, ensuring that the number lies between the minimum and maximum values. This method is used in applications like graphics and statistical computations, as it requires data to stick to specific limits. Creating a User-Defined Function Since Python has no built-in clamp function, we can create our own clamp() function that takes three parameters − n (number to be clamped), min_val (minimum value), and max_val (maximum value) ? def clamp(n, min_val, max_val): if n < min_val: ...

Read More

How to convert numbers to words using Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 2K+ Views

Converting numbers to words is a common requirement in applications like check writing, invoice generation, and text-based reports. Python's num2words library provides a simple way to convert numerical values to their word representations. For example, converting 1, 2, 29 to "one", "two", "twenty-nine" respectively. Installing the num2words Library Before using the library, install it using pip − pip install num2words Basic Number to Words Conversion The num2words() function converts integers and floats to their word equivalents − from num2words import num2words # Convert simple integers print(num2words(42)) # ...

Read More

How to bitwise XOR of hex numbers in Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 4K+ Views

The bitwise XOR is a binary operation that compares two binary numbers bit by bit and returns "1" if the bits are different, and "0" if they are the same. The XOR (exclusive OR) operation follows these rules ? A B A ⊕ B ...

Read More

How to divide large numbers using Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 2K+ Views

Python allows you to perform basic mathematical operations like addition, subtraction, multiplication, and division on large numbers. Python's integers are arbitrary-precision, meaning there is no limit on their size. However, dividing large numbers has some considerations: Regular division results in floating-point numbers, which have precision limitations (15-17 decimal digits) For high-precision results, you need special approaches Python provides multiple division methods: regular division (/), floor division (//), and the decimal module Let us explore each approach with examples ? Using Division Operator (/) The ...

Read More

How to multiply large numbers using Python?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 3K+ Views

Python has built-in support for arbitrarily large integers, making it easy to multiply numbers of any size without worrying about overflow errors. Since Python 3, all integers automatically handle large numbers transparently. Python's integer type can work with numbers far beyond the limits of traditional 32-bit or 64-bit systems. When you perform arithmetic operations, Python automatically handles the memory allocation needed for large results. Using the Multiplication Operator The multiplication operator (*) works seamlessly with numbers of any size in Python ? # Multiply very large numbers a = 15421681351 b = 6184685413848 c = ...

Read More
Showing 1–10 of 61,302 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements