Found 10784 Articles for Python

How to check if a float value is a whole number in Python?

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

1K+ 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

14K+ 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

2K+ 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

134 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

How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?

karthikeya Boyini
Updated on 05-Mar-2020 10:38:46

955 Views

Python provides straightforward functions to convert Decimal to Binary, Octal, and Hexadecimal. These functions are −Binary: bin() Octal: oct() Hexadecimal: hex()ExampleYou can use these functions as follows to get the corresponding representation −decimal = 27 print(bin(decimal),"in binary.") print(oct(decimal),"in octal.") print(hex(decimal),"in hexadecimal.")OutputThis will give the output −0b11011 in binary. 0o33 in octal. 0x1b in hexadecimal.

How to Display the multiplication Table using Python?

Ankith Reddy
Updated on 05-Mar-2020 10:36:28

306 Views

You can create the multiplication table for any number using a simple loop. exampledef print_mul_table(num):    for i in range(1, 11):       print("{:d} X {:d} = {:d}".format(num, i, num * i)) print_mul_table(5)OutputThis will give the output5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 5 X 10 = 50

How to Find the Largest Among Three Numbers using Python?

Samual Sam
Updated on 05-Mar-2020 10:35:19

902 Views

You can create a list of the three numbers and call the max method to find the largest among them. examplemy_list = [10, 12, 3] print(max(my_list))OutputThis will give the output −12ExampleIf you want to calculate it yourself, you can create a simple function likedef max_of_three(a, b, c):    if a > b and a > c:       return a    elif b > c:       return b    else:       return c print(max_of_three(10, 12, 3))OutputThis will give the output −12

How to print current date and time using Python?

Abhinanda Shri
Updated on 05-Mar-2020 10:31:00

5K+ Views

You can get the current date and time using multiple ways. The easiest way is to use the datetime module. It has a function, now, that gives the current date and time. exampleimport datetime now = datetime.datetime.now() print("Current date and time: ") print(str(now))OutputThis will give the output −2017-12-29 11:24:48.042720You can also get the formatted date and time using strftime function. It accepts a format string that you can use to get your desired output. Following are the directives supported by it.DirectiveMeaning%aLocale's abbreviated weekday name.%ALocale's full weekday name.%bLocale's abbreviated month name.%BLocale's full month name.%cLocale's appropriate date and time representation.%dDay of the month ... Read More

How to Solve Quadratic Equation using Python?

George John
Updated on 05-Mar-2020 10:28:41

308 Views

You can use the cmath module in order to solve Quadratic Equation using Python. This is because roots of quadratic equations might be complex in nature. If you have a quadratic equation of the form ax^2 + bx + c = 0, then,Exampleimport cmatha = 12 b = 8 c = 1 # Discriminent d = (b**2) - (4*a*c) root1 = (-b - cmath.sqrt(d)) / (2 * a) root2 = (-b + cmath.sqrt(d)) / (2 * a) print(root1) print(root2)OutputThis will give the output(-0.5+0j) (-0.16666666666666666+0j)

Advertisements