1,760
edits
Changes
no edit summary
<font color= Lab Preparation 'red'>'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''</font>= LAB OBJECTIVES =
:In this labAfter selecting an appropriate text editor to use, then you will first select and install a current distribution of Linux to be used as a host machine. You will NOT be required start to setup Virtual Machines for this lab (that will be covered in a future lab). You will then setup your Python scripting environment on your host machine. This environment setup will include a user-friendly interactive Python environment called create '''ipythonPython''' plus other tools (scripts to learn basic operations such as : '''printing text''', '''gitusing objects''', etc) to be used in later labsand performing '''math operations'''.<br><br>
= INVESTIGATION 1:After selecting an appropriate text editing to use, then you will start to create '''Python''' scripts to learn basic operations such as:INSTALLING LINUX VM=
:Since Python runs '''<u>independentlyCentos 7 VM Details / Minimum Requirements:</u> regardless the of the Linux distribution, you have some flexibility of which Linux OS to use. Below is a table displaying the characteristics of the Centos vs Fedora distributions and related Python packages.'''
::*'''Name:''' centos7
::*'''Boot media / Installation:''' CentOS7 Full Install DVD
:::*Download at Seneca College: CentOS 7 Full Install DVD (image file): http://belmont.senecacollege.ca/pub/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
:::*Download outside Seneca College: http://mirror.csclub.uwaterloo.ca/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
::*'''Disk space:''' 50GB
::*'''CPUs:''' 4 (Number of processors: 1, Number of cores per processor: 4)
::*'''Internal Memory:''' 8GB
::*'''Environment:''' GNOME Desktop
<!--==== Environment Setup ====
:'''Perform the following steps:''' :#Regardless of the Linux distribution you decide to use for this course, this lab will be using and referring to the current version of '''Centos7''', and install the Graphical Desktop for our host machine. When you have finished the installation of Centos7, you may move on to the next steps.
:#Regardless of the Linux distribution that is used for this course, these lab will be using and referring to the current version of '''CentOS 7''' (Graphical Desktop) for our main Linux machine. When creating your VM, refer to the '''Details / Minimum Requirements''' section above for assistance. :#Install CentOS 7 VM selecting the default partitions. When you have finished the installation of CentOS 7, you may proceed to the next step.<blockquotestyle="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Unit Evaluation Feedback Scripts|Each '''Part''' (within an 'Investigation) is referred to as a '''Unit'''. Each Unit will require that the student download and run a '''Unit Evaluation Feedback Script''', which can be used to look at a provides the OPS435 student's Python scripts that was created in order to provide "real-time feedback"of their completed work.<br>This feedback is not considered to be perfect or fool-proof; however, it may provide feedback (hints) in case a student gets stuck or experiences an error when running thier performing administration tasks or when creating their Python scripts. These unit evaluation feedback scripts can also be used to confirm that the student's Python script is on the right track, and provide a consist consistent record of their Python scripting progress throughout their labs.<br><br>}}</blockquote>
<!--<ol><li value="23" style="margin-left:25px;">Issue the following commands (as a regular user) in order to setup, download and run the first unit evaluation feedback script:<br><br><sourcelang="bash">
mkdir -p ~/ops435/lab1/
cd ~/ops435/lab1/
pwd # <-- i.e. confirm that you are in the correct directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython python3 ./CheckLab1.py -f -v lab0a
</source></li>
<listyle="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1a.py"from the check script. <br>When the check script tells you everything is "ok" for "lab0a", you may proceed to the next step.</libr><libr>Make notes for all of your Investigation 1 (part1) observations in your lab log book, and proceed to the next section</li></ol>-->
=== Part PART 2 - SETTING UP YOUR PYTHON ENVIRONMENT FOR OPS435 LABS Setting up Your Python Environment For Labs ===
In order to learn how to use python on your Linux machine, it is important to setup your Linux environment and learn how to interact with a Python shell. The following should be done on a terminal as the superuser (i.e. root).
:'''Perform the following steps:'''
:#The first step is to update your entire system. <sourcelang="bash">
yum update
</source>
:#Install extra packages for enterprise linux:<sourcelang="bash">
yum install epel-release
</source>
:#Next install applications that we needrequired, first Python version 3 and version 2:<sourcelang="bash">
yum install python34 python34-devel # Install python3.4 and python3.4 development libraries
yum install python python2-devel # Install python2.7 and python2.7 development libraries
</source>
:#We now need to create a link to python3.4:<source lang="bash">cd /binln -s python3.4 python3</source>:#Next, you will install a couple of useful applications called '''tmux''' and '''screen'''. They are referred to as '''terminal multiplexers'''. If you plan to spend a lot of time in the terminal, this powerful tool will help you get it done. Lets install it and plan to use it later:<sourcelang="bash">
yum install screen tmux && ln -s /usr/bin/true /etc/sysconfig/bash-prompt-screen
</source>
:# You will now set Set your hostname to the Linux Distribution we are usingin case you did not set it correctly during the install process:<sourcelang="bash">
hostnamectl set-hostname centos7 # Set your hostname to distribution-name
</source>
yum install python-pip # Install python2.7 pip
yum install python34-pip # Install python3.4 pip
</source><br/li>In the dictionary, <li style="gitmargin-left:25px;" is defined as an unpleasant or contemptible person. In the IT industry on the other hand, '''Git''' refers to a version control system that allows you to track any changes made to files and programs. The benefit to using git is primarily found when it's used >Upgrade python34-pip with multiple people, sharing and working on code together. While that is not how we will be using it in this course, you may find some benefits in using it for managing multiple versions of the same program or for backing up your code onto the internet. Check out bitbucket for a free private code repositoryfollowing command:<source lang="bash">pip3.4 install --upgrade pip<br/source><br/li>#<li style="margin-left:25px;">Issue the following command to install '''git''':<sourcelang="bash">
yum install git # Install git command line tool
</source></li></ol>#<blockquote style="margin-left:35px;">{{Admon/tip|What is a Git?| '''Git''' refers to a '''version control system''' that allows you to track any changes made to files and programs. Our primary use for git in this course will be for backup onto to the internet. Check out '''bitbucket''' for a free private code repository.}}</blockquote> <ol><li value="8" style="margin-left:25px;">IPython will be one of the tools we will use the most. Lets install it. You will learn more about it in the next section:<sourcelang="bash">
yum install python-ipython # Install ipython for python2.7
pip3.4 install ipython # Install ipython for python3.4
</source></li>#Issue <li style="margin-left:25px;">Exit from the superuser shell to your regular user shell and issue the following commands in your Ipython shell to check your work for this section:<sourcelang="bash">mkdir -p ~/ops435/lab1/
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython python3 ./CheckLab1.py -f -v lab0apython3 ./CheckLab1.py -f -v lab0b</source></li>#<li style="margin-left:25px;">Before moving on to the next step, make sure you identify and correct any and all errors in "lab1a.pylab0b"output. When the check script tells you everything is "ok", you may proceed to the next step. === Part 3 - Selecting a Text Editor to Use For Your OPS435 Labs === There are a wide range of text editors for the python language and just about any of them will do. As long as you get basic syntax highlighting and automatic indenting out of the application you are good to go. This section will go over a number of different text editors, showing off a few different text editors so students may find their favourite. There are no wrong answers here, give them all a try and use your favourite. <table border="1" cellspacing="0" cellpadding="5" width="40%" ><tr><td width="150px">'''Text Editor'''</td><td>'''Characteristics'''</tdli></trol> <tr><td valign="top">'''Vim'''</tdbr><td valign="top">As a system administrator you have probably spent a ton of time inside vim. Well vim is just as powerful and useful when you get to programming, all the shortcuts and commands you've learned over the years will help you edit programs efficiently. On top of what you already know, it might be time to customize vim a little more for programming. Vim can actually be modified to become a full programming environment with all the features you yould expect.</td></tr><tr><td valign="top">'''Atom'''</td><td valign="top">"A hackable text editor for the 21st Century". This text editor is a powerful tools that comes with everything your need right out of the box. Atom allows for deep customization from everything from complete functionality changes to just changing the theme. Definitely worth checking out, especially for python development.</td></tr> <tr><td valign="top">'''Sublime'''</td><td valign="top">Sublime is a popular text editor with tons of customizations and themes.</td></tr><tr><td valign="top">'''Other Editors'''</td><td valign="top">x</td></tr></table> == Part 3 - Lab Check lab0c == <pre>cd ~/ops435/lab1/pwd #confirm that you are in the right directoryls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.pypython ./CheckLab1.py -f -v lab0c</prebr>
:'''IPython''' is an interactive environment that allows us to run python code line by line as we write it. This will also act like a bash shell prompt allowing users to enter a limited number of Bash shell commands. You will notice this by seeing an [IN] prompt where to enter commands and an [OUT] prompt that will display output from issued commands. We will start creating scripts out of the code within this '''IPython''' environment.<blockquote style= INVESTIGATION 2"margin-left: USING THE IPYTHON COMMAND LINE INTERFACE 35px;">{{Admon/important|style="padding-left:25px"|ipython and python|[https://plot.ly/python/ipython-vs-python/ What is the difference between '''python''' and '''ipython'''?] Please note that in later labs, you can use either '''python''' or '''ipythone''' as the interactive python shell.}}</blockquote>
=== Part PART 1 - Common IPython Ipython Commands and Features ===
==== Using Magic Functions ====
:Lets start with trying to run begin by running some python code in a the ipython interactive shell. This is a advanced python shell, similar to the bash shell that you have been using throughout the linux Linux courses. Throughout these labs the term command can also refer to these "magical functions".
:'''Perform the following steps:'''
ipython3
</source>
:#If you are interested in finding more information about other magic functions in IPython, try entering then within the IPython shell , and typing enter the following(press the letter '''q''' to exit this function):<source>% magic
</source>
:::This should show you resource will appear OVERWHELMING (a OVERWHELMING huge amount of information, as )! As we move through the throughout this course we , you will slowly use different magic functions from here, but we will never use all of them. To be continued. Magic functions than just running bash commands. They cover a huge range of different tasks, while we are writing code, allowing us to interactively inspect the Python we are writing and running. Lets move on for now. == Part 1 - Lab Check lab1a ==
<ol><li value="6" style="margin-left:25px;">Issue the following command to exit your ipython session:<source>
exit
</source>
</li>
:In the future see if '''ctrl-d''' works instead of issuing the exit command to quit the ipython shell.
</li>
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython python3 ./CheckLab1.py -f -v lab1alab0c</presource></ol><br><br>
:#Create a new python file in your ~/ops435/lab1 directory. Call it lab1a.py. The first Python code we will write is going to call the print function. A function is code that has been defined in another location. Functions can take arguments, use these arguments in some way, and then usually return a result. The first function we will use is the "print()" functions, it's sole purpose is to output information to the screen.<prebr><br>:#Add the following line into your source code file:<source>print()</source>And run it from the command-line: <source>python3 ./lab1a.py</source>You will notice that nothing is printed even though we called the "print()" function. This is because we didn't pass any arguments to it, lets try again.<br><br>:# Modify your call to print() to inlcude an argument ('hello world'):<br><source>
print('hello world')
</presource> This time we should now see that the python function "print()" has outputted to the screen the words 'hello world'. In python a word or a bunch of characters like 'hello world' is called a 'string'. So what we did In the above isexample, passed a '''string''' was passed as a '''argument''' to the print '''function'''. These words are important for understanding and talking about different aspects of code.<br><br> === Part 2 - Hello World === Next, we will make our first script with :# Note that there are similarities between the above Python print() function. Open and the Bash echo command, but Python is more picky than bash (which is a new text file called "lab1agood thing).py"Try to run print without the brackets or without the quotes to see what happens. <preblockquote style="margin-left:35px;">%vim ~{{Admon/ops435/lab1/lab1atip|Reading errors|One of the things that makes a good programmer is debugging skills. The first and most important debugging technique is reading and understanding error messages. Try to understand what the errors are saying even if you think you already know what the problem is and already have some idea about how to fix it.py}}</preblockquote> <ol><li value="5" style="margin-left:25px;">Write the following code into our python file. Note the she-bang line at the top of the file to run this script in the python3 environment. You will need to add this she-bang line for all python scripts you create for this course.<presource lang="python">
#!/usr/bin/env python3
print('Hello world')
</presource><li style="margin-left:25px;">Another way of running a pythion program is executing it directly, e.g.: <source>./test.py</source>Note that the file will need execute permissions even though you ran it just fine earlier. Why is that? <li style="margin-left:25px;">Download the check script and check your work. Enter the following commands from the bash shell.<source lang="bash"> cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls lab1a.py #confirm that you have the lab1a.py script in your directory ls CheckLab1.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1a</source><li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may proceed to the next step.</ol>
:In Python, an object is used to store data for use later in the program. This data can be a string, integer, decimal number, characters, etc. We will only be covering '''string''' and '''integer''' variables in this lab. You will learn and use other python object types in future labs.
print(name)
</presource>:#Think about why this does something different:<source lang="python">print('name')</source>:#Now lets try something new, we are going to print out the string and concatenate/combine it with another string. The plus sign can be used to join 2 strings together. However, make sure that your variable object is always outside the quotes, or it will not resolve to a value. <presource lang="python">
print('I have a friend named ' + name)
</presource> === Part 3 - Evaluation === Create a :#To gain practice, complete your python scriptwith the following content and details: lab1b.py::::* The script should have a '''Shebang line''' like you did for your lab1a.py python script::::* The script should use a single variable object called "name"::::* The value of the "name" variable object should be "Isaac"::::* The script, when executed, should print out "How old are you Isaac?" Example ::::*Sample run: <presource>% cd ~/ops435/lab1/%run ./lab1b.py How old are you Isaac?</presource> Try the check checking script as you are working through a script to sometimes get hints.<br><br> <ol><li value="7" style== Part 3 "margin- Lab Check lab1b === This course is designed with a unit testing suite, which can be used to look at the scripts you write left:25px;">Download and give real-time feedback. This feedback is not perfect, however it may offer some hints if you get stuck with a error. It can also be used to make sure you are on run the write track, and show progress. Download the check checking script. Enter the following commands from the '''bash shell.''':<presource lang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython python3 ./CheckLab1.py -f -v lab1b</presource></li><li style="margin-left:25px;"> Before proceeding, make certain that you identify any and all errors in "lab1b.py". When the check script tells you everything is "ok", you may proceed to the next step.</li></ol>
:#Create a python script (called lab1c.py) and first - start with a few simple things to try: :#Lets create some new variables objects to play with.<presource lang="python">
num1 = 5
num2 = 10
</presource> In IPython we :#You can inspect these variables by just typing the name of print the variable. But values in a those integer objects:<source lang="python script this will not provide any output. This feature is useful however for debugging. <pre">print(num1)print(num2)</presource> :#Now we will make a new variable integer object and try some math. :<presource lang="python">
sum = num1 + num2
</presource> This will add the values contained in the variables integer objects together, providing a sum. However you will note that there is no output. First lets Let's inspect the new value. :<pre>sum</pre> Does this value look right? If we wanted to print this out to the screen we could use the following source lang="python code. <pre">
print(sum)
</presource>Does this value look right? Are you sure?<br> :#Now lets try printing this sum out with a string.:<presource lang="python">
print('The sum is: ' + sum)
</presource> What happened? Did you receive an error? This will may have been the first time you've seen this error, but it won't be the last. What we tried to do is combine a string with a number, and this won't work. <br><br>In order to use display this number as a string we will use the "str()" function on it. The "str()" function will return a string of your number and provide it as a argument to "print()". This function will not change the value of your variableobject, your variable object is still a an intergerobject.<br><br> :# Issue the following:<presource lang="python">
print('The sum is: ' + str(sum))
</presource>What did you notice this time?<br /> === Part 4 - Evaluation === Create a :#To gain practice, complete your python scriptwith the following features: lab1c.py:::* The script should have a Shebang line.:::* The script should have a variable an object called '''name''':::* The script should have a variable an object called '''age''':::* The value of the '''name''' variable object should be '''Isaac''':::* The variable object '''age''' should contain a integer:::* The value of the '''age''' variable object should be '''72''':::* The script, when executed, should print out "Isaac is 72 years old!" :::Example run: <presource>%cd ~/ops435/lab1/%run ./lab1c.py
Isaac is 72 years old!
</presource><br>Try the check script as you are working through a script to sometimes get hints.<br><ol><li value=10" style="margin-left:25px;">Download and run the checking script. Enter the following commands from the bash shell:<source lang="bash">cd ~/ops435/lab1/pwd #confirm that you are in the right directoryls CheckLab1.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.pypython3 ./CheckLab1.py -f -v lab1c</source></li><li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1c.py". When the check script tells you everything is "ok", you may proceed to the next step.</li></ol>
:# Try some of the following to see what happens in Python:<source lang="python">print(10 + 5) # additionprint(10 - 5) # subtractionprint(10 * 5) # multiplicationprint(10 / 5) # divisionprint(10 ** 5) # exponents</source>NOTE: You must be careful when combining more complex math operators together. Python uses '''PEMDAS''' ('''P'''arentheses, '''E'''xponents, '''M'''ultiplication and '''D'''ivision, '''A'''ddition and '''S'''ubtraction) to resolve math.<br><br>:# Go over the below examples and see if you understand each situation:<source lang="python">print(10 + 5 * 2) # multiplication happens before additionprint((10 + 5) * 2) # parentheses happen before multiplicationprint(10 + 5 * 2 - 10 ** 2) # first exponents, then multiplication, then addition and subtraction from left-to-rightprint(15 / 3 * 4) # division and multiplication happen from left-to-rightprint(100 / ((5 + 5) * 2)) # the inner most parentheses are first performing addition, then parentheses again with multiplication, finally the division </source>:#To gain practice, complete your script with the following content and details:::* The script should have a Shebang line.::* The object '''x''' should contain a integer with the value '''10'''::* The object '''y''' should contain a integer with the value '''2'''::* The object '''z''' should contain a integer with the value '''5'''::* The script, when executed, should print out "10 + 2 * 5 = 20" (the printout should change if the values in the objects change):::Example run: <source>cd ~/ops435/lab1/./lab1d.py10 + 2 * 5 = 20</source>Try the checking script as you are working through a script to sometimes get hints.<br><br><ol><li style="margin-left:25px;" value="4">Download and run the check checking script. Enter the following commands from the bash shell.:<presource lang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython python3 ./CheckLab1.py -f -v lab1clab1d</presource> Before moving on to the next step make sure you identify any and all errors in "lab1alab1d.py". <br><br></li><li style="margin-left:25px;">When the check script tells you everything is "ok", you may procede proceed to the next step. === Part 5 - Math Operators === Python has a number of math operators you can use in your programs.<prebr><br>10 + 5 # addition10 - 5 # subtraction10 * 5 # multiplication10 </ 5 # division10 ** 5 # exponentsli></preol> But you must be careful when combining more complex math operators together. Python uses PEMDAS(Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) to resolve math. Go over the below examples and see if you understand each situation.<br><br>
=== Part 5 - Lab Check lab1d ==LAB REVIEW =