🔢 Integers and Floats¶
1. Definition and Initialization¶
Integers¶
Integers in Python are whole numbers, positive or negative, without decimals, of unlimited length.
In [1]:
Copied!
x = 1
y = -3255522
z = 35656222554887711
print(x, y, z)
x = 1
y = -3255522
z = 35656222554887711
print(x, y, z)
1 -3255522 35656222554887711
Floats¶
Floats in Python are real numbers, containing one or more decimals.
In [2]:
Copied!
x = 1.10
y = -35.59
z = 35e3 # using "e" to denote an exponential number
print(x, y, z)
x = 1.10
y = -35.59
z = 35e3 # using "e" to denote an exponential number
print(x, y, z)
1.1 -35.59 35000.0
2. Arithmetic Operations¶
You can perform basic arithmetic operations on integers and floats, such as addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**).
In [3]:
Copied!
x = 5
y = 2
print(x + y) # prints: 7
print(x - y) # prints: 3
print(x * y) # prints: 10
print(x / y) # prints: 2.5
print(x // y) # prints: 2 (floored quotient of x and y)
print(x % y) # prints: 1 (remainder of x / y)
print(x ** y) # prints: 25 (x to the power y)
x = 5
y = 2
print(x + y) # prints: 7
print(x - y) # prints: 3
print(x * y) # prints: 10
print(x / y) # prints: 2.5
print(x // y) # prints: 2 (floored quotient of x and y)
print(x % y) # prints: 1 (remainder of x / y)
print(x ** y) # prints: 25 (x to the power y)
7 3 10 2.5 2 1 25
3. Comparisons¶
You can compare integers and floats using comparison operators: equals (==), not equal (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
In [4]:
Copied!
x = 5
y = 2
print(x == y) # prints: False
print(x != y) # prints: True
print(x > y) # prints: True
print(x < y) # prints: False
print(x >= y) # prints: True
print(x <= y) # prints: False
x = 5
y = 2
print(x == y) # prints: False
print(x != y) # prints: True
print(x > y) # prints: True
print(x < y) # prints: False
print(x >= y) # prints: True
print(x <= y) # prints: False
False True True False True False
4. Type Conversion¶
You can convert between integers and floats using the int()
and float()
functions.
In [5]:
Copied!
x = 1.5
print(int(x)) # prints: 1
print(float(x)) # prints: 1.5
x = 1.5
print(int(x)) # prints: 1
print(float(x)) # prints: 1.5
1 1.5
5. Math Functions¶
Python includes a math module that provides mathematical functions. To use it, you first need to import it.
In [6]:
Copied!
import math
x = 2
y = 3
print(math.sqrt(x)) # prints: 1.4142135623730951 (square root)
print(math.pow(x, y)) # prints: 8.0 (power)
print(math.pi) # prints: 3.141592653589793 (pi constant)
import math
x = 2
y = 3
print(math.sqrt(x)) # prints: 1.4142135623730951 (square root)
print(math.pow(x, y)) # prints: 8.0 (power)
print(math.pi) # prints: 3.141592653589793 (pi constant)
1.4142135623730951 8.0 3.141592653589793