Found 10784 Articles for Python

How to Plot Complex Numbers in Python?

Abhinaya
Updated on 18-Jun-2020 06:38:34

2K+ Views

You can plot complex numbers on a polar plot. If you have an array of complex numbers, you can plot it using:import matplotlib.pyplot as plt import numpy as np cnums = np.arange(5) + 1j * np.arange(6,11) X = [x.real for x in cnums] Y = [x.imag for x in cnums] plt.scatter(X,Y, color='red') plt.show()This will plot a graph of the numbers in a complex plane.

How to find Square root of complex numbers in Python?

Chandu yadav
Updated on 18-Jun-2020 06:40:16

444 Views

You can find the Square root of complex numbers in Python using the cmath library. The cmath library in python is a library for dealing with complex numbers. You can use it as follows to find the square root −Examplefrom cmath import sqrta = 0.2 + 0.5j print(sqrt(a))OutputThis will give the output (0.6076662244659689+0.4114100635092987j)

How to get signal names from numbers in Python?

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

341 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?

Ankith Reddy
Updated on 17-Jun-2020 14:54:08

284 Views

To print Narcissistic Numbers, let's first look at the definition of it. It is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 1, 153, 370 are all Narcissistic numbers. You can print these numbers by running the following codedef print_narcissistic_nums(start, end): for i in range(start, end + 1):    # Get the digits from the number in a list:    digits = list(map(int, str(i)))    total = 0    length = len(digits)    for d in digits:       total += d ** length   ... Read More

How to clamp floating numbers in Python?

George John
Updated on 17-Jun-2020 14:51:13

6K+ Views

Clamp function limits a value to a given range. Python doesn't have such a function in built. You can create this function likedef clamp(num, min_value, max_value):    return max(min(num, max_value), min_value) print(clamp(5, 1, 20)) print(clamp(1, 10, 20)) print(clamp(20, 1, 10))This will give the output5 10 10

How can we generate Strong numbers in Python?

Sravani S
Updated on 17-Jun-2020 14:52:33

151 Views

To print Strong Numbers, let's first look at the definition of it. It is a number that is the sum of factorials of its own digits. For example, 145 is a Strong number. First, create a function to calculate factorial:def fact(num):    def factorial(n):    num = 1    while n >= 1:       num = num * n       n = n - 1    return numYou can print these numbers by running the following code:def factorial(n):    num = 1    while n >= 1:       num = num * n   ... Read More

How to convert numbers to words using Python?

Arjun Thakur
Updated on 17-Jun-2020 14:48:33

1K+ Views

The constructor for the string class in python, ie, str can be used to convert a number to a string in python. For example, i = 10050 str_i = str(i) print(type(str_i))This will give the output:But if you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using$ pip install pynum2wordThen use it in the following way>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223) 'one thousand, two hundred and twenty-three'Read More

How to bitwise XOR of hex numbers in Python?

Ramu Prasad
Updated on 17-Jun-2020 14:47:12

2K+ Views

You can get the XOR of any type of numbers using the ^ operator. Specifically for hex numbers, you can use:a = 0x12ef b = 0xabcd print(hex(a ^ b))This will give the output:0xb922The 0x at the beginning of the numbers implies that the number is in hex representation. You can use the ^ operator for other integer representations as well.

How to divide large numbers using Python?

George John
Updated on 30-Jul-2019 22:30:22

918 Views

You can divide large numbers in python 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. You would be better off using a numeric computation library like bigfloat to perform such operations.You can read more about floating point issues that you might face with precision on https://docs.python.org/3/tutorial/floatingpoint.html

How to multiply large numbers using Python?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

2K+ 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. For example, a = 15421681351 b = 6184685413848 print(a * b) This will give the output − 95378247708541418748648

Advertisements