Found 10784 Articles for Python

How to add/subtract large numbers using Python?

karthikeya Boyini
Updated on 05-Mar-2020 11:15:16

1K+ Views

You can add/subtract 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.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

How to find Kaprekar numbers within a given range using Python?

Nikitha N
Updated on 05-Mar-2020 11:13:49

645 Views

A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range. exampledef print_Kaprekar_nums(start, end):    for i in range(start, end + 1):       # Get the digits ... Read More

How to generate pyramid of numbers using Python?

Ankith Reddy
Updated on 05-Mar-2020 11:11:42

679 Views

There are multiple variations of generating pyramids using numbers in Python. Let's look at the 2 simplest formsExamplefor i in range(5):    for j in range(i + 1):       print(j + 1, end="")    print("")OutputThis will give the output1 12 123 1234 12345ExampleYou can also print numbers continuously usingstart = 1 for i in range(5):    for j in range(i + 1):       print(start, end=" ")       start += 1 print("")OutputThis will give the output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15ExampleYou can also print these numbers in reverse usingstart = 15 for i in range(5):    for j in range(i + 1):    print(start, end=" ")    start -= 1 print("")OutputThis will give the output15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

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

Samual Sam
Updated on 05-Mar-2020 11:09:25

106 Views

To calculate Catalan numbers using binomial Coefficients, you first need to write a function that calculates binomial coefficients. exampledef binomialCoefficient(n, k):    # To optimize calculation of C(n, k)    if (k > n - k):       k = n - k    coeff = 1    for i in range(k):       coeff *= (n - i)       coeff /= (i + 1)    return coeff def catalan(n):    return binomialCoefficient(2*n, n) / (n + 1) for i in range (11):    print (catalan(i))OutputThis will give the output −1.0 1.0 2.0 5.0 14.0 42.0 132.0 429.0 1430.0 4862.0 16796.0

How to handle very large numbers in Python?

Abhinanda Shri
Updated on 05-Mar-2020 11:08:24

11K+ Views

You can perform arithmetic operations on 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.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

How to Humanize numbers with Python?

Lakshmi Srinivas
Updated on 05-Mar-2020 10:59:25

223 Views

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'If you want to get results like 1.23 million for 1, 230, 000, you can use the humanize library to do so. You can install it using −$ pip install humanizeThen use it in the following way ... Read More

How to identify and print all the perfect numbers in some closed interval [ 2, n ] using Python?

Chandu yadav
Updated on 05-Mar-2020 10:55:58

1K+ Views

A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.You can find perfect numbers within a given range by testing each number for the given condition in the given range. exampledef print_perfect_nums(start, end):    for i in range(start, end + 1):    sum1 = 0    for x in range(1, i):       # Check if a divisor, if it is, add to sum       if(i % x == 0):          sum1 = sum1 + x          if (sum1 == i):             print(i) print_perfect_nums(1, 300)OutputThis will give the output6 28

How to format numbers to strings in Python?

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

524 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

437 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

How to find cosine of an angle using Python?

Priya Pallavi
Updated on 17-Jun-2020 13:19:23

1K+ Views

Python comes with an amazing standard math library that provides all trigonometric functions like sin, cos, etc. You can import that library and use these functions. Note that these functions expect angles in radians. For example,import math angle1 = math.radians(90) angle2 = math.radians(60) print(math.cos(angle1)) print(math.cos(angle2))This will give the output:6.123233995736766e-17 0.5The first value is very close to zero. Such errors come due to computation limits.

Advertisements