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

George John
Updated on 24-Mar-2026 20:43:34

2K+ Views

To check if a float value is a whole number in Python, you can use several methods. The most straightforward approach is using the is_integer() method available for float objects. Using the is_integer() Method The is_integer() method returns True if the float value represents a whole number, otherwise False − print((10.0).is_integer()) print((15.23).is_integer()) print((-5.0).is_integer()) print((0.0).is_integer()) True False True True Using Modulo Operation You can also check if a float is a whole number by using the modulo operator to see if there's no remainder when divided by 1 − ... Read More

How to compare string and number in Python?

Nikitha N
Updated on 24-Mar-2026 20:43:20

3K+ Views

In Python, you cannot directly compare strings and numbers using comparison operators like , or == because they are different data types. However, there are several approaches to handle string-number comparisons depending on your needs. Direct Comparison Behavior Attempting to compare a string and number directly will raise a TypeError in Python 3 − # This will raise a TypeError try: result = "12" < 100 print(result) except TypeError as e: print(f"Error: {e}") Error: '

How to compare numbers in Python?

Samual Sam
Updated on 24-Mar-2026 20:43:01

17K+ Views

You can use relational operators in Python to compare numbers (both float and int). These operators compare the values on either side of them and decide the relation among them. Comparison Operators Python provides six main comparison operators for comparing numbers ? Operator Description Example (a=10, b=20) Result == Equal to (a == b) False != Not equal to (a != b) True > Greater than (a > b) False = Greater than or equal to (a >= b) False , =,

How to Find Hash of File using Python?

Arjun Thakur
Updated on 24-Mar-2026 20:42:44

3K+ Views

You can find the hash of a file using Python's hashlib library. Since files can be very large, it's best to use a buffer to read chunks and process them incrementally to calculate the file hash efficiently. Basic File Hashing Example Here's how to calculate MD5 and SHA1 hashes of a file using buffered reading − 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 ... Read More

How to Find ASCII Value of Character using Python?

Chandu yadav
Updated on 24-Mar-2026 20:42:19

302 Views

The ord() function in Python returns the ASCII (ordinal) value of a character. This is useful for character encoding, sorting operations, and working with character data. Syntax ord(character) Parameters: character − A single Unicode character Return Value: Returns an integer representing the ASCII/Unicode code point of the character. Finding ASCII Value of a Single Character char = 'A' ascii_value = ord(char) print(f"ASCII value of '{char}' is {ascii_value}") ASCII value of 'A' is 65 Finding ASCII Values of Multiple Characters You can iterate through ... Read More

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

karthikeya Boyini
Updated on 24-Mar-2026 20:42:05

1K+ Views

Python provides built-in functions to convert decimal numbers to different number systems. These functions are bin() for binary, oct() for octal, and hex() for hexadecimal conversion. Syntax bin(number) # Returns binary representation oct(number) # Returns octal representation hex(number) # Returns hexadecimal representation Basic Conversion Example Here's how to convert a decimal number to different number systems ? decimal = 27 print(bin(decimal), "in binary.") print(oct(decimal), "in octal.") print(hex(decimal), "in hexadecimal.") 0b11011 in binary. 0o33 in octal. 0x1b in ... Read More

How to generate statistical graphs using Python?

Ankitha Reddy
Updated on 24-Mar-2026 20:41:49

508 Views

Python has an amazing graph plotting library called matplotlib. It is the most popular graphing and data visualization module for Python. You can start plotting graphs using just 3 lines of code! Basic Line Plot Here's how to create a simple line plot ? from matplotlib import pyplot as plt # Plot coordinates (1, 4), (2, 5), (3, 1) plt.plot([1, 2, 3], [4, 5, 1]) # Display the plot plt.show() Adding Labels and Title You can enhance your plot with descriptive labels and a title ? from matplotlib import ... Read More

How to generate a random 128 bit strings using Python?

Abhinaya
Updated on 24-Mar-2026 20:41:31

3K+ Views

You can generate random 128-bit strings using Python's random module. The getrandbits() function accepts the number of bits as an argument and returns a random integer with that many bits. Using random.getrandbits() The getrandbits() method generates a random integer with exactly 128 bits ? import random hash_value = random.getrandbits(128) print(hex(hash_value)) The output of the above code is ? 0xa3fa6d97f4807e145b37451fc344e58c Converting to Different Formats You can format the 128-bit random number in various ways ? import random # Generate 128-bit random number random_bits = random.getrandbits(128) ... Read More

How to find time difference using Python?

Ankith Reddy
Updated on 24-Mar-2026 20:41:09

2K+ Views

Finding time differences in Python is straightforward using the datetime module and timedelta objects. A timedelta represents a duration — the difference between two dates or times. Understanding timedelta The timedelta constructor accepts various time units as optional parameters ? import datetime # Creating a timedelta object delta = datetime.timedelta(days=7, hours=2, minutes=30, seconds=45) print(f"Duration: {delta}") print(f"Total seconds: {delta.total_seconds()}") Duration: 7 days, 2:30:45 Total seconds: 612645.0 Subtracting Time from Current Date You can subtract a timedelta from a datetime to get an earlier time ? import datetime ... Read More

How to generate XML using Python?

Abhinaya
Updated on 24-Mar-2026 20:40:48

2K+ Views

Python provides multiple ways to generate XML documents. The most common approaches are using the dicttoxml package to convert dictionaries to XML, or using Python's built-in xml.etree.ElementTree module for more control. Method 1: Using dicttoxml Package First, install the dicttoxml package ? $ pip install dicttoxml Basic XML Generation Convert a dictionary to XML using the dicttoxml method ? import dicttoxml data = { 'foo': 45, 'bar': { 'baz': "Hello" ... Read More

Advertisements