Lab 1 (Jan 16_19) - COMP-1001-001_002_003_056 (Intro to Programming & Laboratory)

.pdf

School

Memorial University of Newfoundland *

*We aren’t endorsed by this school

Course

1001

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

9

Uploaded by rami_h

Report
4/1/24, 8:22 AM Computer Science 1001 - Lab 1 https://online.mun.ca/d2l/le/content/567611/viewContent/5087522/View 1/9 Computer Science 1001 - Lab 1 Chapter 2 Practice working with assignment statements and numeric data types Practice working with string data types Learn to use the turtle to draw geometric figures (optional) In order to answer each online multiple choice question, click on the down arrow of the box containing the possible answers and click on the one you think is correct. When you have answered the questions, click on the Check My Answers button to determine how many questions you have answered correctly. The Dismiss Results button closes the results window. The Reset button sets all the answers back to the default values. If the question is a fill-in-the-blank instead of a multiple choice, you simply have to click in the blank box and type your answer; then click on Check My Answers . If you think an answer to an electronically tested question is incorrect or you are having trouble with a particular topic, please contact one of the lab assistants for help. When checking your answers online, you may click on the Check My Answers button after you answer each question or when you have finished answering all the questions for that section. It does not matter if you get answers wrong or send your answers many times; the results are not recorded. When using functions or constants from library modules, make sure you import them. There are several ways to do this, but the method we will use in this lab is to import the required functions/constants from the module so that we do not have to reference the module when calling one or more functions from that module. For example, to use the sqrt function from the math module to find the square root of 9, we can use: from math import sqrt at the beginning of our code, then when we want to use the square root function, we can simply use sqrt(9) rather than math.sqrt(9) . If there are multiple functions or constants to import, they may be imported on the same line using comma separation. For example, from math import sqrt, pi Readings Objectives Notes Pre-lab Exercises
4/1/24, 8:22 AM Computer Science 1001 - Lab 1 https://online.mun.ca/d2l/le/content/567611/viewContent/5087522/View 2/9 Create a folder to store your Lab 1 files. Download each of the Python code files, as needed, and store them in your lab folder for use with each exercise. Check My Answers Reset Dismiss Results 1. Uninitialized variables. In Python a variable cannot be accessed without being defined or initialized. A variable is accessed if its value is used in a statement. Download the following Python file: uninitialized.py (click to download) #Uninitialized variables problem. f = 10 g *= f g = 2 g += f * 5 print ( g ) Run the program, correct the errors that you encounter by re-ordering the statements, then answer the following question: 1.1 What is the output from the program? 2. Swapping the value of variables. Download the following Python file: swapvalues.py (click to download) #Given two variables with values as shown below in the first two statements, #swap the values in the variables so that quantity holds the int value and #unitCost holds the float value. quantity = 15.9 unitCost = 10 print ( "Quantity is:" , quantity ) print ( "Unit Cost is:" , unitCost ) #Correct the following if you do not agree that this will give you the #correct results. unitCost = quantity quantity = unitCost print ( "Quantity is:" , quantity ) print ( "Unit Cost is:" , unitCost )
4/1/24, 8:22 AM Computer Science 1001 - Lab 1 https://online.mun.ca/d2l/le/content/567611/viewContent/5087522/View 3/9 Run the program, correct any errors you encounter and complete the program according to the comment statements of the program. Show Solution 3. Exploring division. Download the following Python file: division.py (click to download) a = 2 b = 14 print ( "e1:" , a / 2 ) print ( "e2:" , a //2) print ( "e3:" ,( a + b ) //2) print ( "e4:" ,( a + b )/ 2 ) print ( "e5:" , a + b / 2 ) Run the program and answer the questions. 3.1 Give the result of each of the following expressions from the above program (assume a = 2 and b = 14). Expression Result a/2 a//2 (a + b)//2 (a + b)/2 a + b/2 3.2 Why is e1 different from e2? ! 1. e1 uses real division whereas e2 uses integer division 2. e2 uses real division whereas e1 uses integer division 3. after executing e1, the variable a has changed so e2 produces a different result 4. there is no difference in the two results 3.3 Why is e3 different from e4? !
4/1/24, 8:22 AM Computer Science 1001 - Lab 1 https://online.mun.ca/d2l/le/content/567611/viewContent/5087522/View 4/9 Check My Answers Reset Dismiss Results 1. e3 uses real division whereas e4 uses integer division 2. e4 uses real division whereas e3 uses integer division 3. after executing e3, the variables a and b have changed so e4 produces a different result 4. there is no difference in the two results 3.4 Why is e4 different from e5? ! 1. the parentheses used in e4 change the order of operations so the division is done before the addition 2. the parentheses used in e4 change the order of operations so the addition is done before the division 3. after executing e4, the variables a and b have changed so e5 produces a different result 4. there is no difference in the two results 3.5 Is there any difference between a/2 and a/2.0? ! 1. no, there is no difference 2. yes, the result in the first expression is an integer whereas the result in the second expression is a real value 3.6 Is there any difference between 2//4 and 2//4.0? ! 1. no, there is no difference 2. yes, the result in the first expression is an integer whereas the result in the second expression is a truncated real value 4. Obtaining input data. Download the following Python file: statistics.py (click to download) #This program inputs four integer values and computes and prints the highest #and lowest of the values. The program also computes the average and #prints all the computed values with appropriate labels. a = input ( "Enter the first value: " ) b = input ( "Enter the second value: " ) c = input ( "Enter the third value: " ) d = input ( "Enter the fourth value: " )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help