Lab 0 (Jan 9_12) - 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:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 1/9 Computer Science 1001 - Lab 0 Chapter 1 Basic Information for Linux Users This lab discusses: logging on to the system the Linux operating system organizing your work on your computer downloading the files for the lab using the programming environment preparing for the next lab logging off 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. Readings Objectives Notes Lab Exercises 1. Lab setup. Each lab contains exercises to be completed prior to or during the lab period. You should read and study these exercises and any code provided for them before the lab session. Also write out as much as you can of your solutions before the lab; then lab time can be devoted to completing your solutions and making any necessary fine tuning adjustments.
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 2/9 The PCs in the lab room on campus are configured to run with the Linux operating system. For the COMP1001 labs, Linux , a free implementation of UNIX for personal computers, is the system of choice. Linux allows multiple windows in operation at the same time, each running a different application. The labs for COMP1001 use three principal applications: the Google Chrome web browser, a shell (terminal) window, and the Python Integrated DeveLopment Environment (IDLE). All of these applications are run on top of a window management system. The window management system presents a desktop environment for the launching of applications and for the display of the user files. Quick access to the applications is provided by the panel to the left on the screen, which is accessible by clicking on Activities in the top left. This environment can also be set up to provide different workspaces. A shell window (often referred to as the command prompt ) may be used to manage directories and files on your disk space and to run Python or IDLE . In order to start a shell window, click on the Activities button in the top left- hand corner of the screen, then start typing "terminal" in the search box. Click on Terminal (probably the first option on the left, or one of the other terminal options). The terminal may also be available from the panel on the left. Alternatively, simultaneously touch the Alt and F2 keys on your keyboard, then, when the Enter a command dialog box appears, type gnome- terminal and touch the Enter key to select the default choice --no mouse required! In order to start Google Chrome , click on the Activities button in the top left-hand corner of the screen, then start typing chrome and click on Google Chrome . (You may need to cancel a pop-up window for the login keyring, several times.) Note: If you have any problems during any of these steps, please feel free to ask one of the lab assistants for help. 2. (Optional) Installing Python 3 at home. You may also want to install Python on your home computer so that you can work on programs offline. Follow the instructions for Installing Python 3 to install Python (including IDLE) at home. 3. (Optional) Remote access to your files. You can also connect to departmental servers remotely to access files from your MUN account. Follow the instructions for Remote Access to Linux from Off Campus to install software on your home machine to connect to a server on campus. 4. Linux commands.
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 3/9 A shell window gives you complete access to all the Linux commands. A command line interpreter is running in the shell window, waiting to carry out your commands. If you need to connect to our servers remotely, knowing some Linux commands may become useful. A few common commands which are useful for COMP1001 are shown in the following table. Use those commands to do the exercises below: Some Linux Commands Command Action mkdir dir creates directory dir cd dir makes dir the current directory pwd shows full path to current directory rmdir dir removes directory dir , provided it is empty ls lists contents of current directory command --help displays some info about command cp file1 file2 copies file1 to file2 mv file1 file2 renames file1 as file2 rm file (irreversibly) removes file ; use the -i option, e.g. rm -i file , to get a displayed prompt confirming removal python3 or sometimes just python invokes the Python 3 interpreter (just using "python" may invoke version 2 - or earlier - depending on the version of Linux you are using) idle runs the Python Interactive DeveLopment Environment ( idle & runs it in the background and shows the command prompt again) ssh machinename make this a secure shell window on machinename Start a shell window and terminate it by typing exit at the command prompt. Start a shell window and terminate it by clicking its Close Button --the X in the window's title bar. Start a shell window. Your home directory has a longer name, but it is also known to Linux as ~ . Make sure your current directory is your home directory by giving the command cd ~ and checking the result with the pwd command. Try the command mkdir --help . Create a subdirectory called comp1001 in your home directory to store all of your course files. make sure you are in your home directory by typing: cd ~ type the command: mkdir comp1001 Create a subdirectory called lab00 in your comp1001 directory to store your Lab 0 files, and go into that directory. change into your comp1001 directory by typing: cd comp1001 type the command: mkdir lab00 make ~/comp1001/lab00 your current directory by typing: cd lab00 Download the source code file for this lab, available here , and save the file to your ~/comp1001/lab00 folder. If the file saves automatically to your Downloads folder, you may need to use the following command to move the file to your lab00 folder:
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
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 4/9 type: mv ~/Downloads/FirstTurtle.py ~/comp1001/lab00/FirstTurtle.py In addition to ~ , some other useful abbreviations in pathnames are . and .. . represents the current working directory .. represents the parent directory of the current working directory move back to the parent directory of your current directory by using the command: cd .. 5. Understanding some of the common Linux commands. Given the directory structure below, answer the following questions (ask the lab assistants for help if you need it!): 5.1 Choose the single Linux command to remove the file called file1 if your current directory is ~/cs2700 . ! 1. rm ~/cs2700/assign2/part1/file1 2. rm assign2/part1/file1 3. rm file1 4. rm ~/part1/file1 5. either 1 or 2 5.2 Choose the single Linux command to change into the directory lab2 if your current directory is ~/ . ! 1. cd ~/notes/lab2 2. cd cs2700/lab2 3. cd cs2700/notes/lab2 4. cd ~/lab2
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 5/9 Check My Answers Reset Dismiss Results 5. cd cs2700/project/notes/lab2 5.3 Choose the single Linux command to move the file advi into the directory lab1 if your current working directory is ~/cs2700/notes . ! 1. mv lab2/advi lab1 2. mv notes/lab2/advi notes/lab1 3. mv advi lab1 4. mv lab2 lab1 5. mv lab1 lab2/advi 5.4 Choose the single Linux command to create a new subdirectory called temp inside the project directory if your current working directory is ~/ . ! 1. mkdir ~/cs2700/project/temp 2. mkdir temp 3. mkdir cs2700/project/temp 4. mkdir ~/temp 5. either 1 or 3 5.5 Choose the single Linux command to create a new subdirectory called temp inside the lab3 directory if your current working directory is part1 . ! 1. mkdir temp 2. mkdir ~/cs2700/notes/lab3/temp 3. mkdir ../../notes/lab3/temp 4. mkdir ~/temp 5. either 2 or 3 6. either 2 or 4 6. Wildcard characters. When using shell commands, it is sometimes useful to use wildcard characters . The wildcard characters * and ? may be used when you wish to perform commands on multiple files or directories at one time. The character * can be used to substitute for any number of characters in a file or directory identifier. The character ? can be substituted for exactly one character in a file or directory identifier. For example, if you have a directory ~/temp which contains a number of text files (with a .txt extension) you could use the
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 6/9 Check My Answers Reset Dismiss Results wildcard character * with one copy command to copy them all to the directory ~/comp1001/lab00 at one time as follows: cp ~/temp/*.txt ~/comp1001/lab00 . If you also have, in the same directory, the files can.doc , Fan.doc , ran.doc and tran.doc , and if you only wanted to copy the first three files to your lab00 directory, you could use the command: cp ~/temp/?an.doc ~/comp1001/lab00 and only the files with exactly one letter followed by an.doc would be copied. Given the directory listing below, answer the following questions: after.p listing telnet transfig angle printer.c term.new unpackmaps ankle.c Program.p tex unzip answers.p tcpip tipx unzip-4.01 help.tex tcsh ts uuencode 6.1 How many files would be listed by giving the ls *.c command? ! 6.2 How many files would be listed by giving the ls a*.* command? ! 6.3 How many files would be listed by giving the ls t?p* command? ! 6.4 How many files would be listed by giving the ls ????.??? command? ! 7. Become familiar with typing commands in the Python shell (interactive/immediate mode). Python is an interpreted language so one can type Python expressions into the Python shell window and the interpreter immediately shows the result. The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. To practice with typing commands in the Python shell, complete the questions below. 7.1. Practice with Python expressions. 7.1.1 Type each of the following expressions into the Python shell and press Enter . Record the results displayed by the Python
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
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 7/9 Check My Answers Reset Dismiss Results interpreter and consider why this output is produced. Expressions and statements will be covered further in the next lab. Expression Result 2 / 5 2.0 / 5 2.0 // 5 4 % 3 20 + 3 * 5 (20 + 3) * 5 2 ** 3 'Hi' + 'there' 'Hi' + '5' 'Hi' * 5 print('Hi there') 7.2. Practice with invalid Python expressions. Type each of the following expressions into the Python shell and press Enter . These are invalid expressions so the interpreter will show error messages. Read the error messages and click on the Show Solution button below to see the corrected expressions. #Invalid Expression 1 pint ( 'Hello World!' ) #Invalid Expression 2 'Hi' + 5 #Invalid Expression 3 1 2
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 8/9 #Invalid Expression 4 print ( hello ) #Invalid Expression 5 'hi' * 'bye' #Before trying the next invalid expression, first assign #the values, 3 and 4, to variables, a and b, respectively, #as follows: a = 3 b = 4 #Invalid Expression 6 c = d * a #Invalid Expression 7 2 / '5' #Invalid Expression 8 print 'Hi' #Invalid Expression 9 2 / 0 #Invalid Expression 10 Print ( 'Bye' ) Show Solution 8. Open and execute a Python program from IDLE (script mode). Working directly with the Python interpreter is only appropriate to test short fragments of code. Anything longer than a couple lines of code should be put into a program (script). Click on the File option in the IDLE menu and then click on Open . Select the file that you downloaded above. Click on the Run option in the IDLE menu and then click on Run Module . What does this program do? 9. Create and execute a Python program within IDLE. Click on the File option in the IDLE menu and then click on New File . Type the following code in the window #Calculate the area of a circle from math import * radius = input ( "Enter the radius of the circle: " ) radius = float ( radius ) area = pi * ( radius ** 2 ) print ( "The area is" , area )
4/1/24, 8:26 AM Computer Science 1001 - Lab 0 https://online.mun.ca/d2l/le/content/567611/viewContent/5087542/View 9/9 Save your program by choosing Save As... from the File menu. Select your lab00 folder as the location and name your file: calc_area.py When a file is created/modified it should be saved to a directory within your home directory, regardless of which editor was used to create the file. The saved file will exist in the specified directory in the file structure. When using machines in the lab, any machine will give you access to your file structure. Thus this saved file can be accessed or modified from any machine in the lab or any machine where you can access your LabNet home directory. Run the program by clicking on the Run option in the IDLE menu and then Run Module . Test your program and make sure that it behaves as you would expect. If you use radius 5.5, you should see the following output to your Python shell window: Enter the radius of the circle: 5.5 The area is 95.03317777109125 10. Preparing for the next Lab. There are generally two components to the COMP1001 labs: lab exercises and evaluation. (Note that there is no evaluation for Lab 0.) You should complete any pre-lab exercises and as much of the lab exercises as possible prior to your assigned lab slot. During the assigned lab time, you can work on and complete the lab exercises and ask questions. The lab evaluation will consist of a quiz that is written within the final 50 minutes of the lab. The quiz will consist of multiple choice questions and one or more longer questions. Preparation for the lab is essential. 11. Logging out of the System. Once all of your files have been saved, exit any windows that you may have open. In order to close Chrome (or other similar windows), click on the X in the upper corner. In order to close the shell window, you may also just type exit at the prompt. You are now ready to logout of the system. In order to do this, move the mouse to the menubar at the top of the screen and click on the power icon in the top right-hand corner; then click on your name, then on the Log Out option (do NOT click on the Switch User option, if that option is available) and then click on the Log Out button. Author: Department of Computer Science, Memorial University of Newfoundland OC-BE240108
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