CptS 111 Lab #1 Exploring Python Basics Learning Objectives: - Use the IDLE Shell window to run Python interactively - Use the IDLE Editor window to write a Python program (script) - Run Python programs in the Shell window - Write programs that make use of concepts from zybooks and save them to a directory/folder Lab Procedure: Each lab consists of tasks that must be done in order. When instructed to do so, you must demonstrate to your TA that you have completed a task or you won t get credit for it. Credit is binary: you either get credit or you don t. The same procedure will be followed for all labs. 1 Your TA will help you with the tasks as needed, particularly during this first lab. You are also free to ask for help from your classmates and to offer it as well. The initial tasks indicated by 0 s don t count for credit because they re housekeeping tasks. Task 0: If you haven t done so already, open an account on Piazza (http://piazza.com). The Piazza forum will become very useful to you when you become stuck on programming assignments! Your instructors (I m including TAs as instructors) will also use it to communicate with you, e.g., letting you know whether they have to cancel their office hours. Task 00: Be sure that you have a correct version of Python on your laptop (3.4, 3.5, or 3.6). If you don t have a correct version, download it fromhttp://python.org/download. If you run into problems, ask for help. (Note to Mac users: You ll also need to install ActiveTcl from http://www.activestate.com/activetcl/downloads.) Task 000: Start the IDLE application. IDLE is an Integrated Development Environment (IDE) that allows you to run the Python interpreter. It has an interactive window called the Shell window where you can enter commands and run programs, and it has an Editor window that allows you to write and save programs (code/scripts). The IDLE IDE is simple compared to other IDEs, but it will still provide some help with commands as you will discover. Windows: To start IDLE, click the Start button in the lower left corner of the screen. If you don t see Python in the menu that appears, click on All Programs. Eventually you ll see a list of applications. Click on Python 3.x and then on IDLE (Python GUI). This should bring up a window labeled Tk Python Shell. The interactive Python prompt (>>>) indicates that you can enter Python commands because you re in the IDLE Shell window. 1 TAs can award partial credit if they think it s justified. 1
Mac OS X: In a Finder window look for a directory called Python 3.x in the Applications folder. Click on the directory, and then double click on IDLE. This will open up the IDLE Shell window where you can enter Python commands. Task 1: Using the IDLE Shell Window. IDLE allows you to have one Shell window and multiple Editor windows (we ll discuss Editor windows in the next Task). You use the Shell window to run Python interactively and to run programs that you ve created in an Editor window (you can also run Python programs, also called scripts, at the command line, but we won t cover this in CptS 111). Type the following commands into the Shell window: 1. help(print): As its name implies the help() function will provide help with certain Python constructs.. cat = : Declare the variablecat and assign it a value of. 3. type(cat): Returns the data type of the value stored in variablecat. What is it? 4. cat = dog : Reassign the stringdog to the variablecat. 5. type(cat): Now what iscat s type? 6. # This is a comment.: Comment lines start with an octothorpe. 7. type(cat) # No comment.: Comment lines can be placed next to a command, but they should be used carefully so comments don t get missed. Figure out how to get the following output: >>> Hello-O-World, and (another blank line) Goodbye-O-World! >>> Note that the interactive prompt signs and the blank line text are not to be reproduced but are included to let you know you re supposed to have blank lines in some places. Remember that you can usehelp(print) if you need help in figuring out how to print a blank line. Task : Using the IDLE Editor Window and Saving Files In this task you will use the editing capability of IDLE to edit a file and then save it to a directory. First open IDLE. Next click on thefile menu and then highlight and click onnew Window, or, alternatively, type Control-N (i.e., hold down the control key and then type the N key) (Windows)
or Command-N (Mac OS X). This will open up a new window labeleduntitled. This window is not interactive; it is the window in which you will edit your Python programs. Click on theuntitled window to select it. Next type the following # your name # today s date # CptS 111 # Lab #1, Task print("hello, World!") print("good-bye, World!") For your name, type your name, and for today s date, type today s date. Next you need to save this program. Click onfile and selectsave or type C-S. Windows: A new window will appear that requests information about how to save your file. Click themy Documents icon that appears on the left. Then create a new folder calledcs111 by clicking on the icon in the upper right that looks like a folder with a sparkle in one corner. A new folder will appear and the text will be selected. Type CS111. Now, double click on this new folder and create another new folder called Lab1. Now double click on Lab1. Find the field labeled File name: at the bottom of the window and type hello.py. Click on the Save button or else hit thereturn key. Mac OS X: After you type C-S and thesave window opens you will see a field in which you can type the name of the file. If you don t see columns of directories and files, click on the downward-pointing arrow. The window will expand and reveal some of your directories and files. You should see Documents centered just under the writing field. Click the button labeled New Folder and type CS111 in the space for the name and then click on Create. Click again on the New Folder button, type Lab1 in the writing field, and then click Create. Finally, typehello.py in thesave As: field, and click onsave. The edit window should now be labeled hello.py. Above this window, look for the Run pulldown menu. Click on Run and then on Run Module. Alternatively, just hit the F5 function key (Windows) or fn-f5 (Mac OS X). The results of your program should now appear in the interactive window (the Python Shell window). Demonstrate that you have successfully completed this task to your TA for credit. Task 3: Body Mass Index and Metric Dimensions The body mass index (BMI) is given by the formula weight kg (height m ) In the future we will just write C-X with the understanding that C- is the control key on a Windows machine and the command key on a Mac. 3
whereweight kg is weight (or, more precisely, mass) in kilograms andheight m is height in meters. What BMI tells us about one s body or health is subject to debate, but we won t worry about this. Instead, we ll write a program that will prompt a user for his or her weight in pounds and height in inches and then report the user s BMI, weight in kilograms, and height in meters. One pound corresponds to 0.4535937 kilograms and one inch corresponds to 0.054 meters. Thus, someone who weighs 160 pounds and is six feet tall (7 inches) has a mass of roughly 7.57 kilograms and a height of 1.888 meters. Using IDLE, open a new file (C-N), and write a program that prompts the user for his or her weight and height (in pounds and inches). The user should be able to enterfloat values. Your program should convert values as necessary, calculate the BMI, and then report the BMI and the user s weight and height in metric units. For example, 1 Enter weight [pounds]: 160 Enter height [inches]: 7 3 BMI = 1.6996783839 4 Mass = 7.574779 [kilograms] 5 Height = 1.888 [meters] Here s another example (this time withfloat inputs): 1 Enter weight [pounds]: 130.5 Enter height [inches]: 69.5 3 BMI = 18.9949961478 4 Mass = 59.19380485 [kilograms] 5 Height = 1.7653 [meters] After your program is working properly, save it to yourlab1 directory. Then show it to your TA and demonstrate that it does what it should. Task 4: Calculating Your Grade We ll assume that all your assessment scores are on a 100-point scale. Then your overall score is given by: score = 0.17 exam1+0.18 exam+0.18 final +0.5 prog assign +0.08 zypa+0.0 zyca+0.1 lab where exam1 and exam are your scores on the two midterms, final is your score on the final exam,prog assign is your programming assignments score, zypa is your zybooks participation activities score, zyca is your zybooks challenge activities score, andlab is your labs score. Write a program that will prompt the user for all their scores one at a time and report their final overall score. Here are some examples: 1 Please enter the following information: 3 Exam 1 score: 100 4 Exam score: 100 4
5 Final exam score: 100 6 Programming assignments score: 0 7 zybooks participation activities score: 100 8 zybooks challenge activities score: 100 9 Labs score: 100 10 11 Your score for CptS 111 is: 75.0 1 Please enter the following information: 3 Exam 1 score: 83 4 Exam score: 84 5 Final exam score: 84 6 Programming assignments score: 100 7 zybooks participation activities score: 100 8 zybooks challenge activities score: 100 9 Labs score: 100 10 11 Your score for CptS 111 is: 91.35 When you think your program is working properly, show it to your TA and demonstrate that it works. Be sure to save it to your Lab1 directory. Task 5: Elapsed Time in Seconds and Hours Write a program that calculates the total amount of elapsed time in both seconds and hours. The program should prompt the user for an integer number of hours, an integer number of minutes, and an integer number of seconds. It should compute the total number of seconds as an integer value, and then it should compute the total number of hours as a floating-point value. The output should be separated from the input by a blank line. Here s an example result: 1 Enter the number of elapsed hours: 4 Enter the number of elapsed minutes: 31 3 Enter the number of elapsed seconds: 0 4 5 The total number of elapsed seconds: 1680 6 The total number of elapsed hours: 4.5 When you re done with your program, save it to your Lab1 directory, and make sure your TA has given you credit for this task. Congratulations on finishing your first lab! If there s time left, help others who are still struggling or else do your zybooks reading. 5