Found 27104 Articles for Server Side Programming

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

629 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 calculate catalan numbers with the method of Binominal Coefficients using Python?

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

101 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 format numbers to strings in Python?

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

519 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

427 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.

How to use multiple for and while loops together in Python?

Ankith Reddy
Updated on 17-Jun-2020 12:36:21

250 Views

You can create nested loops in python fairly easily. You can even nest a for loop inside a while loop or the other way around. For example,for i in range(5):    j = i    while j != 0:       print(j, end=', ')       j -= 1    print("")This will give the output1, 2, 1, 3, 2, 1, 4, 3, 2, 1,You can take this nesting to as many levels as you like.

How to use single statement suite with Loops in Python?

karthikeya Boyini
Updated on 17-Jun-2020 12:29:42

233 Views

Similar to the if statement syntax, if your while clause consists only of a single statement, it may be placed on the same line as the while header. Here are the syntax and example of a one-line for loop:for i in range(5): print(i)This will give the output:0 1 2 3 4

How do I run two python loops concurrently?

Lakshmi Srinivas
Updated on 17-Jun-2020 12:22:33

615 Views

You will need to use a multiprocessing library. You will need to spawn a new process and provide the code to it as an argument. For example,from multiprocessing import Processdef loop_a():    for i in range(5):       print("a") def loop_b():    for i in range(5):       print("b") Process(target=loop_a).start() Process(target=loop_b).start()This might process different outputs at different times. This is because we don't know which print will be executed when.

Python: Can not understand why I am getting the error: Can not concatenate 'int' and 'str' object

Ayush Gupta
Updated on 12-Mar-2020 12:31:58

79 Views

This error is coming because the interpreter is replacing the %d with i and then trying to add 1 to a str, ie, adding a str and an int. In order to correct this, just surround the i+1 in parentheses. exampleprint("\ Num %d" % (i+1))

Advertisements