Exam 1 Review#

Open In Colab

Q&A

Q: what is the purpose of a dictionary?
A: To be able to store key-value pairs….two pieces of information for a single element in a collection. Think of student names and their emails. If I wanted to send a student an email, but I only knew their name, it would be helpful to have a variable where I could look up someone’s name and find their email. Dictionaries allow for this!

Q: I still dont understand the difference between elif and else, and collections are still confusing.
A: Let’s discuss this during the review today!

Q: I’m confused on indexing, or keying, items in a dictionary. Can you add items from other dictionaries from earlier in your code into another dictionary?
A: You could add a key value pair from another dictionary into a new dictionary, but that’s not something we do very typically.

Q: Why doesn’t the order of the code matter when you’re making an alias of another variable? Why does the value of an alias change when I define it before I change the value of the variable it’s an alias of?
A: This is something I’ve chosen not to focus on this quarter, so it won’t be on your exam (but it is still good to know!). But aliasing works differently for immutable vs. mutable variable types. So, for immutable types (think integer), in the following code, when the value of a changes….b remains unchanged. This is b/c integers are immutable. So, the order of the code still matters; it’s the behavior of aliases with different variable types that determines the outcome. Definitely let me know if this doesn’t clarify!

a=3
b=a
a=4
b

…but for the following code with lists (mutable!) when the original list (my_list) is altered, its copy/alias (my_list2) also changes:

my_list = [1, 2, 3]
my_list2 = my_list
my_list[0] = 'new value'
my_list2

Q: So, list and tuple are the same idea, but tuple is immutable and list is mutable?
A: Yup! Other thing to remember is lists are defined using square brackets ([]) and tuples with parentheses (())

Q: lists can be stored as values in dictionaries but not as keys?
A: Correct! Keys can only be of immutable type variables. Values can be any type.

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 constructions:

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

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

    • Given a task:

      • what kind of code constructions 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:

  • An ID

  • Your Brain

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

Exam Window: 4/28-5/3 (Mon-Sat); 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 (8)

  3. Functions (6)

  4. Conditionals (4)

  5. Collections (4)

Notes:

  • This is the expected layout. Small changes to this could be made after the staff proofread/test-drive the exam.

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

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

a ='first'
b = 'second'
'first second'
a + ' ' + b
'first second'

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

Note to self:

I still dont understand the difference between elif and else.

  • elif: a condition you want to check if the ifs/elifs above it are not true

  • else: what you want to happen if everything every if/elif above is not true; a catch-all

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 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/QQCf7AvT1E5r7qH58

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
out1 = display_char(5)
out2 = display_char(2)
out3 = display_char(-1)
out1 == '¶'
True

Concepts in Q1:

  • variables: assignment

  • functions: definition and execution

  • collections: lists and indexing

Q2#

Q2. Define a function determine_match.

This function should take in two parameters, char (the output from display_char in Q1) and list_of_objects (a list of objects).

If the output from display_char is in an element in list_of_objects, this function should return True; otherwise, it should return False.

For example:

  • determine_match(display_char(5), ['¶', 'Ɏ', 'Ɓ']) should return True

  • determine_match(display_char(5), ['a', 'b', 'c']) should return False

Note: this is more complicated than what I will ask during the written exam…but if you understand this you’re in very good shape

def determine_match(char, list_of_objects):
    if char in list_of_objects:
        return True
    else:
        return False
def cookie(a, b):
    if a < 32:
        output = True
    else:
        return False

    return output
determine_match(char=display_char(5), list_of_objects=['¶', 'Ɏ', 'Ɓ'])
True
determine_match(display_char(5), ['a', 'b', 'c'])
False

Concepts in Q2:

  • operators: membership (in lists)

  • functions: definition

  • conditionals

Q3#

Q3. Define a function compare_name_lengths() that will, given a name and a comparison_name, 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'

len('Shannon')
7
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

Note to self:

I still dont understand the difference between elif and else.

Concepts in Q3:

  • function definition

  • operators: comparison operators

  • collections: strings and len

  • conditionals

Q4#

⚠️ I accidentally included a loops question. Loops are NOT on this exam. Leaving it here, b/c some students wanted to see the approach/answer. This will make more sense NEXT week. We'll discuss this one in class once we discuss loops.

Q4. Define a function name_dictionary that will generate a dictionary that stores each unique letter in the input name as a different key, and the number of times each letter shows up in your name as the letter’s value.

For example, name_dictionary('Shannon')would return: {'S':1, 'h':1, 'a': 1, 'n': 3, 'o': 1}

Note: we have not done a ton of practice with dictionaries yet, so this is likely a more difficult one. If you’ve got this one down, you’re in great shape!

def name_dictionary(name):
    name_dict = {}
    
    for char in name:
        if char not in name_dict:
            name_dict[char] = 1
        else:
            # the following line is equivalent to name_dict[char] = name_dict[char] + 1
            name_dict[char] += 1
            
    return name_dict

name_dictionary('Shannon')
{'S': 1, 'h': 1, 'a': 1, 'n': 3, 'o': 1}

Concepts in Q4:

  • function definition

  • collections: dictionaries, indexing

  • conditionals

  • loops <- OOPS!