CSC108 - Week 1 Notes
CSC108 - Week 1 Notes
CSC108H1F 2014
Week 1
Python as a Calculator
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.
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.
Alexander Magony
CSC108H1F 2014
- 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.
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
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.
Alexander Magony
CSC108H1F 2014
Defining Functions
- As well as using built-in functions, we can define our own
eg:
def f(x):
return x ** 2
- Function definition:
def functionname(parameters):
body
notice the body is indented.