Exam 1 Review#

Course Announcements

Due this week:

  • CL3 due Friday

  • A2 due Sunday

Notes:

  • E1 starts Monday

Midterm Expectations#

You will be expected to:

  1. Describe and understand all coding concepts discussed thus far

  2. Recognize, understand and respond to Python syntax:

    • statements & comments, whitespace, code blocks & indentation levels

  3. Recognize, understand and be able to explain code constructs:

    • variables (including collections), operators, functions, & conditionals

  4. Be able to apply what you’ve learned:

    • Given a task:

      • what kind of code constructs are needed to answer it?

      • what kind of concepts does it relate to?

  5. With respect to everything above, be able to read and respond to short code snippets, write out short bits of code, and debug code.

Day of Exam:#

What to Bring:

  • A Physical ID

  • Your Brain

Location: TTC-CBTF - AP&M B349 (basement)

  • there are lockers for your stuff; best to arrive a little early to figure them out

  • you will be taking the exam on an unfamiliar computer

  • if you have OSD accommodations: Pepper Canyon Location

Exam Window: 4/27-5/1 (Mon-Fri); 45min

Exam 1 Plan#

Exam (25 Qs):

  • Q1 - blank jupyter notebook (no credit)

  • Q2-11 Multiple Choice

  • Q12-23 “Short Answer” (line of code, matching, drop-down, parsons)

  • Q24-26 Code Debugging

