1 of 51

Exam 1 Review

Jake Shoudy

Sep 12, 2022

CSCI 110 - Review

2 of 51

Announcements

3 of 51

STEP Info Session

  • Tomorrow!
  • Please RSVP
  • Location TBD
  • 5-6:30pm
  • FREE FOOD
  • Yes this is the night before the midterm:
    • Come take a study break?
  • STEP application opens September 19th
  • Next event:
    • September 20th
    • Darian will be on campus!

4 of 51

Quiz 4 Scores

Score

Mean

67%

Min

22%

Max

100%

Median

70%

Percent of class above 70%

50%

Percent of class above 90%

12%

5 of 51

Project 1: Part 3

Update: due Sunday (9/18) at 11:59pm! (No late days allowed!)

Writing functions

PLEASE GET HELP EARLY

6 of 51

HW4

More functions practice. Also writing some tests!

Due 9/14

7 of 51

Reminder: Exam 1

This Wednesday (9/14)

BE ON TIME (exam will start on the hour)

MEET IN LAB

No quiz this week :)

8 of 51

Recap

9 of 51

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

10 of 51

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

11 of 51

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

12 of 51

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

13 of 51

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

14 of 51

Order of Operations

ParenthesesExponentsMultiplicationDivisionAdditionSubtraction

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

15 of 51

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

=

=

=

=

=

=

16 of 51

String Operators

+ Add

  • Concatenate (combine) two strings
  • Must be used on two strings. Otherwise, TypeError

* Multiply

  • Repeat a string
  • Must be used with one string and one int

17 of 51

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:

  • int() to convert something to an integer
  • str() to convert something to a string
  • float() to convert something to a float
  • bool() to convert something to a boolean

18 of 51

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'

19 of 51

Comparison Operators

<

<=

==

>

>=

!=

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

20 of 51

Logical Operators

Logical operators: combine booleans

  • and
  • or
  • not

21 of 51

Combining Operators

PEMDAS but for boolean expressions…

PNAO!

(Parentheses, not, and, or)

22 of 51

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

23 of 51

if statement

if [condition]:� [execute this code]� [execute this code]

If there are two lines of indented code, both lines "belong" to if statement.

24 of 51

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

25 of 51

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!

26 of 51

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!

27 of 51

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.

28 of 51

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.

29 of 51

while loop

x = 0

while x < 5:

print(‘small’)

print(‘woo!’)

x < 5

False

small

Start

True

woo!

30 of 51

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!

31 of 51

break statement

x = 1

while True:

if x == 5:

break

print("test")

x = x + 1

print("will this print?")

32 of 51

Better: use a good condition

x = 1

while x < 5:

print("test")

x = x + 1

print("will this print?")

33 of 51

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

34 of 51

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

35 of 51

Practice: Indexing

school = 'Fisk'

print(school[1])

print(school[len(school) * -1])

print(school[4])

i

F

IndexError

36 of 51

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’

‘’

37 of 51

Escaping

\n - newline

\t - tab

\" - quotation mark

\' - apostrophe

\\ - backslash

38 of 51

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

39 of 51

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

40 of 51

Import Statement

import math

print(math.ceil(3.4))

from math import ceil

print(ceil(3.4))

OR

41 of 51

Flip a Coin

from random import randint

number = randint(1,2)

if number == 1:

print('Heads')

else:

print('Tails')

42 of 51

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

43 of 51

Function: Defining A Function

Name the function

def my_function(parameter1, parameter2):

# do some task using parameter1 and parameter2

44 of 51

Function: Defining A Function

Name the parameter(s) to do the task

def my_function(parameter1, parameter2):

# do some task using parameter1 and parameter2

45 of 51

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

46 of 51

Variables vs Functions

Variables memorize data, give name to values

Functions memorize code, give name to a block of code

47 of 51

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

48 of 51

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

49 of 51

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

50 of 51

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)

51 of 51

Questions?