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:

Arithmetic Operators

Operator
Example

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:

Output:

Comparison Operators and Functions

Input:

Operator
Example

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

Output:

Exercises

  • Create a Health Calculator Using Python - Forthcoming!

Resources

Last updated