*Note: This lesson was written using Python 2.xx. If you are using Python 3.xxx any changes to the code will be annotated under headings: Python 3.xxx
Open up a new Jupyter Notebook. Remember, when working with Jupyter Notebooks, you need to hit Shift+Enter to execute your code.
Python Arithmetic Operators are pretty standard. Take note of the division problem in line . 10/7 is not 1, but by default Python only shows integer answers
You can work around it by defining the first number as a float()
Python 3 handles division differently. To get integer numbers only in division, use //.
- + : Addition
- – : Subtraction
- * : Multiplication
- / : Division
- % : Modulus (returns the remainder of a division problem: 5%2=1)
- ** : Exponent (4**2 = 16)
Variables in Python are pretty straight forward. Unlike other programming languages, you do not need to define the variables first. Python dynamically assigns the data type.
Three main rules:
- Variables must start with a letter or _
- Variables are case sensitive
- Avoid using command keywords (print, def, for)
notice lowercase ‘a‘ returns an error
Remember, Jupyter notebooks only return the last command. If you want both variables, use the print command
In Python 3, print statements require ().
print (A) print (B)
You can perform arithmetic functions on variables
And of course, variables can hold strings as well as numbers
print (E) print (F)