Python Integers and Floats

Python allows us to work with integers (type int) and floats (type float), two fundamental data types discussed in a previous blog post about Python Data Types.

Python Integers and floats are numerical representations, with the main difference being a floating number has a decimal point, and an integer does not.

The Python programming language will follow the math rule of PEMDAS. It will perform math functions by looking at the following:

P – Parenthesis
E – Exponent
M – Multiplication
D – Division (floor or integer)
A – Addition
S – Subtraction

Python can be used as a basic calculator. From your programs or the Python interpreter, you can treat it like a calculator using the following operators

  • + addition
    • This will add the two values (either integers or floats) together
  • - subtraction
    • This will subtract the two values (either integers or floats) together
  • * multiplication
    • This will multiply the two values (either integers or floats) together
  • / division, aka floating-point division
    • This will divide the two values (either integers or floats) together
    • This will also return any remainder if there are any,
    • 10/4 # Returns: 2.5
    • There is a concept called floor division (or integer) division in Python, which will not return the remainder, only the whole integer.
      • This is represented with //
      • Example:
        • 10//4 # Returns: 2
  • ** To the power of
    • This will raise the integer or float to the power of
    • You can square a number
      • 4**.5 # Returns: 2.0
    • or raise a number to a power of
    • 4**2 #Returns: 16
  • % this is modulo, and it will return the remainder after division
    • You will see this if you use Python if statements to check if a number is divisible by another number.
    • An example would be you want to see every number that 4 can go into evenly.

Link on floating-point numbers: https://docs.python.org/3/tutorial/floatingpoint.html.