01-Numbers
01-Numbers
___
Content Copyright by Pierian Data
1
Now let’s start with some basic arithmetic.
[1]: 3
[2]: # Subtraction
2-1
[2]: 1
[3]: # Multiplication
2*2
[3]: 4
[4]: # Division
3/2
[4]: 1.5
[5]: 1
Whoa! What just happened? Last time I checked, 7 divided by 4 equals 1.75 not 1!
The reason we get this result is because we are using “floor” division. The // operator (two forward
slashes) truncates the decimal without rounding, and returns an integer result.
So what if we just want the remainder after division?
[6]: # Modulo
7%4
[6]: 3
4 goes into 7 once, with a remainder of 3. The % operator returns the remainder after division.
[7]: 8
2
[8]: # Can also do roots this way
4**0.5
[8]: 2.0
[9]: 105
[10]: 156
[12]: 10
[14]: # Check
a
[14]: 10
Yes! Python allows you to write over assigned variable names. We can also use the variables
themselves when doing the reassignment. Here is an example of what I mean:
[15]: # Check
a
3
[15]: 10
[17]: # Check
a
[17]: 20
The names you use when creating these labels need to follow a few rules:
1. Names can not start with a number.
2. There can be no spaces in the name, use _ instead.
3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
4. It's considered best practice (PEP8) that names are lowercase.
5. Avoid using the characters 'l' (lowercase letter el), 'O' (uppercase letter oh),
or 'I' (uppercase letter eye) as single character variable names.
6. Avoid using words that have special meaning in Python like "list" and "str"
Using variable names can be a very useful way to keep track of different variables in Python. For
example:
[18]: # Use object names to keep better track of what's going on in your code!
my_income = 100
tax_rate = 0.1
my_taxes = my_income*tax_rate
[19]: 10.0
So what have we learned? We learned some of the basics of numbers in Python. We also learned
how to do arithmetic and use Python as a basic calculator. We then wrapped it up with learning
about Variable Assignment in Python.
Up next we’ll learn about Strings!