Server Side Programming Articles - Page 2547 of 2650

How to get signal names from numbers in Python?

Govinda Sai
Updated on 17-Jun-2020 14:55:56

530 Views

There is no straightforward way of getting signal names from numbers in python. You can use the signal module to get all its attributes. Then use this dict to filter the variables that start with SIG and finally store them in a dice. For example,Exampleimport signal sig_items = reversed(sorted(signal.__dict__.items())) final = dict((k, v) for v, k in sig_items if v.startswith('SIG') and not v.startswith('SIG_')) print(final)OutputThis will give the output:{: 'SIGTERM', : 'SIGSEGV', : 'SIGINT', : 'SIGILL', : 'SIGFPE', : 'SIGBREAK', : 'SIGABRT'}

How to print Narcissistic(Armstrong) Numbers with Python?

Sumana Challa
Updated on 16-May-2025 19:37:20

465 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 = 370. The algorithm to check for an Armstrong number is as follows - 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. Calculate the sum of the power. Compare ... Read More

How to clamp floating numbers in Python?

Sumana Challa
Updated on 06-May-2025 18:53:39

6K+ Views

Clamping refers to limiting a number to a specific range, i.e., making sure that the number lies between the minimum and maximum value mentioned. This method is used in applications like graphics and statistical computations, as it requires the data to stick to specific limits. Clamping Floating Numbers in Python The following are some of the approaches to clamp floating numbers in Python - Creating a User-Defined Function Since Python has no built-in clamp function, in the following program, we will create our clamp() function, which takes three parameters - n (number to be clamped), min (minimum value), and max (maximum ... Read More

How to convert numbers to words using Python?

Sumana Challa
Updated on 30-Apr-2025 18:10:50

2K+ Views

The given task is to convert the numerical values to their respective word representation (i.e, we need to spell the numbers in text ). For example, if the given numbers are 1, 2, 29, the resultant values would be: one, two, twenty-nine, respectively. We can do so, using the function(s) available in the num2word library. Converting Numbers to Words Using num2word() The num2words() is a function of the Python library with the same name (num2words). This is used to convert numbers like 56 to words like fifty-six. In addition to the numerical parameters, this function accepts two optional parameters - ... Read More

How to bitwise XOR of hex numbers in Python?

Sumana Challa
Updated on 09-May-2025 10:44:12

3K+ Views

The bitwise XOR is a binary operation in which we compare two binary numbers bit by bit and return the value "1" if the bits are not same, and 0 if they are the same. The XOR(exclusive OR) operation follows the below rules - A B A ⊕ B ... Read More

How to divide large numbers using Python?

Sumana Challa
Updated on 28-May-2025 15:25:14

1K+ Views

Python allows you to perform the basic mathematical operations like addition, subtraction, multiplication, and division on large numbers as Python's integers are arbitrary-precision, hence there is no limit on their size.You can divide large numbers as you would normally do. But this has a lot of precision issues as such operations cannot be guaranteed to be precise, as it might slow down the language.Usually, dividing large numbers results in a floating-point number, which cannot be precise since Python's float can only accurately be represented up to 15-17 decimal digits.To overcome this issue, we can perform division operation using division operator ... Read More

How to multiply large numbers using Python?

Sumana Challa
Updated on 28-May-2025 19:11:48

3K+ Views

You can multiply large numbers in Python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. As long as you have version 2.5 or better, just perform standard math operations, and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum. To multiply large numbers in Python, we can use the basic multiplication operator or use the fractions ... Read More

How to calculate catalan numbers with the method of Binominal Coefficients using Python?

Sumana Challa
Updated on 05-Jun-2025 13:56:50

248 Views

Catalan numbers are defined as a sequence of natural numbers that can be used to find the number of possibilities of various combinations. The below formula is used to calculate catalan number using the binomial coefficient ( denoted as (nk) and represents the number of ways to choose k items from n )- For example, if the input parameter n is given 6, the output would be 142, that is calculated using the above formula in the following way: C(6)=C(0)C(5) + C(1)C(4) + C(2)C(3) + C(3)C(2) + C(4)C(1) + C(5)C(0) Following are the two main methods used to calculate the ... Read More

How to format numbers to strings in Python?

V Jyothi
Updated on 05-Mar-2020 10:54:42

644 Views

You can format a floating number to a fixed width in Python using the format function on the string. examplenums = [0.555555555555, 1, 12.0542184, 5589.6654753] for x in nums:    print("{:10.4f}".format(x))OutputThis will give the output −0.5556 1.0000 12.0542 5589.6655ExampleUsing the same function, you can also format integers −nums = [5, 20, 500] for x in nums:    print("{:d}".format(x))OutputThis will give the output −5 20 500ExampleYou can use it to provide padding as well, by specifying the number before d:nums = [5, 20, 500] for x in nums:    print("{:4d}".format(x))OutputThis will give the output −5 20 500The https://pyformat.info/ website is a great ... Read More

How to add binary numbers using Python?

karthikeya Boyini
Updated on 17-Jun-2020 14:39:15

694 Views

If you have binary numbers as strings, you can convert them to ints first using int(str, base) by providing the base as 2. Then add the numbers like you'd normally do. Finally convert it back to a string using the bin function. For example,a = '001' b = '011' sm = int(a,2) + int(b,2) c = bin(sm) print(c)This will give the output:0b100

Advertisements