Numbers and Math
This page provides syntax for using numbers and mathematic operations in Python. Each section includes an example to demonstrate the described syntax and operations.
Types of Numbers
- Integer (positive and negative counting number) - e.g., -3, -2, -1, 0, 1, 2, and 3: - int- holds signed integers of non-limited length
- long- holds long integers (exists in Python 2.X, depreciated in Python 3.X)
 
- Float (real or floating point numbers) - e.g., -2.14, 0.0, and 3.777 - float
 
- Boolean: (0 = False and 1 = True) - bool
 
Use type() function to determine type
Input:
# Define two variables x and y
x = 100
y = 3.14
# Print out the variable types for each
print(type(x))
print(type(y))Output:
<class 'int'>
<class 'float'>Arithmetic Operators
Addition
x + y
Subtraction
x - y
Multiplication
x * y
Division
x / y
Floor Division
x//y
Power (Exponent)
x ** y
Remainder (Modulo)
x % y
Input:
# Demonstrates different math operations
using f-strings
n1 = 7    # First number
n2 = 3    # Second number
 
# Output results of different math operations
print(f"{n1} + {n2} = {(n1 + n2)}")           # Addition
print(f"{n1} - {n2} = {(n1 - n2)}")           # Subtraction 
print(f"{n1} * {n2} = {(n1 * n2)}")           # Multiplication 
print(f"{n1} / {n2} = {(n1 / n2)}")           # Division 
print(f"{n1} // {n2} = {(n1 // n2)}")         # Floor Division
print(f"{n1} ** {n2} = {(n1 ** n2)}")         # Power/Exponent
print(f"{n1} % {n2} = {(n1 % n2)}")           # Modulo/RemainderOutput:
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2.3333333333333335
7 // 3 = 2
7 ^ 3 = 343
7 % 3 = 1Comparison Operators and Functions
Input:
Equality
x == y or isequal(x, y)
Inequality
x != y or !isequal (x, y)
Less than
x < y
Less than or equal to
x <= y
Greater than
x > y
Greater than or equal to
x >= y
# compare.py
# Demonstrate comparison operators                                                                               
# Assign values to variables using parallel assignment                                                           
c1, c2, c3, c4 = 25, 50, 75, 50
print(f"  c1 = {c1}, c2 = {c2}, c3 = {c3}), c4 = {c4}")
# Output results of different comparison operations                                                             
 
# Testing equality                                                                                               
print(f"c1 = c3 is {(c1 == c3)}")
# Changing values using abbreviated assignment operators                                                        
c1 *= 3    	# Shorthand for c1 = c1 * 3                                                                       
c4 += 1    	# Shorthand for c4 = c4 + 1                                                                       
print(f"c1 = {c1}, c2 = {c2}, c3 = {c3}, c4 = {c4}")
 
# Testing less than and greater than
print(f"  c1 < c2 is {(c1 < c2)}")
print(f"  c4 <= c2 is {(c4 <= c2)}")
print(f"  c1 > c2 is {(c1 > c2)}")
print(f"  c3 >= c2 is {(c3 >= c2)}")Output:
 c1 = 25, c2 = 50, c3 = 75), c4 = 50
c1 = c3 is False
c1 = 75, c2 = 50, c3 = 75, c4 = 51
  c1 < c2 is False
  c4 <= c2 is False
  c1 > c2 is True
  c3 >= c2 is TrueExercises
- Create a Health Calculator Using Python - Forthcoming! 
Resources
- W3 Schools: Python Data Types 
- W3 Schools: Python Arithmetic Operators 
- W3 Schools: Python Numbers 
Last updated
