U3: Data Structures & Iteration


3.1_lists Starting Data Structures¶ So far we have stored data in variables in basically all of our lessons. This is fundamental to programming, but it is be no means the whole story. For example, when we’ve worked with multiple points of data, we’ve stored them in multiple variables, “x”, “y”, “z”, etc. But we can […]

List Answers

3.1_answers 3.1 Answers¶ 1) In one line, create a list called “words” that contains the words in the sentence “I have a dream”. In [4]: # declare a list using the typical syntax words = ["I", "have", "a", "dream"] # print evidence print(words) ['I', 'have', 'a', 'dream'] 2) Change the list so that it says “I […]

Iterating with "for"

3.2_Iteration_for Iterating: the ‘for’ Keyword¶ Now that we have stored our data in a structure, we can proceed to use it as we see fit. If we still have to access each element with a variable one at a time, however, there wasn’t that much point in storing it that way to begin with. This […]

Iterating with 'for' Answer

3.2_answers 1) Iterate through the following list and print everything out. In [4]: # list of ints numbers = [1, 2, 3, 4, 5, 6, 7] # create a for loop to print them all for item in numbers: print(item) 1 2 3 4 5 6 7 2) Iterate through the following list and print out […]

Interlude: Nested conditions & and

3.3int_nested conditionals We introduced compound logic the last lesson when we paired a ‘for’ and an ‘if’. It’s worth noting you can use compound or ‘nested’ logic like this with two if’s as well. I am showing it here because there was already ‘else’ and ‘elif’ to worry about and achieve flexibility when we were […]

Paired Data: Dicts

3.4_Dicts Paired Data: Dicts¶ Paired data refers to items that are store together with one as the “key” and one as the “value”. In Python, the data structure used for this is called a ‘dict’ (short for dictionary), because it mirrors the way a dictionary functions: the key is the word, and it is stored […]

Paired Data: Dicts Answers

3.4_answers 1) Create a dict that maps the first 5 letters of the alphabet to the iteger of their order, ie A to 1, B to 2, etc. In [8]: # create a dictionary d = {"A":1, "B":2, "C":3, "D":4, "E":5} 2) There are three parts: Print out all of the keys in the dict from […]

Unit 3 Review

U3_review Lists¶ Lists are declared with [ ] and can store multiple items of the same type. Commas are used to separate them. In [48]: # a list of words from the Joey Bada$$ song I am listening to my_stuff = ["Land", "of", "the", "Free"] Our old standby functions are still with us. In [49]: #len still […]

Unit 3 Problems

U3_problems Q1) You are writing code to calculate the cost of a hot sauce buying rampage. The shopping list is represented by a list, and the prices are stored as name-price entries in a dictionary. If an item is in the cart, but not the dict, assume it is found to be out of stock. […]

Unit 3 Problems Answers

U3_answers Things to consider: Yes, there are repeats in the list. It doesn’t matter for this program, because iterating through the list will result in it counting the repeats. The items in the list are indexed, and the value at each index is passed through in the iteration. Consider the following: In [9]: # a list […]