Day 4: Lists and Loops

back · home · slides · CMSC 201 (Fall 2024) @ UMBC · doing things for more things

CMSC 201: Lists and Loops

Agenda:

  • HW1 retrospective
  • HW2: what is it?
  • "Fun": IOCCC
  • Who cares? (AKA: what will we make)
  • Lists and loops
  • Goal: Understand lists and loops pretty well; be able to make any lists and know all the loop "gotchas"

    My email is: sdonahue@umbc.edu (Shane Donahue), office hours Tu/Th ITE 373 2-3PM (please do try to come with questions)

    HW1

    Probably one of the hardest assignments in terms of new things to learn? (Editing longer files, submitting multiple files, python operators and input). It went OK I think!

    Most common issues: square root, order of operations, formatting output. TAs will grade at some point (probably around two weeks-- but you should already loosely know if you did it right).

    HW2

    Let's look at HW2! Maybe HW1?

    We have HW1 through HW4 due, then a midterm (week of October 7th)! Then Project 1, HW5, HW6, Project 2, second midterm (week of Nov 10th), Project 3, Thanksgiving break, then final :)

    Full schedule: Google calendar for this class (reminder, you can read these notes at cmsc201.sourque.com).

    I will be out week of October 7th (I'll be in Italy) and week of October 28th (I'll be in Chile). For week of October 7th (midterm), I'll release a practice exam, and week of October 28th (number systems and debugging), I'll have guest lecturers. I will still be responding to emails but likely slower, please use your TA when possible!

    "Fun": IOCC: The International Obfuscated C Code Contest

    Remember the donut source code that was shaped like a donut? There's a whole competititon for that!

    One of my favorites from the latest IOCCC: https://www.ioccc.org/2020/tsoj/prog.c. What does it do?

    Asteroids! How????

    Other highlights:

  • An entire tic tac toe game implemented in a single call the print function? https://www.ioccc.org/2020/carlini/index.html
  • Automatic minesweeper with source code shaped like a mine? https://www.ioccc.org/2020/endoh1/index.html
  • Many fun competitions online :) This is one popular style of many.

    Who cares (about lists and loops)?

    Loops (and conditionals (and variables and data)) are the only tools you need to write every program ever. Lists help too. What do you write when you have the power to write anything?

    Nintendogs! Image of nintendogs DS game cover, http://www.vgchartz.com/games/boxart/full_7494769JapanFrontccc.jpg

    Lists

    Lists let you store lists of things! Here are my favorite numbers :)

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]

    You can also make empty lists. These are all empty.

    my_favorite_numbers = []
    my_other_cool_list = list()
    epic_list = []

    Empty lists are falsey!

    if epic_list: print("not going to print")

    You can have multiple types of objects in one list:

    my_favorite_things = [1, 1, "cat", -5.0, "computerz"]

    Like many things in python, just because it's possible doesn't mean it's a good idea. Having lists with multiple types is usually not a good idea, since it's hard to write good code when you don't know what type you're working with.

    You can even have lists in lists (this will be important later) (multidimensional lists):

    my_favorite_things = [1, 1, "cat", -5.0, ["computor", "programs"]]

    Indexing a list means getting a specific element from it. Indexes start at 0. You use square brackets [] to specify an index.

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]
    # Print the first element
    print(my_favorite_numbers[0])

    What if we want a sub-list? Slicing can give us a copy of a sub-list. The colon in the middle separates our start and end indicies.

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]
    # Give me the first three
    print(my_favorite_numbers[:3])

    Lists are mutable. This means you can change them, by modifying elements:

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]
    # Change the first element!
    my_favorite_numbers[0] = 400
    # Print the first element
    print(my_favorite_numbers[0])

    You can add to the end of a list by appending to it.

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]
    # Add a new element!
    my_favorite_numbers.append(21)
    # Print the list
    print(my_favorite_numbers)

    You can get the list size with the len() function:

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]
    print(len(my_favorite_numbers))

    Loops

    Cycling through items in a loop is called iteration.

    Why do we need loops? Let's say we want to iterate through this list and print each one:

    my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13]

    Option 1: print them all manually 😵

    print(my_favorite_numbers[0])
    print(my_favorite_numbers[1])
    print(my_favorite_numbers[2])
    print(my_favorite_numbers[3])
    print(my_favorite_numbers[4])
    print(my_favorite_numbers[5])
    print(my_favorite_numbers[6])
    

    Ok, that works, but what if the list size changes? What if we had 1000 elements?

    Option 2: for loop (for-each loop) 🤩

    for x in my_favorite_numbers:
    	print(x)
    

    Wow! Fun and efficient!

    Loops let you do something as many times as you want! For loops are the most common.

    Here's fizzbuzz from last time:

    for x in range(1, 101):
        if x % 3 == 0:
            print("Fizz", end="")
        if x % 5 == 0:
            print("Buzz", end="")
        if not (x % 3 == 0) and not (x % 5 == 0):
            print(x, end="")
        print()

    Range

    range(start, stop, step)

    You can think about range like a function that returns a list of numbers. Important to be precise about inputs to range. start by default is 0.

    Combining loops and conditionals: let's write some nintendogs!

    Goal:

  • For some number of days (loop!)...
  • Feed or play with toy (from lists)
  • Print dog happiness at the end :)