Goals
Upon successful completion of this lab, you should be able to:
- Edit Python programs in our primary development environment and a web-based development environment
- Read, write, and edit simple Python programs that use input statements, variables, output statements, numeric data types and simple arithmetic operations.
Introduction
This is our first lab. How do labs work? Generally:
- You will open the Lab Instructions (on this website) and the Lab Write-up (on Moodle) in two different browser tabs.
- You will follow the instructions from top to bottom. At certain points in each lab, you will be prompted to do questions in the write-up. Also, at certain points in each lab, you will be prompted to demo something for some credit.
- When you get to the demo, if it is not already clear how to submit and get demos recorded, then just call an instructor or student assistant over and ask about the process.
- Each lab, to get full credit, you will complete three components: (1) demos that are recorded by staff, (2) the write-up on Moodle, and (3) submit code on Moodle for grading.
- All components of the lab (write-up, demos, code) are due by the following Tuesday at 2PM.
Note: If you brought your own computer, go to the Programming Tools page and follow the instructions to install the course software before continuing with the lab. The course staff is happy to help with this too.
For this first lab, you will be doing the following:
- Review our course syllabus, including the deadlines and policies.
- On Moodle, find and click on "Lab 1 write-up"
- Answer Questions 1–3 in that writeup
- Practice writing and executing python statements in an Online Python Tutor in Practice section, below
- Set up the software (called PyCharm) that you will use to write Python programs in Setup section, below
- Do Parts A–C, below
Practice
In this part of the lab, you will type print
statements into an online Python tutor to see how they work.
Instructions
- Go to your browser tab with the Lab 1 writeup in Moodle, and answer Question 4 (and ONLY
Question 4). Vocabulary is not the main point of this class, but it's important to have a
common language to talk about these new concepts.
Feel free to ask the instructor for help or search the internet -- whatever it takes to help you understand what all of these words mean.
Click the "Check" button to check your answers until you get full credit for the question. - There is an online Python 3 tutor that
allows you to watch a program execute step by step. Open that website in a new tab and enter the following Python code in the box labeled
"Write your Python code here:"
print('Hello!') print('Hi!')
Then click "Visualize execution." - You should now see your code at the top left of the window. A red arrow points to the next instruction that will be executed.
Try clicking the "Forward" button once. Verify that you can see the following:
- The output of the first statement will be displayed in the window just below your program.
- The red arrow has moved on to the next statement to be executed.
- A faint green arrow points to the statement that you just executed.
- Each time you click "Forward", the next statement will be executed and the output will be updated. Try it!
Click the "Edit code" link just below your program, and replace your old code with the following code:
print('Hello!', 'Hi!')
Visualize this program.Notice that the default behavior of the
print()
function in Python is to print each argument separated by a space. Below, the same code is repeated, but this time the two arguments are highlighted to show where arguments begin and end:print('Hello!', 'Hi!')
What if we didn't want a space printed between the arguments? It turns out that there is a special argument we can use with print: a named argument called
sep
. Thesep
argument changes the separator that is inserted between each argument.Replace your code with the following code:
print('Hello!', 'Hi!', sep='*')
Visualize this program. Observe how the output changes. By default, the value of the named parametersep
is the string' '
(a space). Above, we have changed the default value a space to the string'*'
.- Try to answer Question 5 in your Moodle writeup. For each problem, try to make a guess about the behavior, click the "Check" button to check your guess, and then try the command using the visualizer in the Online Python Tutor to see exactly what each statement will print.
- Answer Question 6 in your writeup. Most writeups have a multiple-choice or free-response question like this.
There are two reasons why:
- Writing about technical topics is an important skill. It helps solidify your understanding of the material, helps you talk to peers, facilitated help-seeking, allows you to look online for help independently, etc.
- Verbalizing your understanding of a concept is the easiest way to elicit minor misunderstandings that you may have, before they cause you serious problems. Ask course staff for help if you have trouble expressing yourself: trouble expressing an idea is often a symptom that your internal understanding does not quite make sense yet.
- Now replace your code in the Online Python Tutor with the following code:
x = "I" y = 'see' x = "what" x = 'you' y = "did there!"
Then click "Visualize execution." - Try to answer Question 7 in your Moodle writeup based on what you observed. If you get stuck, you can click the "First" button in the Online Python Tutor to restart the visualization.
- Continue to Setup.
Setup
In this part of the lab, you will use PyCharm (http://www.jetbrains.com/pycharm/) to write and run a Python program. The screenshots on this page were taken on a Mac. For now, that is the operating system that you should run in our labs.
Instructions
- Find the PyCharm icon on the menu-bar, as displayed below, and double-click on it to launch PyCharm.
- The following screen will pop up. The first time around, it may take a bit longer for it to launch.
- Select Create New Project. A new window will appear as shown below.
- You create a new project for every lab for this course. So, let's call the project for this lab, Lab01. Click Create button on bottom right of this window.
- When you create a project, PyCharm creates a folder (directory) for you so that you can store all files that are related to that project in that folder. Double-click on Lab01 (top-left) to see the following image.
- Right-click the mouse in the highlighted project-name and then slide the mouse and place it on New menu-item and then click on File.
- Now, type the name of the Python program for this lab, lab01.py and click on OK
- Now, PyCharm is ready for you to enter code in the file that you just created.
- Copy and paste the following code-segment into the empty panel.
""" Lab 1: This program prompts for your name and then greets you by name. Author: ____________ """ def main(): name = input('What is your name? ') print('Hi there, ', name) main()
- Modify the docstring at the top of the program to add yourself as the author of the program.
- Go to the Run menu (on the top of the screen), then select "Run...". A sub-window opens up:
- Click on lab01 to run the program.
- Notice that the code-window splits and in the bottom window, the prompt appears. Put the mouse in the bottom window and type your name at the prompt and hit the return key.
- To run it again, click on the green arrow on the top-right corner of the code-window.
The program that you just created is stored in cs115user/PycharmProjects/Lab01/lab01.py as depicted below.
Caution: In the CS lab, if you restart your machine, the Lab01 folder and lab01.py file will be deleted. You always need to preserve your work before you log-out and leave.
Advice: You have a few options to preserve your work. See Programming Tools for ideas: you can bring a flash drive, e-mail it to yourself, etc. One option is to use the CS Department's server, called blue. Directions for using blue are also outlined on the Programming Tools page.
Part A
Instructions
- Follow the instruction in Setup section and create a new Python program called lab01a.py in project cs115/lab01. Note: To create the project cs115/lab01, you will need to replace the "untitled" in step 3 of Setup section with cs115/lab01.
- In PyCharm, modify your program so that it looks like this. Make sure your name is in the docstring.
""" Lab 01a: This program prompts for your name and then greets you by name. Author: ____________ """ def main(): name = input('What is your name? ') print('Hi there, ', name, '.', sep="") main()
- Run your program again. Reflect: what does
sep
do? If you aren't sure, go back to Step 6 from Practice and maybe ask an instructor for clarification. - Modify the program so that it asks you for your favorite movie
and then tells you that your taste is terrible.
Here is an example of what your program should do. In this example,
the user's input is
underlined and italicized,
and the rest of the text should be printed by your program:
What is your favorite movie? Transformers Ugh, Transformers is a terrible movie.
- Here is another example. In this example, the user's input is Mission Impossible.
What is your favorite movie? Mission Impossible Ugh, Mission Impossible is a terrible movie.
- Change the first line of the docstring so that it accurately describes what the program does now.
- Demo. When your program's behavior matches the sample outputs exactly, flag down the instructor or student assistant to demo your program. This demo is part of your lab grade, so don't skip it! You can redo the demo as many times as necessary until you get it perfect.
- Once you have done the demo and are happy with your program, answer Question 8 in Moodle without using Python Tutor.
- Continue to the next part.
Part B: Computing the area of a square
The instructions below will help you write a program that:
- Asks the user to enter the side length for a square.
- Calculates the area of the square.
- Prints that calculated area.
You should be able to trace through the execution of this program and understand how the values of the variables change after each statement.
Instructions
- Answer Questions 9-11 in Moodle.
- Follow the instruction in Setup section and create a new Python program called lab01b.py in project cs115/lab01.
- Type (or copy-paste) the following program exactly as written, substituting your name for the italicized text:
""" Program: CS 115 Lab 01b Author: Your name Description: This program will compute the area of a square, given the side length. """ def main(): # Get the side length length = int(input('Enter a numeric value: ')) # Compute the area of the square square_area = length * length print("The area of a square with side length ", length, " is ", square_area, ".", sep="") main()
- Execute the program several times to complete the table in Question 12 of your Moodle writeup.
- Each time you execute the program, you will be prompted to enter a value. When that happens, type in the information from one of the rows, exactly as it appears in the Input Value column, and then press the enter key.
- Record the value that your program reports for the area of the square.
- Continue to Part C.
Part C: Computing the perimeter of a square and volume of a cube
In this part of the lab, we will extend our program from Part B, to compute the perimeter of a square and volume of a cube, given the length of the side of square/cube.
Recall that the perimeter of a square with side length s can be calculated as
P = 4s.
And the volume of a cube with side length s can be calculated as
V = s3.
Here is a sample of what your revised program will do. The user's input is italicized and underlined.
Enter a numeric value: 10 The area of a square with side length 10 is 100. The perimeter of a square with side length 10 is 40. The volume of a cube with side length 10 is 1000.
Instructions
Save lab01b.py as lab01c.py. In PyCharm, this can be done by clicking on the "File" menu on top, and then clicking on "Save As..". Work on the file lab01c.py from this point onwards.
Since you are extending your program, do not delete any of your existing code as you follow the instructions below. Also, do not prompt the user to enter another value.
- In your current program, notice that the statement
square_area = length * length
computes the area of a square whose side length islength
and saves the result in a variable calledsquare_area
. - Just after that statement, add a similar statement to your program to calculate the perimeter of a square whose side length is also
length
. Your statement should store the result of this calculation in a new variable with an appropriate name. - After your current print statement, add a statement to the program to print the perimeter of the square. This statement should be similar to the
print
statement that prints the area of the square, but be sure to change the wording to match the sample output above. - Run your program, typing in the number 10 when prompted. Compare your results with the output below:
Enter a numeric value: 10 The area of a square with side length 10 is 100. The perimeter of a square with side length 10 is 40.
- Ensure your program matches the sample output above exactly. Then move on to the next step.
- Just after the perimeter calculation, add a line to the program to calculate the volume of a cube whose edge length is
length
. Store the result of this calculation in a new variable with an appropriate name. - Add a line to the program to print the volume of the cube. Be sure the wording matches the sample output exactly.
- Run your program. Make sure it matches the example below:
Enter a numeric value: 10 The area of a square with side length 10 is 100. The perimeter of a square with side length 10 is 40. The volume of a cube with side length 10 is 1000.
- Run your program. Record the perimeter of the squares and volumes of the cubes in Question 13 and 14 of your writeup.
- Update the docstring at the top of your program so that it accurately describes what the program does. Make sure that it lists you as the author of the program.
- Continue to Submission.
Assignment Submission
Instructions
- Answer Question 15 in your Moodle writeup. Review your answers, and then click the "Next" button at the bottom of the quiz. Once you do that, you should see something similar to this (note, number of questions may be different in your writeup; just make sure all questions have been answered):
Click the "Submit all and finish" button. Warning: You must hit "Submit all and finish" so that your writeup can be graded! It is not submitted until you do this. Once you have submitted your quiz, you should see should see a dialog box similar to one shown below. The important part is that the "State" is recorded as Finished.
Please leave this window up as a tab in your browser.- Click on the "Lab 1 code" link in Moodle. Follow the instructions to upload your source code (lab01c.py) for Lab01. You should see a dialog box similar to one shown below. Keep this open.
Warning: Ultimately, it is your responsibility to ensure you have submitted all the parts of the lab correctly. You might want to double-check everything is submitted, and also backed up.
Optional: For your peace of mind, you can call over the instructor or a student assistant and have them verify your confirmation screens and let you know if it looks like you submitted all the parts of the lab and that your demo got recorded. We are happy to be a second set of eyes for you.
If you are confident that everything is completed, you may close your tabs, log out of your machine and leave lab.