Python Articles - Page 953 of 1048

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 handle very large numbers in Python?

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

13K+ 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 format numbers to strings in Python?

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

638 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

691 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

2K+ 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 check if a float value is a whole number in Python?

George John
Updated on 17-Jun-2020 12:57:59

2K+ Views

To check if a float value is a whole number, use the float.is_integer() method. For example,print((10.0).is_integer()) print((15.23).is_integer())This will give the outputTrue False

How to compare string and number in Python?

Nikitha N
Updated on 17-Jun-2020 12:56:56

2K+ Views

Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).When you order a numeric and a non-numeric type, the numeric type comes first.If you have a number in a str object, you can simply convert it to a float or an int using their respective constructors. For example, i = 100 j = "12" int_j = int(j) print(int_j ... Read More

How to compare numbers in Python?

Samual Sam
Updated on 05-Mar-2020 10:48:16

17K+ Views

You can use relational operators in python to compare numbers(both float and int) in python. These operators compare the values on either side of them and decide the relation among them. Assume variable a holds 10 and variable b holds 20, thenOperatorExample==(a == b) is not true.!=(a != b) is true.>(a > b) is not true.=(a >= b) is not true.= b) print(a

How to Find Hash of File using Python?

Arjun Thakur
Updated on 05-Mar-2020 10:46:21

3K+ Views

You can find the hash of a file using the hashlib library. Note that file sizes can be pretty big. Its best to use a buffer to load chunks and process them to calculate the hash of the file. You can take a buffer of any size.Exampleimport sys import hashlib BUF_SIZE = 32768 # Read file in 32kb chunks md5 = hashlib.md5() sha1 = hashlib.sha1() with open('program.cpp', 'rb') as f: while True:    data = f.read(BUF_SIZE)    if not data:       break    md5.update(data)    sha1.update(data) print("MD5: {0}".format(md5.hexdigest())) print("SHA1: {0}".format(sha1.hexdigest()))OutputThis will give the outputMD5: 7481a578b20afc6979148a6a5f5b408d SHA1: ... Read More

How to Find ASCII Value of Character using Python?

Chandu yadav
Updated on 05-Mar-2020 10:39:47

221 Views

The ord function in python gives the ordinal value of a character(ASCII). You can use this function to find the ascii codes as followsExamples = "Hello" for c in s:    print(ord(c))OutputThis will give the output72 101 108 108 111

Advertisements