Week02 tutorial solutions PDF

Title Week02 tutorial solutions
Course Introduction to Programming
Institution University of Melbourne
Pages 3
File Size 99.8 KB
File Type PDF
Total Downloads 46
Total Views 127

Summary

Answers to Week 2 Tutorial....


Description

COMP90059 Introduction to Programming Semester 1, 2019 School of Computing and Information Systems The University of Melbourne Week 2 Tutorial: Suggested Solutions

Background In this tutorial we will practice with Python expressions, statements and programs. We will also provide instructions to access the web-based COMP90059 GROK environment, and to install Python and the IDLE Integrated Development Environment on your personal PC. Only GROK is required for the course, but there are some advantages to having IDLE installed on your personal PC. Important note: We use Python version 3 exclusively in this course. Python version 2 is not compatible and should not be used for new code. Any Python version 3.x is acceptable, but we will use a few Python 3.6 features, so you need to be prepared to work around this if you use an earlier version.

Exercises 1. First, we will try using Python as a calculator. Suppose we are using Python interactively, by typing at the Python or IDLE prompt, and pressing Enter after each line. How would Python respond? (a) 3.5 / 2 A: 1.75 (b) 1 + 2 / 3 A: 1.6666666666666665, noting that if you are using Python 2 by mistake, the answer will be 1, since integer division rounds down in Python 2. We will exclusively use Python 3 in this course. (c) (1 + 2) / 3 A: 1.0 (d) math.sqrt(2) A: NameError: name ’math’ is not defined (e) 5 ** 3 A: 125 Can you explain why Questions 1b and 1c return different answers? A: Because of operator precedence. Division binds more tightly than addition, thus if we want to do the addition first we must adjust the precedence by parentheses. Can you explain why Question 1d does not return a useful result? A: Because of Python’s module imports system. If we want to use mathematical operations we must import the math module. We will look at this in the next question. 2. Suppose we type the following (with Enter after each line). How would Python respond? 1

(a) import math A: No response, the prompt returns immediately, but the math module is available for use now. (b) math.sqrt(2) A: 1.4142135623730951 (c) math.exp(3 * math.log(5)) A: 124.99999999999994 Can you explain why Question 2c returns almost the same answer as Question 1e, but not exactly? A: We have used the identity ab = eb loge a , but when powers and logs are used, the calculation is not performed exactly but only to a given level of precision, usually 15–16 significant digits in Python. 3. In the above we have been typing Python expressions, which are mathematical (or other) formulas that can be calculated to return an answer. Python prints the answer if an expression is typed at the prompt. Now we will try some Python statements, which are concrete steps to be performed and which do not necessarily return an answer unless we give the explicit command to print something. On the other hand, statements can modify the environment, e.g. by storing information to be printed later on. Type the following (press Enter after each line). How does Python respond? (a) print(5 ** 3) A: 125 (b) name = ’John’ A: No response, the prompt returns immediately, but the variable name is available for use now. (c) print(name) A: John (d) print(’Hello, ’ + name) A: Hello, John (e) name = input() A: No response, Python waits for you to type a line of input, and then puts whatever you typed into the variable name for later use. For example you could type the name Peter, and then press Enter. (f) print(’Hello, ’ + name) A: Hello, Peter What is meant by a string in Python? A: A string is a sequence of characters, or less formally it is something like a word or a sentence of text. It is normally written in either single or double quotation marks, we used single quotation marks in items 3b–f above. Strings can be manipulated more-or-less like numbers, but they support different operations, for instance the ‘+’ operator on strings means joining them together. Suppose we typed ’Hello, ’ + name at the prompt, not surrounded by print(). What would we observe? Why is the result different from Question 3f above? A: ’Hello, Peter’. This has quotation marks, because Python distinguishes between strings and numbers when printing the result of an expression. It prints things out in such a way that they could be typed back into Python to re-create the same string or number. On the other hand, print() tries to convert whatever you give it into a string, and then print only the contents of the string. For example, print(3) and print(’3’) have the same effect. This allows a more natural communication with a user who is not a Python programmer. 4. Now that we understand statements, we will create a file of statements to be executed later. If using GROK, we would log in (see section headed ‘Accessing the GROK environment’), choose any worksheet and then type the following code into the programming area headed ‘program.py’: 2

name = input(’What is your name? ’) print(’Hello, ’ + name) If we now click ‘Run’ in GROK, then in the ‘Output’ window below the programming area we will be able to see what the program is asking and respond to it. If using IDLE, we would open the File menu, and choose New File. We would see a blank IDLE window, and type the following into it: name = input(’What is your name? ’) print(’Hello, ’ + name) Then, we would save this file by the opening File menu, and choosing Save As. It can be stored in an appropriate place such as an isys90088 folder inside your home directory, called hello.py or similar. To run the program in IDLE, we would open the Run menu and choosing Run Module, or simply press F5. IDLE should return to the main interpreter window with your program running in it. You should then be able to type your name and have the program respond to you. Congratulations, you have written your first Python program!

Extra questions for self-study 5. Now for a more complex program. See if you can remember the steps involved in creating a new program, typing it in, saving it, and running it. What will the following program do? colour = input(’What is your favourite colour? ’) animal = input(’What is your favourite animal? ’) number = input(’What is your favourite number? ’) print(’I have never heard of a ’ + colour + ’ ’ + animal + \ ’ with ’ + number + ’ legs!’) Why did we use a backslash in the middle of the print() statement? A: The backslash is a line continuation character. We use it when a Python statement is too long to fit comfortably on one source code line. In fact, the backslash is optional when there is an open set of parentheses as here. But it might be considered good practice to use the backslash anyway. What would happen if we changed the print() statement as follows? Why? print(’I have never heard of a’, colour, animal, ’with’, number, ’legs!’) A: It would operate the same, but it may be more readable and convenient. Here we have given print() a comma-separated list of things to print, and it will try to print them joined up by spaces. This way is only applicable if we are happy to have spaces in between each thing that we print. What would happen if we changed the print() statement as follows? Why? print(f’I have never heard of a {colour} {animal} with {number} legs!’) A: It would operate the same, but it may be more readable and convenient. We are using Python’s built-in formatting and interpolation mechanism, which has been introduced in Python 3.6. What would happen if we changed the print() statement as follows? Why? print(’I have never heard of a {} {} with {} legs!’.\ format(colour, animal, number)) A: It would operate the same, but it may be more readable and convenient. We are using the built-in format method of Python strings, which should be available in any Python 3.x version.

3...


Similar Free PDFs