Week 2: Introduction to CS21 and Python Programming

Week 2 Goals

  • manipulate Python numeric and string data types

  • learn to iterate over data using for loops

  • practice string operations: +, *, len()

  • practice using the range function

  • practice using accumulators

Week 2 Code

favorite.py

The favorite.py program introduces input, converting str to int and converting int to str. The program should ask the user to enter their favorite number. The program should then respond (as the computer) that its favorite number is one more than whatever the user’s favorite number was.

You can use whatever prompts and outputs you would like. If you run the program you might get an output like this:

$ python3 favorite.py
Let's see if we have the same favorite numbers!
What is your favorite number? 5
Your favorite number was 5
My favorite number was 6

$ python3 favorite.py
Let's see if we have the same favorite numbers!
What is your favorite number? 82
Your favorite number was 82
My favorite number was 83

The solution is in the file favorite_rw.py.

kilometers.py

The kilometers.py program is the first program you will write on your own. Use the favorite.py solution we wrote together in class as a guide. The program should ask the user for a distance in miles and respond with the equivalent distance in kilometers. You can assume that 1 miles = 1.61 kilometers.

You can use whatever prompts and outputs you would like. If you run the program you might get an output like this:

$ python3 kilometers.py
This program will convert from miles to kilometers.
Enter a distance in miles: 10
10 miles is 16.1 kilometers.

$ python3 kilometers.py
This program will convert from miles to kilometers.
Enter a distance in miles: 314
314 miles is 505.54 kilometers.

The solution is in the file kilometers_rw.py.

loop_examples.py

The loop_examples.py program introduces for loops. A for loop allows your program to repeat a section of the code a specified number of times.

  1. The first example, Ex1, iterates over a list of integers (int), [1, 2, 3, 4, 5] and prints each value in the list on a line by itself.

  2. The second example, Ex2, iterates over a range that contains the values 0, 1, 2, 3, 4 and 5. Once again, we print each value on a line by itself.

  3. In Ex3, we again iterate over a list of integers, but this time instead of printing each value in the list, we print the value in the list multiplied by 2.

  4. In Ex4, we iterate over a list of strings (str). Just like in Ex1, we print each value in the list on a line by itself.

kilometers_loop.py

Starting with the solution found in kilometers_rw.py, modify the program so that it prints a table converting miles to kilometers.

The output might look like this:

This program will convert from miles to kilometers.
mi  km
10  16.0
20  32.0
30  48.0
40  64.0
50  80.0
60  96.0
70  112.0
80  128.0
90  144.0

We will write this two ways: once using a for loop over that iterates over a list, and once that iterates over range.

sum_list.py

Given a list of numbers, compute the sum and print the answer. We will write this program together from the partially completed code provided.

For example, if the list contained the values [10, 8, 12, 5], this program would print the sum of those values, 35.

The output might look like this:

The sum of these numbers...
10
8
12
5
...is 35

Can you modify the program we wrote so that it computes the product of the numbers (multiplies them together) instead of the sum? Save your solution in product.py.

bling.py

Write a program that asks the user to enter a phrase. Add some bling to that phrase by inserting asterisks (*) between each character in the phrase.

Your output might look like this:

This program adds bling to your favorite phrase.
Enter a short phrase: loops
*l*o*o*p*s*

Can you modify the program so that the user can provide what the bling looks like? For example, maybe your new output would look like this:

This program adds bling to your favorite phrase.
Enter a short phrase: loops
Enter your bling: .
.l.o.o.p.s.