The following is part of a on-going collection of Jupyter notebooks. The goal being to have a library of notebooks as an introduction to Mathematics and technology. These were all created by Gavin Waters. If you use these notebooks, be nice and throw up a credit somewhere on your page.
First, we want to start out by saying that numbers are not always stored as "real" numbers. If you dont specify that a whole number is a real( floating point ) then python assumes that is is an integer
3.0 + 2
5.0
3+2
5
print(2+3)
print(2-3)
print(2/3)
print(2**3)
5 -1 0.6666666666666666 8
Please notice that $2^3$ is expressly written as "**" not "^"
3^4 #This is an XOR statement in binary, "XOR: one or the other but not both"
7
011 = 3 in binary that becomes $0*2^2 + 1*2^1 +1*2^0$
100 = 4 in binary that becomes $1*2^2 + 0*2^1 +0*2^0$
2^3 # This may lead to quite a bit of confusion.
1
011 = 3 in binary that becomes $0*2^2 + 1*2^1 +1*2^0$
010 = 2 in binary that becomes $0*2^2 + 1*2^1 +0*2^0$
The quotient function is built into python using the "//" command
print(13/4)
print(13//4)
3.25 3
Also we can check whether or not two numbers are different from each other. This is done by boolean logic, we make a statement and then python outputs "True" or "False"
print(4>3)
print(4<3)
print(4<=3)
print(4>=3)
print(4!=3)
print(4==3)
True False False True True False
These inequalities can also work for strings. In the sense that the letters have a natrual order and we can treat them as numbers in a 26-base system.
print("abc">="a")
print("abc">="ab")
print("abc">="abc")
print("abc">="abe")
print("abc">="ac")
print("This is a very long statement, but just a single letter is more than it">="u")
True True True False False False
This is a great way to check if two strings are equal.
print("is this equal"=="is this equal")
print("IS THIS EQUAL"=="is this equal")
True False
Can you guess what the operator "%" does from the following examples?
print(0%4)
print(1%4)
print(2%4)
print(3%4)
print(4%4)
print(5%4)
print(6%4)
print(7%4)
print(8%4)
0 1 2 3 0 1 2 3 0
Hint, its the remainder function
You can use python shorthand to do quick operations on variables. For example, if you have a variable and want to just add a number to that variable. you would use....
x = 2
print(x)
x +=5
print(x)
2 7
Remember that this alters your variable, so use cautiously. Also, this is an operation, so you cannot print this.
x = 2
print(x +=3)
File "<ipython-input-12-6218ede57f0a>", line 2 print(x +=3) ^ SyntaxError: invalid syntax
Lets have a look at some of these quick steps
x = 2
x +=3
print(x)
x -=2
print(x)
x /=4
print(x)
x **=2
print(x)
5 3 0.75 0.5625
x="now I know"
print(x)
x+=" something new "
print(x)
now I know now I know something new
x *=3
print(x)
now I know something new now I know something new now I know something new