COMP90059 Tutorial 1 PDF

Title COMP90059 Tutorial 1
Author Micheal Fett
Course Introduction to Programming
Institution University of Melbourne
Pages 5
File Size 257.5 KB
File Type PDF
Total Downloads 37
Total Views 131

Summary

Tutorial 1 ...


Description

COMP90059 Introduction to Programming Semester 2, 2020 School of Computing and Information Systems The University of Melbourne Tutorial 2

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 (b) 1 + 2 / 3 (c) (1 + 2) / 3 (d) math.sqrt(2) (e) 5 ** 3 Can you explain why Questions 1b and 1c return different answers? Can you explain why Question 1d does not return a useful result? 2. Suppose we type the following (with Enter after each line). How would Python respond? (a) import math (b) math.sqrt(2) (c) math.exp(3 * math.log(5)) Can you explain why Question 2c returns almost the same answer as Question 1e, but not exactly? 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)

1

(b) name = ’John’ (c) print(name) (d) print(’Hello, ’ + name) (e) name = input() (f) print(’Hello, ’ + name) What is meant by a string in Python? 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? 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: 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 COMP90059 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? Note: The material for the following questions is more advanced and will only be covered in the weeks 1–2 lectures if time permits. Otherwise, please use the simpler method shown above. What would happen if we changed the print() statement as follows? Why? print(’I have never heard of a’, colour, animal, ’with’, number, ’legs!’) 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!’) What would happen if we changed the print() statement as follows? Why?

2

print(’I have never heard of a {} {} with {} legs!’.\ format(colour, animal, number))

Accessing the GROK environment The screenshots below illustrate briefly the steps in logging into GROK and using it to run a program.

Step 1: Open http://groklearning.com in your browser, and click ‘Log In’.

Step 2: In the box marked ‘Email’, type your University of Melbourne email address, usually [email protected].

Step 3: The ‘Password’ box disappears, and instead you are given a button ‘Log in via Unimelb’. Click on this to log in.

Step 4: You are taken to a University of Melbourne authorization page. Log in with your central username and password.

Step 5: Click on the title bar of the course ‘ISYS90088 2018 S2: Introduction to Application Development’ to open it.

Step 6: In the course information page, scroll down to access the summary of the course contents.

3

Step 7: Click on any of the worksheets to open it. If you only want to experiment with GROK, it does not matter which worksheet you open.

Step 8: You are shown a welcome page for the worksheet. For now, simply type any program in the programming area, which is headed program.py.

Step 9: Click on the ‘Run’ button in the toolbar to the top right, above the programming area.

Step 10: All going well, your program’s output should appear in the ‘Output’ area, below the programming area. If you used print(), then it prints to here. If you used input(), then you can type into here to interact with your program.

Some points to note about GROK as compared to general programming environments: • GROK is mainly oriented towards completing lab and assignment work, and managing the submission and testing process, rather than for experimenting with Python generally. • In GROK you must write a complete program before you can run it, and you must use print() if you want to see anything, whereas in IDLE you can use Python like a calculator. • GROK saves your work to the GROK website (i.e. in the cloud), but it doesn’t store files generally, it only stores partially completed lab and assignment work for specific problems. • By contrast, IDLE lets you save as many files as you want, but you must save them to your personal PC, and if you do this, you are responsible for backing up your files!

Installing Python and IDLE At the time of writing the latest Python release is 3.6.5 and there is a download page at https://www.python.org/downloads/release/python-365/, if this is not available you can go to https://www.python.org/downloads/ and click through the prompts to choose your operating system and/or Python release. • On Windows: choose the ‘Windows x86-64 executable installer’ which currently links to https://www.python.org/ftp/python/3.6.5/python-3.6.5-amd64.exe. Execute this program and follow the prompts to install Python. • On Mac OSX: choose the ‘macOS 64-bit installer’ which currently links to

4

https://www.python.org/ftp/python/3.6.5/python-3.6.5-macosx10.9.pkg. Open this package and follow the prompts to install Python. • On Linux: use your OS’s package manager to install Python and IDLE. For example, on Ubuntu execute the following command in a Terminal: sudo apt-get install python3 idle3 You will probably see a message such as python3 is already the newest version. However, IDLE is not installed by default and will be installed by this command. • On the Engineering Faculty lab PCs: version 3.6.4 is already installed. However, note that it is far more convenient to use a personal PC, as you will be able to work independently.

Creating a working folder You will need to create a folder to store your Python programs in. You should create this in your home directory, which is located in one of the following places: • On Windows: normally C:\Users\username where username refers to your login username. • On Mac OSX: normally /Users/username where username refers to your login username. • On Linux: normally /home/username where username refers to your login username. • On the Engineering Faculty lab PCs: at H:\ which should be automatically mounted for all users. In practice, some students do not have a home directory, and in this case a ticket should be opened with Central IT Support. The ticket will be assigned to Engineering IT Support who will create the home directory in due course. Browse to here using your OS’s file manager (start by opening ‘Computer’ on Windows) and create a folder called isys90088 or similar (right-click and choose ‘New’ then ‘Folder’ on Windows).

Launching the environment To launch the environment: • On Windows: open the Start Menu and (on Windows 10) type in the command-line area, or (on older versions of Windows) choose Run. Then type the following command, adjusting for your Python version if necessary: C:\Python36\Lib\idlelib\idle.bat To make this easier you may wish to add C:\Python36\Lib\idlelib to your PATH and/or create desktop shortcuts, we will leave this for you to experiment with. • On Mac OSX or Linux: start a Terminal window and then type: idle • On the Engineering Faculty lab PCs: open the Start Menu and choose the folder ‘Python 3.6’ and then ‘IDLE (Python 3.6 64-bit)’. In the IDLE window you should see a welcome message something like Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> The >>> prompt allows you to start typing Python code which will be evaluated immediately. In Question 4 we saw how to put statements into a file to use later on, as opposed to evaluating expressions immediately. Note that when you open or save your files in IDLE, you will need to navigate to your home directory, and then to the isys90088 folder you created in the previous step.

5...


Similar Free PDFs
Tutorial 1
  • 4 Pages
Tutorial 1
  • 2 Pages
Tutorial 1
  • 21 Pages
Tutorial 1
  • 11 Pages
Tutorial 1
  • 5 Pages
Tutorial 1
  • 2 Pages
Tutorial 1
  • 7 Pages
Tutorial-1
  • 1 Pages
Tutorial 1
  • 13 Pages
Tutorial 1
  • 2 Pages
Tutorial 1
  • 4 Pages
Tutorial 1
  • 4 Pages
Tutorial 1
  • 3 Pages
Tutorial 1
  • 2 Pages