Python Fundamentals: Integers and Floats


Python Integers and Floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place.

a. Creating an integer and a float: Integers and floats in Python can be created by assigning a number to a variable.

Code

int1 = 10
float1 = 20.5
print(int1)
print(float1)

Output

10
20.5

b. Arithmetic operations: Python includes the following arithmetic operations - addition, subtraction, multiplication, division, floor division, modulus, and exponentiation.

Code

x = 10
y = 3

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y)
print(x % y)
print(x ** y)

Output

13
7
30
3.3333333333333335
3
1
1000

c. Comparison operations: Python includes the following comparison operations - equals, not equals, greater than, less than, greater than or equal to, less than or equal to.

Code

x = 10
y = 20

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)

Output

False
True
False
True
False
True

d. Converting between integer and float: Python includes the int() and float() functions to convert between integers and floats.

Code

x = 10.5
y = 20

print(int(x))
print(float(y))

Output

10
20.0

Enquiries

[email protected]

Copyright © 2023 - slash-root.com