# Variables,  and Expressions

Lab Overview
The goal of this lab is to start writing short programs involving variables, assignments and expresssions. This will be accomplished by three small checkpoints. We will practice ﬁnding and ﬁxing syntax and semantic errors, we will study good program structure and also learn about hard-coding and how to avoid it.
Checkpoint 1: Syntax versus Semantic Errors
Download the program lab1 area.py from the Piazza resources pages under Labs. (Store this in a folder you create for Lab 1.) This simple Python program to compute the surface area of a rectangular solid (the sum of the areas of the six rectangular faces) contains both syntax errors (two) and a semantic error. If you don’t remember the diﬀerence between a syntax error and a semantic error, look back at the Lecture 1 notes. Working on your own machine and IDE, please ﬁnd and ﬁx the two syntax errors, making sure that the program runs. Remember that Wing IDE actually shows you syntax errors by underlining a part of the line in red. Often, this is the line containing the error. When you have ﬁxed the syntax errors, now ﬁx the semantic error. You will see that ﬁxing a semantic error is much harder, because you need to understand what the program is trying to do. This is where good programming practice comes in handy. Remember in your programs to:
• give meaningful (but short) names to your variables, • write your program so that it has a clear logic ﬂow: assign values to variables ﬁrst, then use these variables in computation and then print the result of the computation, • and ﬁnally write short comments to explain what your program is doing at pivotal points (we will talk about this later in class).
To complete Checkpoint 1: show a TA or a mentor that you have successfully resolved all the errors in this program and are able to run it.
Checkpoint 2: How big is your hard disk?
How much data can your hard drive take? This question is harder to answer than you think. The main measure of data in computers is a byte. For example, an integer in Python is 8 bytes. In computer science, a gigabyte (GB) is a measure equivalent to 210∗210∗210 = 230 bytes. In fact, most operating systems use this convention. A terabyte (TB) is 1024 gigabytes, or 240 bytes. We call this base 2 computation, everything is a power of 2. However, computer manufacturers ﬁnd this too complicated. So, instead they use base 10 computation. When they sell you a computer, a gigabyte is assumed to be 103∗103∗103 = 109 bytes. A terabyte similarly is 103 gigabytes, i.e. 103 ∗109 = 1012 bytes.
So, when you buy a 128 GB hard disk (manufacturer deﬁnition, base 10), put it in your computer, and your computer now thinks that it is actually 119 GB (computer deﬁnition, base 2). As a result, you have somehow lost 9 GB before even using your computer. Sad but true. In this checkpoint, you will write a program that prints out this size diﬀerence. To accomplish this, you are going to write a new Python program. Start by clicking File - New in the IDE to create a new program to type into. Type your solution for 128 GB ﬁrst. Make sure you use at most three variables: one for base 10 hard disk size, one for base 2 hard disk size and one for the diﬀerence. Assign value 128 to the base 10 disk size, then write formula to compute the value of the other two variables. When you are done with the computation, print the result in the following format using your variables:
128 GB in base 10 is actually 119 GB in base 2, 9 GB less than advertised.
Now, save this (very short) program to a ﬁle called lab1 part2.py in your Lab 1 dropbox folder. Run it to make sure it is correct. Once it is complete, now reuse your code to print the same information for hard disk sizes of 256 GB, 512 GB and 1024 GB as well. If you did this right, all you have to do is copy all your code, and change only the value of the base 10 variable. It looks repetitive right? We will talk about that in class on thursday! No hard-coding: You must use at most three variables for this solution and no hard coding of values. What is hard-coding? It is writing the solution directly into a program. In these early exercises and homeworks, the problems are easy so we know what the answer is before we start. This does not mean we should not write a program to learn the program syntax. For example, the following print statement is using hard-coding:
base10 = 128 print "128 GB hard disk"
The following print statement is not using hard-coding:
base10 = 128 print base10, "hard disk"
Of course, we need to hard-code the value for the initial variable for now until we learn how to read user input.
To complete Checkpoint 2: When you are convinced your program is correct, show it to a TA or a mentor. Your program should have only three variables and should not hard-code values.
Checkpoint 3: Jupiter and Earth
For the next checkpoint, you are going to write Python code that calculates and outputs some properties of Jupiter, the largest planet in our solar system. Jupiter has a diameter of 88,846 miles and its average distance from the Sun is about 483,632,000 miles. These look like big numbers, but they are a bit hard to understand intuitively. One way to appreciate them is to compare them to the values for the Earth, which has a diameter of 7,926 miles and a distance from the Sun of about 92,957,100 miles. Write a Python program to calculate and output
2
1. the ratio of Jupiter’s distance from the Sun to that of the Earth,
2. the ratio of Jupiter’s volume to that of the Earth,
3. the time in minutes and seconds it takes light to travel from the Sun to Earth (use 186,000 miles per second as the speed of light), and
4. the time in minutes and seconds it takes light to travel from the Sun to Jupiter.
Similar to checkpoint 2, start by clicking File - New in the IDE to create a new program to type into. Type your solution to just the ﬁrst calculation, using variables for both the Earth’s distance and Jupiter’s distance, and printing a string along with the ratio to explain the output. Save this (very short) program to a ﬁle in your Lab 1 dropbox folder, and then run it. Your output should look something like
Jupiter is 5.202744061507943 times farther from the Sun than the Earth is.
Repeat this process for each of the required calculations, adding to your program. Run the program after adding each part of the solution. For the second calculation, you will need the volume code from Lecture 2. For the third and fourth calculations, start by calculating the number of seconds. Then convert this number to an integer (if it is not already an integer). Finally, convert it to minutes and seconds. This is a bit tricky, requiring the use of integer division and remainder discussed during lecture. Can you ﬁgure it out?
To complete Checkpoint 3: show a TA or a mentor the completed solution, including both the code and output. You are allowed to hard-code the initial values given to you (diameter and distance of planets), but not the results of the computations.
Learning to use the submission server