Exam 1 Review
Jake Shoudy
Sep 12, 2022
CSCI 110 - Review
Announcements
STEP Info Session
Quiz 4 Scores
| Score |
Mean | 67% |
Min | 22% |
Max | 100% |
Median | 70% |
Percent of class above 70% | 50% |
Percent of class above 90% | 12% |
Project 1: Part 3
Update: due Sunday (9/18) at 11:59pm! (No late days allowed!)
Writing functions
PLEASE GET HELP EARLY
HW4
More functions practice. Also writing some tests!
Due 9/14
Reminder: Exam 1
This Wednesday (9/14)
BE ON TIME (exam will start on the hour)
MEET IN LAB
No quiz this week :)
Recap
Base 2 (binary) -> Base 10
Each Byte can be decoded into a human-readable (base 10) number.
11001011
27=128
= (1 x 27) + (1 x 26) + (0 x 25) + (0 x 24) + (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20)
26=64
25=32
24=16
23=8
22=4
21=2
20=1
= 128 + 64 + 0 + 0 + 8 + 0 + 2 + 1
= 203
Base 10 -> Base 2
Each base 10 number can be converted into binary as well!
87
= 64 + 23
= 64 + 16 + 7
= 64 + 16 + 4 + 2 + 1
= 0 + 64 + 0 + 16 + 0 + 4 + 2 + 1
= (0x 27) + (1 x 26) + (0 x 25) + (1 x 24) + (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20)
= 01010111
Data types
Data Type Definition Value Examples
integer (int) number without a decimal point 5, -40, 0, 13382
float number with a decimal point 3.14, 2.0, -9.5
string (str) sequence of characters surrounded by quotes ‘abc’, “Hi!”, ‘3.14’
boolean (bool) True or False True, False
Practice: Data types
“100”
true
0.0
‘’
‘True”
name
str
float
str
not a value!
not a value!
not a value!
False
-1334
bool
int
Arithmetic Operators (for floats and ints)
+ Add�- Subtract�* Multiply�/ Divide�** Exponent
// Floor Division
Round down to nearest whole number
% Modulo (mod)
Remainder
If any operand is float, evaluated value is float
If all operands are integers, evaluated value is integer
EXCEPT DIVIDE / - evaluated value is always float
Order of Operations
Parentheses�Exponents�Multiplication�Division�Addition�Subtraction
1
2
3
4
3) M and D have the same precedence, go left to right. % and // are included in “MD”
4) A and S have the same precedence, go left to right
Practice: Arithmetic Expressions
(3 + 2) * 2 ** 3
5 * 2 ** 3
5 * 8
17 % 6 / 5 * (7 // 2)
17 % 6 / 5 * 3
5 / 5 * 3
40
3.0
=
=
=
=
=
=
String Operators
+ Add
* Multiply
Casting functions
Casting is how we convert between different types.
You cast a variable or value of one type to another type by using the function of the destination type:
Practice: String Expressions
int('2' * 3) + 3
str(42) + 10
int(2.99)
bool(“False”)
bool(-1)
float('1' + '3')
int('7' * 3.0)
float('gg')
225
TypeError
2
True
True
13.0
TypeError
ValueError: could not convert string to float: 'gg'
Comparison Operators
<
<=
==
>
>=
!=
less than
less than or equal to
greater than or equal to
greater than
equal to
not equal to
Logical Operators
Logical operators: combine booleans
Combining Operators
PEMDAS but for boolean expressions…
PNAO!
(Parentheses, not, and, or)
Practice: Combining Operators
False
True
x = 10
y = 3
z = ‘6’
x >= 6 and z == 6
z != ‘x’ or y < 3 and not x < 11
x + y <= x - y or z
True
(z != ‘x’ or y < 3) and not x < 11
False
if statement
if [condition]:� [execute this code]� [execute this code]
If there are two lines of indented code, both lines "belong" to if statement.
if statement
x = 12
if x % 3 == 0:
print(‘trips’)
if x % 4 == 0:
print(‘quads’)
print(‘woo!’)
x % 3 == 0
False
trips
Start
True
woo!
x % 4 == 0
False
quads
True
if/else statement
x = 12
if x % 3 == 0:
print(‘trips’)
else:
print(‘not trips’)
print(‘woo!’)
x % 3 == 0
False
trips
Start
not trips
True
woo!
elif statement
x = 12
if x % 5 == 0:
print(‘quint’)
elif x % 4 == 0:
print(‘quads’)
elif x % 6 == 0:
print(‘hex’)
else:
print(‘nope’)
print(‘woo!’)
x % 5 == 0
True
False
x % 4 == 0
quads
True
Start
quint
False
x % 6 == 0
hex
True
False
nope
woo!
if vs. while
If the lights are on, shout "LIGHTS!".
While the lights are on, shout "LIGHTS!".
If you've clapped your hands less than 5 times, clap your hands 1 time
While you've clapped your hands less than 5 times, clap your hands 1 time
If you like tacos, give me a thumbs up.
While you like tacos, give me a thumbs up.
while loops
Control structure that runs a block of code repeatedly until a certain condition is no longer met
while [condition]:� [execute this code]� [execute this code]
If there are two lines of indented code, both lines "belong" to while statement.
while loop
x = 0
while x < 5:
print(‘small’)
print(‘woo!’)
x < 5
False
small
Start
True
woo!
while loop
x = 0
while x < 5:
print(‘small’)
x = x + 1
x = 0
print(‘woo!’)
x < 5
False
small
x = x + 1
Start
True
woo!
break statement
x = 1
while True:
if x == 5:
break
print("test")
x = x + 1
print("will this print?")
Better: use a good condition
x = 1
while x < 5:
print("test")
x = x + 1
print("will this print?")
Strings
len(s): returns length of string s
string[i]: access individual characters in string for index i
string[start:end]: substring - gets string from start to end, not including end
format: replaces {} with anything
upper/lower: changes string to all upper/lower case letters
strip: removes any leading or trailing spaces from a string
replace: finds instances of the first string and replaces it with the second
escaping: use \ to signify some special meaning
Indexing
Index: position of an element in a sequence
Always start counting from index 0, which means last index is always one less than string length: len(word) - 1.
Go Bulldogs!
0
1
2
3
4
5
6
7
8
9
10
11
Practice: Indexing
school = 'Fisk'
print(school[1])
print(school[len(school) * -1])
print(school[4])
i
F
IndexError
Practice: Slicing
name = 'W.E.B. Du Bois'
print(name[2:8])
print(name[:5])
print(name[-4:])
print(name[5:2])
‘E.B. D’
‘W.E.B’
‘Bois’
‘’
Escaping
\n - newline
\t - tab
\" - quotation mark
\' - apostrophe
\\ - backslash
Practice: String functions
thing = “ dollar$ ”.strip().upper()
print("I get {} {} when I {} {}".format(3, thing, False, "start"))
I get 3 DOLLAR$ when I False start
print("\"All quotes found on the Internet are true\"\n\t- Abraham Lincoln")
"All quotes found on the Internet are true"
- Abraham Lincoln
Strings and while loops!
word = input(“Give me a word: ”)
index = 0
count = 0
while index < len(word):
if word[index] == ‘a’:
count += 1
index += 1
print(count)
Give me a word: banana
3
Import Statement
import math
print(math.ceil(3.4))
from math import ceil
print(ceil(3.4))
OR
Flip a Coin
from random import randint
number = randint(1,2)
if number == 1:
print('Heads')
else:
print('Tails')
Function: Defining A Function
Define a function to reuse again and again using def
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Function: Defining A Function
Name the function
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Function: Defining A Function
Name the parameter(s) to do the task
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Return Value
Function can have multiple return statements but only one will ever run
def my_function(argument1, argument2):
if some statement is True:
return True
else:
return False
Variables vs Functions
Variables memorize data, give name to values
Functions memorize code, give name to a block of code
Argument vs Parameter
argument: value provided as input to the function in function call
parameter: named variable representing input in the function definition
result = abs(-8)
other_result = abs(2)
def abs(num):
# code
argument
parameter
Passing arguments
result = my_function(5, 3)
other = my_function(-8, 2)
print(result)
def my_function(a, b):
if a > b:
print(‘yo’)
return a * 2
else:
return a // 2
print(‘woah’)
print(‘dough’)
a = 5, b = 3
result = 10
yo
Passing arguments
result = my_function(5, 3)
other = my_function(-8, 2)
print(result)
def my_function(a, b):
if a > b:
print(‘yo’)
return a * 2
else:
return a // 2
print(‘woah’)
print(‘dough’)
a = -8, b = 2
result = 10
other = -4
yo
10
Common Confusions
call != define
call != print
return != print
print is just a function
print can still happen inside functions!
every function has a return value (or None)
Questions?