Class Notes (1,100,000)
CA (620,000)
UTSC (30,000)
All (10)
Lecture 3

CSCA08 Lecture 3: CSC108 - Week 3 Notes

Computer Science
Course Code

This preview shows page 1. to view the full 5 pages of the document.
Alexander Magony
CSC108H1F 2014
Week 3
Functions, Variables, and Call Stacks
-Visualizes calling functions in other functions and how multiple stack frames store similar
variable and stuff. Again, it’s much easier to watch this video.
Type bool
-boolean values: “true” or “false”. States whether certain conditions are true. The values True
and False are type bool (as opposed to
-Comparison operators: states true or false if the comparison is true or false.
Less than ( < ): Eg. 3 < 4 returns True
Greater than ( > ): Eg. 3 > 4 returns False
Equal ( == ): Eg. 3 = = 1 * 3 returns True since both sides are equal. 7 == 7.0 is also True.
Greater than or equal to ( >= ): Eg. 3 >= 3 is True since they are equal in this case.
Less than or equal to ( <= ): Eg. 4 <= 3 is False since 4 is greater than 3.
Not equal to ( != ): Eg. 3 != 5 is True since 3 is not equal to 5.
Of course, variables and functions can be used in comparisons as well.
-Logical operators: can be used with Boolean expressions, like the comparison operators.
not: returns True if the expression is NOT true. This can be used multiple times in a row
(eg. not not (3 == 3) which is the same as saying 3 == 3). Eg. not (3 > 4) is True because 3
> 4 on its own is not true.
and: returns True if all expressions are true. Can compare two or more expressions. Eg. (3
> 2) and (2 == 2.0) is True because both expressions are true.
or: returns True if at least one of the expressions are true. Can compare two of more
expressions. Eg. (3 > 2) or (2 < 1) or (2 ** 2 == 1) is True because at least one of the
expressions are true.
Again, variables and function calls can be compared. Eg. class_grade > 50 or class_grade
== mean_grade(70, 80, 90) can be evaluated (the result depends obviously on the variable
and the function).
Operators can be combined. In these cases we have to be careful about the order.
eg. not 80 >= 50 or 90 >= 50 is True as it is equivalent to (not 80 >= 50) or (90 >= 50).
-This is different from not (80 >= 50 or 90 >= 50) which is false.
-As we can see, not is applied first, before or.
-The operator precedence (highest to lowest) is: not, and, or.
-Use parentheses to ensure the order of operations occurs in the order you like or
just for clarity. Eg. ( 4 != 4) or ( 2 > 3) is clearer than 4 != 4 or 2 > 3.
Converting between int, str, and float
-The types we have learned so far are int, str, float, and bool.
We can convert between the first three using built-in functions.
You're Reading a Preview

Unlock to view full version

Only page 1 are available for preview. Some parts have been intentionally blurred.

Alexander Magony
CSC108H1F 2014
-str(number) can be used to convert a number to a string.
eg. str(3) returns ‘3’ as in a string, not a number. If multiplied by 2, you would get ’33’, not
-int(string or number) can be used to convert a string (containing only digits) or a number
into an integer.
eg. int(‘3’ * 5) returns 33333.
eg. int(4.65) returns 4.
-float(string or number) can convert a string (containing only digits) or a number into a float.
eg. float(‘465’) returns 465.0.
eg. float(24) returns 24.0.
-if float or int functions are used to convert a string containing characters other than numeric
digits, a ValueError is returned since Python is unable to do the conversion.
-Using this with inputs:
eg. input(“Enter the number of shoes: “ and the user responds ‘863’, a string (which needs
to be converted to a number, in this case an int, to be compared to other numbers).
If num_shoes_left = 627, we can make num_shoes_wanted = int(input(“Enter the number
of shoes: “)) and then compare the variables to see if we have enough shoes. In this case,
num_shoes_left >= num_shoes_wanted would return False.
If statements
-We can control when our code runs based on boolean expressions.
-if statements: only execute if the boolean expression is True.
Generally written in this form:
if expression:
-the statements are only executed if the expression is True. If none of the if statements are
fulfilled, nothing is returned. If we print this, it shows that the value None is printed. In most
cases, we want something else to be returned if the if statement is false.
-elif statement: must follow an if statement. If the first if statement is False, then this
expression is evaluated. If THIS statement is True, the expressions underneath are run.
elif expression:
-else statement: if none of the preceding statements are True, run the below expressions.
This covers all of the rest of the options, ensuring that None is never returned.
else expression:
-A few rules: there can be 0 or more elif clauses.
You're Reading a Preview

Unlock to view full version