Topics (#Qs):

  1. Variables (3)

  2. Operators (6)

  3. Functions (7)

  4. Conditionals (4)

  5. Collections (4)

Notes:

  • To self: demo parsons problems (parsons_date_people, parsons_buy_car)

else vs elif

  • else - catch-all; what you want to happen if everything else above is False

  • elif - test additional conditions

# importance of additional condition (elif)
score = 95

if score >= 90:
    print('A')
elif score >= 80:
    print('B')
elif score >= 70:
    print('C')
else: 
    print('F')
A
# problem if we just used else in this example
# would say 50% is a 'B'; that is not right
score = 50

if score >= 90:
    print('A')
else:
    print('B')
B
# can you use more than one if
# python says yes
# logic says no
score = 95

if score >= 90:
    print('A')
    
if score >= 80:
    print('B')
    
if score >= 70:
    print('C')
else: 
    print('F')
A
B
C

When to use print? return?

  • print - to just see values on the screen (debugging, testing)

  • return - get output from a function (if you have def -> put return at the end)

    • issue with print() at end of function - you can’t store the output

    • on exam, if we ask for output or you to return something, we’re expecting return

    • get in the habit of return at end of function definition

Tuples?

  • a type of variable where you can store multiple things

  • but you CANNOT change any of the values after it’s been defined

my_tuple = (1, 2, 3, 4,)
my_tuple
(1, 2, 3, 4)
my_tuple[0] = 'new_val'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[19], line 1
----> 1 my_tuple[0] = 'new_val'

TypeError: 'tuple' object does not support item assignment

Indentation rules

  • if it’s part of your function, must be indented inside your function

  • return must be at level of indentation when you actually want the output

def return_grade(score):
    if score >= 90:
        out = 'A'
    elif score >= 80:
        out = 'B'
    elif score >= 70:
        out = 'C'
    else: 
        out = 'F'
    
    return out
return_grade(95)
'A'

Topics#

  • Variables (including collections)

  • Operators

  • Functions

  • Conditionals

Variables#

Types:

  • strings, integers, floats, Booleans

  • Collections: lists, tuples, dictionaries

Concepts:

  • Assignment

  • Purpose of/use for each variable

  • Mutable vs. Immutable

  • Indexing

Operators#

  • assignment : =

  • math: +, -, /, *, **, //

  • comparison : ==, !=, <, >, <=, >=

  • logic: and, or, not

  • membership : in, not in

    • difference in how membership operators behave on different types of variables (strings, lists/tuples, dictionaries)

  • concatenation (strings): +

Notes:

  • know the difference between = and ==

  • Be able to assign variables of different types and how to visually distinguish different variable types

  • = -> assignment; store the thing on the right in the variable on the left

  • == -> equalty; is the thing on left exactly equal to the thing on the right -> returns a Boolean

  • != -> not equal (if thing on left not equal to thing on right -> returns True)

  • += or /= -> will remove from practice exam; not on real exam

// - floor division
% - modulus

16/5 3r1
- floor division gets you the three
- modulus gets you the 1 (remainer)

Functions#

  • defining a function (def)

    • return

  • executing a function

    • parameters

      • keyword vs. positional

      • default values

Notes:

  • know difference between function definition and function execution

  • be able to debug a provided function given the goal of the function

Conditionals#

  • if, elif, & else

Notes:

  • deeply understand how to logic through/read conditionals

  • know which parts are required/when to use which

  • know how to think through a given conditional

Questions#

What questions do you have!?

Practice#

These practice questions are NOT representative of the typical question on the exam. But, many students are feeling pretty good about MC, matching questions, and short answer questions (and you’ve done lots of those on PL so far) and less confident when there’s less structure. So, I figured if we did these together…we’d get everyone feeling a bit more confident!

The form we’ll use for today (we’ll just keep using the same one….): https://forms.gle/Wf2HdBhk9ttv7xTW8

Q1#

Q1. Given the function below, describe what the function is doing. Then, execute the function so that each of the following is True:

  • out1 == '¶'

  • out2 == 'ƫ'

  • out3 == 'ḉ'

def display_char(index):
    
    objects = ['x', 'ã', 'ƫ', '%', '§', '¶',
              'Ł', 'Œ', 'Ɓ', 'Ɍ', 'Ɏ', 'ḉ']
    
    char = objects[index]
    
    return char

What this function is doing:

  1. taking in a single index (numeric; int)

  2. defining a list of objects containing single character strings/symbols

  3. indexing into that list to extrac a single character from that list -> storing in char

  4. Retuning that single character from objects from the function

# YOUR ANSWER HERE
out_1 = display_char(5)
out_2 = display_char(2)
out_3 = display_char(-1)
# checking my work
print(out_1, out_2, out_3)
¶ ƫ ḉ

Concepts in Q1:

  • variables: assignment

  • functions: definition and execution

  • collections: lists and indexing

Q2#

Q2. You’ve written a function compare_name_lengths() that isn’t working. It’s supposed to work like this: given two strings – name and a comparison_name – it will determine if there are more letters in name (name) relative to the number of letters in the comparison_name.

If your name is:

  • shorter than the comparison_name, return the string ‘shorter’

  • longer than the comparison_name, return the string ‘longer’

  • the same length as comparison_name, return the string ‘same length’

For example:

  • compare_name_lengths(name='Shannon', comparison_name='Josh') would return 'longer'

  • compare_name_lengths(name='Josh', comparison_name='Kayden') would return 'shorter'

Debug the provided function to accomplish the above.

# Original
def compare_name_lengths(name, comparison_name):
    if name > comparison_name:
        output = 'shorter'
    elif name < comparison_name:
        output = 'longer
    elif name = comparison_name:
        output = 'same length'
# Debugged
# conditionals in correct direction
# ensured equality used ==
# added return statement
# used len to compare length of strings
# fixed missing quote at end of longer
def compare_name_lengths(name, comparison_name):
    if len(name) > len(comparison_name):
        output = 'shorter'
    elif len(name) < len(comparison_name):
        output = 'longer'
    elif len(name) == len(comparison_name):
        output = 'same length'

    return output
compare_name_lengths(name='Shannon', comparison_name='Josh')
'shorter'
compare_name_lengths(name='Josh', comparison_name='Kayden')
'longer'
compare_name_lengths(name='Josh', comparison_name='Lisa')
'same length'

Concepts in Q2:

  • function definition

  • operators: comparison operators

  • collections: strings and len

  • conditionals