You are on page 1of 4

Alexander Magony

CSC108H1F 2014

Week 1
Python as a Calculator

Addition: eg. 2 + 3 returns 5.


Subtraction: eg. 2 - 1 returns 1
Multiplication: eg. 3 * 4 returns 12
Exponentiation: eg. 2 ** 3 returns 8

- Division: eg. 4 / 2 = 2.0


Notice the decimal value. Dividing like this will create a float type. Adding,

subtracting, and multiplying integers (of type int) will return values of type int.
However, dividing like this will always return a float, even if there is no remainder.
eg. 5 / 2 returns 2.5
Floating point numbers are approximations of real numbers and may return
inaccurate results (eg. 7 / 3 returns 2.3333333333333335)
Integer division: eg. 4 // 2 returns 2
eg. 2 // 3 returns 0
eg. 5/3 returns 1
We get back a number of type int. We only get the whole number back. 2 divides
into 3 less than once, so we get back 0.
Modulo (remainder): eg. 4 % 2 = 0 (verbalized 4 mod 2)
eg. 2 % 3 returns 2. Since 2 does not divide into 3, there is 2 remainder.
eg. 5 % 3 = returns. Since 5 divides once into 3, there is a remainder of 2 that
remains undivided.

- Order of Operations: generally left to right. However, the following operations take
precedence over lower operations, from highest to lowest.

** (highest precedence). eg. 3 + 2 ** 3 returns 11. The 2 ** 3 is calculated first.


- (negation). eg. - 10 * 2 returns - 20. The negative is applied to the 10 before

being multiplied by 2.
*, /, //, %. eg. 4 + 5 * 3 returns 19. Multiplication before addition.
+ and - (lowest precedence). 3 + 4 - 5 returns 2. When only addition and
subtraction left, is calculated left to right.
eg. -10 * 3 + 5 ** 3 returns 95. First 5 ** 3, then -10 * 3, then -30 + 125.

- Syntax: rules that describe valid combo of Python symbols.


eg. 3 + returns a SyntaxError. So does ** . And 4 + 3 ) - 5 .
- However, if you enter (4 + 5 - 3 , python will wait for you to write a closing
bracket since commands can span multiple lines.

- Semantics: meaning of an expression. Eg. 2 + 3 means 2 is added by 3.


3 / 0 is correct in terms of Syntax but produces a Semantic Error since nothing can
be divided by 0.

Alexander Magony

CSC108H1F 2014

Python and Computer Memory


- all used values are stored in the computer memory and referred to by a memory address.
- variable: named location in computer size.
eg. variable shoe_size may store memory address x34 which refers to the value 8.5, the
value stored at memory address x34. Every new value is stored at a new memory address.

- Some terminology:
a value has a memory address.
a variable contains a memory address. A variable refers/points to a value.
With out example:
- Value 8.5 has memory address x34. Variable shoe_size contains memory address x34.
Value of shoe_size is 8.5. shoe_size refers/points to value 8.5

Variables
- Assignment statements can assign values to variables.
eg. base = 20. Now entering the variable base will return 20.
- 20 has a memory address (eg. x3). This memory address is now contained in base.
You can then enter height = 12.
In programming, the equals symbol is different than regular mathematics.
You can now enter an expression like base * height / 2 and get 120.0. You can assign
this value to a variable by entering area = base * height / 2. Now area refers to 120.0.
- If you then enter base = 2.5, the x3 memory address is replaced by a new memory
address (eg. x4) which refers to the new value of 2.5. Then enter height = 7 and a
similar reassignment occurs.
- If you now reenter area = base + height / 2, area will refer to 8.75 since base and area
now point to new values contained by new memory addresses.
However, if you simply evaluate area before this, it will still refer to the old value as it
still contains the old memory address.

- For executing an assignment statement:


1. Evaluate the expression on the right of the = sign to produce a value with a memory
address.

2. Store the memory address of the value in the variable on the left of the =.
- Variable rules:
legal Python names start with a letter or _ and must only contain letters, _, or digits.
- Variables cannot start with a number, it produces a SyntaxError.
- If you include an illegal character (eg. @), SyntaxError.
- Variables are case sensitive so sUm is a different variable than sum.
Conventionally, variables are generally written in lower case and words are separated by
_. This is called pothole_case.

Alexander Magony

CSC108H1F 2014

Visualizing Assignment Statements


- The prof goes through visualizing assignment statements. Easier to watch in the video. Some
important points:
If you execute:
x=1
y=x+2
x=7
y will refer to 3 by the end of it, not 9. x will now contain the new memory address which
refers to 7, but y will still refer to 3.
VIDEO: https://teach.cdf.toronto.edu/StG108/content/challenges/60/1

Built-In Functions
- Many built-in functions in Python that allow us to perform different operations.
- max(multiple numbers separated by commas): returns the largest value.
max(3,2) returns 2.
- In this example, we are passing (to provide to a function) two arguments (a value given
to a function) to the function. Python will evaluate this function call (ask Python to
evaluate a function).
Mousing over the operation will give you details as to what sort of values should be entered
or return.
Can use more arguments of type int or float.
eg. max(3, 4 + 5) returns 9, the evaluated value of the second argument.

- Form of a function call: function_name(arguments).


1. Evaluate the arguments
2. Call the function, passing in the argument values.
- all built-in functions can be listed by entering dir(__builtins__)
- descriptions of each function can be found by entering help(example_function). eg.
help(max).

- abs(number) -> number


returns absolute value of argument.
eg. abs(-45.34) returns 45.34
eg. abs(34) returns 34.
So floats are returned as floats and ints as ints.
- pow(x, y[, z]) -> number
Two arguments needed, third optional argument. With two arguments, x ** y is evaluated.
With three, (x ** y) % z

eg. pow(2, 5) returns 32, like 2 ** 5.


3

Alexander Magony

CSC108H1F 2014

- round(number[, ndigits]) -> number


Rounds the first argument to the amount of digits provided by the second argument. If
there is no second argument, round to the closest int.

eg. round(2.5) returns 2


eg. round(3.5) returns 4.
- If rounding a .5, round to the nearest even number.
eg. round(0.06323, 1) returns 0.1
eg. round(323.3, -1) returns 320.

Defining Functions
- As well as using built-in functions, we can define our own
eg:
def f(x):
return x ** 2

x is a parameter of the function.


def indicates that we are defining a function
f is the name of the function.
return indicates that we are passing back a value.

- Function definition:
def functionname(parameters):
body
notice the body is indented.

- f(3) of our example would return 9.


- Can store results in variables (eg. result = f(3)). So after the value of the right side of the =
sign is evaluated, the memory address of the value is stored in the variable. So results gets
the memory address of the call to f(3). So result gets 9.

You might also like