*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.
Arithmetic Operators
Python Arithmetic Operators are pretty standard. Take note of the division problem in line [9]. 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.xxx
Python 3 handles division differently. To get integer numbers only in division, use //.
10//7 1
10/7 1.4285714285714286
Arithmetic Operators
- + : Addition
- – : Subtraction
- * : Multiplication
- / : Division
- % : Modulus (returns the remainder of a division problem: 5%2=1)
- ** : Exponent (4**2 = 16)
Variables
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
Python 3.xxx
In Python 3, print statements require ().
print (A) print (B)
You can perform arithmetic functions on variables
Python 3.xxx
print (C)
And of course, variables can hold strings as well as numbers
Python 3.xxx
print (E) print (F)