Numbers

Adding, Subtracting, Multiplication, Division and More!

Types of Numbers

Integers: Numbers without a decimal part.

5, 10, -2, 0, -210

Floats: Numbers with a decimal point

1.0, 1.2, 1.2345, -3.45, -3.0, 5.0001

Integer Operations

You can perform all the basic operations in Python


                    #Addition
                    2+3
                    #Subtraction
                    3-2
                    #Multiplication
                    3*2
                    #Division
                    3/2
                

                    5
                    1
                    6
                    1.5
                

More Integer Operations

You can also use two asterisks (*) to represent exponents and parenthesis to group terms.


                #Exponentiation - 3^2
                3**2
                #Exponentiation - 3^3
                3**3
                #Order of Operations
                2+3*4
                #Grouping
                (2+3)*4
            

                9
                27
                14
                20
            

Operations with Floats

Adding, Multiplying, Subtracting and Dividing Floating Point Numbers behave like you would expect.


              #Addition
              0.1+0.1
              0.2+0.2
              #Multiplication
              2*0.1
              2*0.2
          

              0.2
              0.4
              0.2
              0.4
          

Operations with Floats

You might sometimes get a lot of decimal places and a random number at the end


              #Addition
              0.2+0.1
              #Multiplication
              3*0.1
          

        0.30000000000000004
        0.30000000000000004
          

Mixing Integers and Floats

Performing operations with integers and floats gives a varying type of results.


              #Division - Int / Int
              4/2
              #Addition - Int + float
              1+2.0
              #Multiplication - Int * float
              2*3.0
              #Exponentiation - float^Int
              3.0**2
          

        2.0
        3.0
        6.0
        9.0
          

Summary

Here is a quick rundown of the expected result of math operations

  • Int + Int = Int
  • Int*Int = Int
  • Int/Int = Float
  • Int*Float = Float
  • Float**Int = Float

Working with Number Types

In Python you can assign the value of a number to a variable.

You can even separate place values with an underscore to make the numbers easier to read!


              #You can use underscores to separate place values.
              universe_age = 14_000_000_000
              print(universe_age)
                    

              14000000000
                    

Multiple Assignment

You can assign multiple values to variables in one line to make your code shorter. Usually you initialize variables that are related.


              # Assign multiple values in a line
              x,y,z = 1,2,3
              print(x)
              print(y)
              print(z)
                    

              1
              2
              3
            

Constants

Constants are value that do not change during the entire program. We want to have the habit of using variable names in ALL CAPS.


              # Define a constant in ALL CAPS
              PI = 3.141592
              print(PI)
            

              3.141592