Home Practicum 1 Practicum 2 Practicum 3 Practicum 4

Welcome to Practicum 2!

Today we will cover four important topics: using command line, arithmetic operations, variables, and handling user input. In the end, we will combine them all!

Command line

In the previous exercise you learnt how to run your code in Atom. That approach will be good most of the time, but it's also important to know how to use the command line to move around your hard drive and run code.

To open the command line:

To display the current directory: To move to your home directory: To display contents of the current directory: After you have displayed the contents, you will probably see that there are other directories in the current directory. To move to one of them, both in Windows and mac os type cd DIRECTORY_NAME to move to that directory. To move out from a directory, (both windows and mac os) type cd ..

Now, move to the directory where you saved the code from the previous exercise. Once you located it, run your code by typing: python hello_world.py

Interactive Python and arithmetic operations

Another good thing about using the command line is that it allows you to start the Interactive Python in a directory of your choosing. Then, you can run (evaluate) simple python commands for quick prototyping without having to run your entire code. Let's first use it as a calculator:

  1. Start Interactive Python by typing ipython in the command line.
  2. Type the following commands (one at a time!) and see the output:
    3 + 5
    3 - 5
    3 * 5
    3 / 5
  3. There are also other arithmetic operators available, such as (try running all these operations):
    • exponent (power) **:
      2 ** 3
      2 ** 4
    • floor division (skips the decimal digits, compare the output to regular division) //:
      10 // 3
      10 / 3
    • modulus (returns the remainder of the division operation) %:
      10 % 3		#10 = 3 * 3 + 1, where 1 is the reminder
      11 % 3		#11 = 3 * 3 + 2, where 2 is the reminder
      12 % 3		#12 = 4 * 3 + 0, where 0 is the reminder

Variables

Variables can be used to store values, such as numbers:

x = 5
print(x)
  1. Create a variable called y to hold value of 3.
  2. Try running x + y.
  3. You can also use variables to store results of operations. Try z = x + y. Print the value of z.
  4. Variables don't have to have single letter names and they can store other values than numbers. Try today = 'Wednesday'. Print the value of today

Taking user input

We ask the user for data using the input function, with the prompt as an argument, for example try running this code in the interpreter:
fav_show = input('What is your favorite show? ')
The user's response is now stored in a variable called fav_show. You can print it out on the screen using the print function with the fav_show variable as the argument:
print(fav_show)
You can also add your text or other variables to print in the same line and separate them with commas, like this:
print('Too bad', fav_show, 'is so underrated!')
Note that there are no apostrophes around fav_show when we pass it as an argument, unlike with the words 'Too bad'. Do you understand why? What happens when you run print('fav_show')? Why doesn't it print the name of the show you typed in? To exit ipython type exit and press Enter.

Excercise 1

Now, in atom, not in ipython, write a program that:

  1. asks the user for the title of the series they want to watch and the number of episodes.
  2. prints out the input variables in a readable way (i.e. not just as two values one after another but as a sentence about how many episodes of which series they will save for offline viewing).
'''
DS2000
Fall 2019
PR02 - bits and bytes
'''
# ask the user about the title
# ask the user about the number of episodes
# print

Save your answer as pr02.py, it will be a part of your today's submission. You will see that when you ask the user for input and run it in atom, it doesn't really work. Use command line to navigate to the directory where your file is, then run python pr02.py to see it actually work.

Parsing user input

In python everything that the user enters is treated as text. If we expect a number, we must explicitely tell Python to treat the input as one. We can use float for this if we expect a fraction or int if we expect an integer. Run these two pieces of code and see how the output differs.

Multiplying a string

string = input('Give me an integer: ')
print('Your number times ten is: ', 10 * string)

Multiplying a number

string = input('Give me an integer: ')
number = int(string)
print('Your number times ten is: ', 10 * number)

Excercise 2

Modify the contents of pr02.py for this excercise.

Let's assume that one episode of the series is 1.5 GB (1.5 gigabytes) of data. Let's extend the code from the previous excercise to estimate how much data in total the user will need to download. Now your code:

  1. asks the user for the title of the series they want to watch and the number of episodes
  2. converts the number of episodes input to a number
  3. calculates the total amount of data and saves it to total_data variable
  4. prints out the input variables in a readable way
  5. prints out the total amount of data in GB.

Save your answer as pr02.py, it will be a part of your today's submission.

Unit conversion

Great, now we know how much data in gigabytes we will need to transfer. Internet connection speeds are expressed in megabits per second, Mbps. To answer how long it will take to download all this data, we have to first convert gigabytes to megabytes to megabits. Have a look at this:

So, "kilo" means thousand (10^3), "mega" means million (10^6), "giga" means billion (10^9). How many bytes are in a megabyte?. If there are 8 bits in a byte, how many bits are in a megabyte? How many megabits is that?

Side story, 1 kilobyte was not always 1000 bytes, it used to be 1024 bytes instead. If you have extra time try googling and wrapping your head around the explanation....

Converting gigabytes to megabits is a two step process: first we convert gigabytes to megabytes, then we convert megabytes to megabits.

Excercise 3

In the pr02.py file insert additional code for converting the total_data from gigabytes to megabits. Now your code:
  1. asks the user for the title of the series they want to watch and the number of episodes
  2. converts the number of episodes input to a number
  3. calculates the total amount of data and saves it to total_data variable
  4. calculates the size of total_data in megabytes and prints it out.
  5. uses the caculated size in megabytes to calculate and print out the size of total_data in megabits
  6. asks the user for their Internet speed in megabits per second (If you're not sure what kind of speeds are reasonable: Internet providers in Boston offer speeds between 50 and 1000 Mbps, but in general your WiFi is not faster than 300 Mbps.)
  7. uses the calculated size in megabits and the speed in megabits per second, to tell the user how many seconds the download will take.
  8. prints out the input variables in a readable way.

Test case: use your code to calculate how long it would take to download 4 seasons of 13 episodes (52 episodes total, each 1.5 GB) on a 6 Mbps connection. Expected output: 104000. If your output is different, test each line one after another and try to find which one introduces the error.

Excercise 4

The code so far gives an anwer in seconds which is not the most intuitive. Using math operations such as integer division // and modulo % report that time in days, hours, minutes, and seconds (use a slower internet speed to test if your answer is below an hour). Now your code:
  1. asks the user for the title of the series they want to watch and the number of episodes
  2. converts the number of episodes input to a number
  3. calculates the total amount of data and saves it to total_data variable
  4. calculates the size of total_data in megabytes and prints it out.
  5. uses the caculated size in megabytes to calculate and print out the size of total_data in megabits
  6. asks the user for their Internet speed in megabits per second (If you're not sure what kind of speeds are reasonable: Internet providers in Boston offer speeds between 50 and 1000 Mbps, but in general your WiFi is not faster than 300 Mbps.)
  7. uses the calculated size in megabits and the speed in megabits per second, to tell the user how long (in days, hours, minutes, and seconds) the download will take.
  8. prints out the input variables in a readable way.

Start by implementing just minutes and seconds. Then build up to hours and days.

Test case: use your code to calculate how long it would take to download 4 seasons of 13 episodes (52 episodes total, each 1.5 GB) on a 6 Mbps connection. Expected output: 1 days, 4 hours, 53 minutes, and 20 seconds. If your output is different, test each line one after another and try to find which one introduces the error.