Section 2.3 Programming with Data
Preview Activity 2.3.1.
Consider the following code that prints out the first ten square counting numbers.
(a)
Copy this program into a code cell, and modify it to print out the squares from 0 to 14.
(b)
Write a sentence about what you notice about the provided code. What do you think could be improved to make this code more elegant?
Activity 2.3.2.
Usually you will want to not only store collections of data, but do something with each datum. A for loop allows you to deal with data piece-by-piece.
(a)
Save the following numbers as a list named favorite_numbers
.
(b)
Adapt the partial example shown in 2.3.2 to display output similar to 2.3.3 for each number.
(c)
To deal with pairs of data, for loops can also be nested. Adapt the code shown in 2.3.4 to create output as shown in 2.3.5.
Activity 2.3.3.
Often the way you want to deal with data is conditional, that is, the code you want to run will depend on if a variable satisfies a certain condition.
(a)
Paste the code in 2.3.6 into three code cells, changing the value of money_in_bank
so that each cell outputs a different response.
(b)
Bugfix the code shown in 2.3.7.
Activity 2.3.4.
A common exercise in entry-level programming interviews is known as “FizzBuzz”. Write a program that for each of the numbers \(1\) through \(100\) prints that number, the word “Fizz” if that number is divisible by \(3\text{,}\) and “Buzz” if that number is divisible by \(5\text{.}\) Example output for the numbers \(1\) through \(15\) is given in 2.3.9.
The code in 2.3.8 implements FizzBuzz in Python, but the lines are out of order and missing indentation. Fix the code to correctly display output as in 2.3.9.
Exercises Exercises
1.
Use the code from string import ascii_lowercase; letters = list(ascii_lowercase)
to obtain a list of the lowercase English alphabet. Then print(letters)
to display them as the output of your code cell.
2.
The line for count,letter in enumerate(letters):
allows you to loop through each letter in letters
while also keeping count of where you are in the loop (starting with the first counting number \(0\)).
Use enumerate
to produce the output shown below.
Letter 0 is a. Letter 1 is b. [...] Letter 25 is z.
3.
Copy your code from the previous exercise, and modify it using ord(letter)
to display the following output instead.
Letter 0 is a, which has Unicode value 97. Letter 1 is b, which has Unicode value 98. [...] Letter 25 is z, which has Unicode value 122.
4.
Scrabble 1 is a popular crossword-style game. Like most word games, deciding on what word is best to play is often a mathematical exercise.
First, save https://raw.githubusercontent.com/zeisler/scrabble/master/db/dictionary.csv
as the file dictionary.csv
in the same folder as this notebook.
Then unscramble the code in 2.3.10 to create nested loops that count the number of vowels used in each of the first fifty words of the Scrabble dictionary.
5.
Copy your code from the previous exercise, and then replace the print
line with an if
/elif
/else
conditional to produce output similar to 2.3.11.
https://scrabble.hasbro.com/en-us