Changes

From CDOT Wiki
Jump to: navigation, search

OPS435 Python Lab 1

1,601 bytes added, 9 May
LAB OBJECTIVES
= LAB OBJECTIVES =
:In this lab, you will first select and install a current distribution of Linux to be used as in a host machineVirtual Machine on your external SSD. You will NOT be required to setup Virtual Machines for do all of your future work within this lab VM (that will be covered in a future labincluding quizzes!). You will then setup your Python scripting environment on your host machine. This environment , including the setup the of '''python''' libraries and will also include the setup of a user-friendly interactive Python environment called '''ipython'''. You will also install additional framework tools (such as '''git''', '''vim''', and '''tmux''') to be used in later labs.
:After selecting an appropriate text editor to use, then you will start to create '''Python''' scripts to learn basic operations such as: '''printing text''', '''using variablesobjects''', and performing '''math operations'''.<br><br>
= INVESTIGATION 1: INSTALLING LINUX VM=
<!-- Try to use tables and simplify the layers of WIKI to make top links less verbose to students -->
:Centos 7 - release 1810 will be used for this course. The purpose for this is to allow you to use a consistent and stable '''RedHat''' based OS, and to lower the amount of new linux distros that you need to learn.
:Since Python runs <u>independently</u> regardless Most of you will likely use the of school's Windows machines with the Linux distributionVmware hypervisor. It is also possible to use your own computer, but check with your professor before you have some flexibility of which Linux OS to usedo that.
:The stable version of Centos 7 - release 1161 will be chosen(tested) to be supported for this course. This is to keep the ops stream on red hat based systems, lower the amount of new linux distros that need to be learned by students. This should be the default choice for this course, as it allows for the course to run longer before getting outdated with new software and updates. :Centos 7 comes with python 2.7, which means that it is not optimal out for the box for teaching this coursewe will be primarily using Python3. However , not teaching practicing python 2.7 would be a mistake , since so many programs and operating systems still depend on python 2Python2. It would be good to is recommended that students note some changes general improvements/differences in Python2 and encourage new work to be done in python3 while paying attention to specific projects that work only with python 2Python3.
:'''<u>Centos 7 VM Details / Minimum Requirements:</u>'''
::*'''Name:''' c7hostcentos7
::*'''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-16111810.iso:::*Download outside Seneca College: http://mirror.csclub.uwaterloo.ca/centos/7/isos/x86_64/CentOS-7-x86_64-EverythingDVD-16111810.iso::*'''Disk space:''' XGB 50GB ::*'''CPUs:''' X4 (Number of processors: 1, Number of cores per processor: 4)::*'''Internal Memory:''' X8GB::*'''Environment:''' GNOME Desktop
<!--==== Environment Setup ====
:'''Perform the following steps:'''
:#Regardless of the Linux distribution you decide to use that is used for this course, this these lab will be using and referring to the current version of '''Centos7CentOS 7''', and install the (Graphical Desktop ) for our host main Linux machine. When la creating your VM, refer to the '''Details / Minimum Requirements chart displayed ''' section abovefor assistance. :#Install CentOS 7 VM selecting the default partitions. When you have finished the installation of Centos7CentOS 7, you may proceed to the next step. <blockquote style="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.}}</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><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>
<li value="3" style="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.<br><br></li><li style="margin-left:25px;">Make notes for all of your Investigation 1 (part1) observations in your lab log book, and proceed to part 2.</liol></ol-->
=== PART 2 - 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>
::You will be required to use a text editor in order to create and modify your Python scripts. There are many text editors that provide various features to become more comfortable and productive during your Python coding sessions. ::Below is a listing of several common text editors and their features.<blockquote style="margin-left:35px;">{{Admon/tip|style="padding-left:25px"|Comparison of Common Selecting an Appropriate Text EditorsEditor|'''VimGeany''' (recommended text editor for labs)<br>A simple graphical text editor for developers. It is available in the CentOS package repositories.<br>'''Vim'''<br>vim is a powerful text editor for system administration and programming tasks. All of the shortcuts and commands you've learned over the years will help you edit programs efficiently. Vim can be modified to become a full programming environment with all the necessary features.<br><br>'''AtomSublime'''<br>This Another powerful and popular text editor is easy to use and contains powerful tools that comes with everything your need immediatelydesigned for programming. Atom allows for deep customisation from everything from complete functionality changes to just changing the themeIs not free or open source.<br><br>'''Sublime'''<br>Insert discussion here}}</blockquote>
<ol><li value=6" style="margin-left:25px;">Installing vim(Vi IMproved) will give us syntax highlighting and allow for advanced customization for terminal editing:<source>yum install vim-common vim-enhanced # Install vim </source></li><li style="margin-left:25px;"> '''Python pip ''' is a package manager specifically for Python. While it is usually not recommended to install software outside of '''dnf ''' or '''yum''', sometimes the only way to get a specific or latest version will be through pip:<sourcelang="bash">
yum install python-pip # Install python2.7 pip
yum install python34-pip # Install python3.4 pip
</source></li>
<li style="margin-left:25px;">Upgrade python34-pip with the following command:<source lang="bash">pip3.4 install --upgrade pip</source></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?|In the dictionary, "git" 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 Our primary use for git is primarily found when it's used 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 will be for backing up your code backup onto to the internet. Check out '''bitbucket ''' for a free private code repository.}}</blockquote>
<ol><li value="98" 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>
<li style="margin-left:25px;">Issue 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.</li></ol><br><br>
<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</pre></li><li style="margin-leftINVESTIGATION 2:25px;USING THE ">Before moving on to the next step make sure you identify any and all errors in IPYTHON"lab1a.py". When the check script tells you everything is "ok", you may procede to the next step.<br><br></li><li styleCLI ="margin-left:25px;">Make notes for all of your Investigation 1 (part1) observations in your lab log book, and proceed to investigation 2.</li></ol>
:'''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 35px;"IPYTHON>{{Admon/important|style=" CLI =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>
:IPython is an interactive environment that allows us to run python code line by line as we write it. This will function almost exactly like a bash shell prompt, enter a command and recieve the output back. However the commands that we will be running are lines of python code. Using this method we will start creating scripts out of the code we build in the IPython environment.
=== PART 1 - Common 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 courses. Throughout these labs the term command can also refer to these "magical functions". 
:'''Perform the following steps:'''
:#Make sure you're using your regular user login and not using the root user for the following section.:#To get into access the '''ipython ''' shell type, issue the following command:<source>
ipython3
</source>Now we are now inside the IPython environment. We can run some basic bash commands in here, within this is done through shell by using issuing IPython magic functions. <br>
:#Lets try a few commands out now:<source>
% ls% pwd% cd ~/% ls</source>Now hold on. '''Remember:''' You are not using Python here. These , instead, these are aliases, for Bash shell commands that IPython gives you access to. What you are actually using is bash, but not all bash commands are available in the IPython environment.<br><br>
:#Lets find out which ones are available, type the following command into the IPython shell:<source>
% alias</source>We should now have a list of all the bash commands available in IPython. Shortly we will go over how to add new bash commands into this environment, but you must remember, these are only here to assist in your python scripting, we are not here to learn bash commands.<br><br>:#Next lets add a new bash command that seems to be missing from this list:<source>%alias vim vim</source>:#The vim command will give us our much needed syntax highlighting, while we are editing scripts from within the IPython environment. These magic %alias functions do not save in between sessions, this creates a problem since you would have to create them every time you start IPython. This will create a error:<source>exitipython3%vim</source> You should be seeing an error telling you invalid syntax. This is happening because we need to create a config file to make this alias persist in-between sessions.<br><br>:#Exit your current IPython session:<source>exit</source>:#Now, create a new file and add the following content to it:<source>vim ~/.ipython/profile_default/startup/00-alias.ipy </source>:#Place our alias inside:<source>%alias vim vim</source>:#Save and quit the file. Now lets return to our IPython shell and confirm that our alias is available right away:<source>ipython3%vim</source><br>At this point vim should open successfully and you should now understand how to create new IPython aliases and store them persistently. Use these aliases to customize your environment with any bash commands you thing IPython is missing.<br><br>:# Exit vim now and head back to the IPython shell. <br><br>
:#Lets setup a directory structures for completing and organizing labs. These should be the locations to store your lab scripts.<source>
%mkdir ~/ops435%mkdir ~/ops435/lab1% mkdir ~/ops435/lab2% mkdir ~/ops435/lab3% mkdir ~/ops435/lab4% mkdir ~/ops435/lab5% mkdir ~/ops435/lab6% mkdir ~/ops435/lab7% mkdir ~/ops435/lab8
</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 resource will appear OVERWHELMING (a huge amount of information)! As we move throughout this course, you will slowly use different magic functions from here, but we will never use all of them. 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.
<ol><li value="6" style="margin-left:25px;">Issue the following command to exit your ipython session:<source> exit</source></li>:This should show you a OVERWHELMING amount In the future see if '''ctrl-d''' works instead of information, as we move through issuing the course we 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 exit command to interactively inspect quit the Python we are writing and running. Lets move on for nowipython shell.</li>
<ol><li value="13" style="margin-left:25px;">Perform the following steps to evaluation evaluate this unit. Running this evaluation script will give you an error, read this error message to see if you can figure out why. This is happening because we are checking a lab script file that we HAVE NOT created yet, so these errors are ok for now. But run Run these check scripts regularly as you work through the labs, they may give you hints if you get stuck. </li>:<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 lab1alab0c</presource></liol><li style="margin-left:25px;"br>Make notes for observations in your lab log book, and proceed to investigation 3.</li></olbr>
= INVESTIGATION 3: WRITING PYTHON SCRIPTS =
:During In this investigation we will start writing our very first python scripts. These will be very basic and help us practice syntax and foundation skills, such as: outputting text to the screen, storing data inside variablesobjects, and using math operators. <blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Object and Variable|The terms '''object''' and '''variable''' are used to refer to a data item in computer programming. The term '''variable''' are mostly used in 3rd generation programming/scripting languages (e.g. bash) while the term '''object''' is preferred in object oriented programming/scripting languages like Python. In the context of Python programming/scripting, When you see the term '''variable''' in the lab instructions, it can be replaced by the term '''object'''.}}</blockquote> 
=== PART 1 - Common Introductory Python Functions ===
====Printing TextCreating the introductory "Hello World" Script====
Lets start IPython interpreter You will learn to create a simple python script in this section. This python script will just print the text "hello world". The "hello world" an old traditional first program students usually are taught to create, which is based on the first programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and start writing some Brian Kernighan. You will learn how to run the python script in the python3 shell as well as learn how to run the python codescript from the bash shell.
:'''Perform the following steps:'''
:'''Perform the following steps:'''
:#Issue the following commands:<source>ipython3%cd Create a new python file in your ~/ops435/lab1%pwd%ls</source>Our directory. Call it lab1a.py. The first python 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.<br><br>:#At the prompt, issue Add the following python functionline 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 happened when is printed even though we ran this called the "print()" function. This is because we didn't pass any arguments to it, lets try again.<br><br>:# Issue the following functionModify your call to print() to inlcude an argument ('hello world'):<br><source>
print('hello world')
</source>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>:# Note that there are similarities between the Python print() function and the Bash echo command, but Python is more picky than bash (which is a good thing). Try to run print without the brackets or without the quotes to see what happens.<blockquote style====Creating the introductory "Hello Worldmargin-left:35px;" Script==== Next, we will make our first script with >{{Admon/tip|Reading errors|One of the above function. Open things that makes a new text file called "lab1agood programmer is debugging skills.py"The first and most important debugging technique is reading and understanding error messages:'''Perform Try to understand what the following steps:''' :# Edit errors are saying even if you think you already know what the following file:problem is and already have some idea about how to fix it.}}<source/blockquote>%vim ~/ops435/lab1/lab1a.py</sourceol> <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. <sourcelang="python">
#!/usr/bin/env python3
print('Hello world')
</source>
<li style="margin-left:#Save the file and quit vim. We will now go over the process 25px;">Another way of manually running this python scripta pythion program is executing it directly, e.g.: <source>./test. Both in py</source>Note that the Bash shell and in the IPython shellfile will need execute permissions even though you ran it just fine earlier.Why is that? <li style="margin-left:#Now lets try running 25px;">Download the check script directly and check your work. Enter the following commands from the IPython bash shell by issuing the follwoing commands:.<sourcelang="bash">ipython3%run cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls lab1a.py</source><br><br>Your python script should have run, if #confirm that you have any errors you should check that you typed the lab1a.py script in exactlyyour directory ls CheckLab1. Be careful of extra spaces, symbols, letters, or lowercasepy || wget https://uppercase differencesraw.githubusercontent.<br><br>:#Exit out of IPythoncom/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1. py:#Now, from the Bash shell we will give it the correct linux permissions and run it python3 . This is just showing the multiple ways you can use this python script/CheckLab1. You are not required to have IPython running on a system, however hopefully we can use IPython's powerful features to our advantage.:# Issue the following command to exit your ipython session:<source>exitpy -f -v lab1a
</source>
:# Run your python script by issuing the following commands:<source>chmod 755 ~/ops435/lab1/lab1a.pypython3 ~/ops435/lab1/lab1a.py</source>li style="margin-left:#Download the check script and check your work. Enter the following commands from the bash shell.<source25px;">cd ~/ops435/lab1/pwd #confirm that you are in the right directoryls lab1a.py #confirm that you have the lab1a.py script in your directoryls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.pypython ./CheckLab1.py -f -v lab1a</source>:#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.:#Make notes for observations in your lab log book, and proceed to part </ol> === PART 2.- Working with Python Objects ===
=== PART 2 - Working with Variables ===: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.
A variable is used to store data for use later in the program. This data can be a string, integer, decimal, etc. We will only be covering string and integer variable types in this lab. You will learn and use other variable types in future labs.==== String Objects ====
==== :String Variables ====objects contain text to be used in your program. Examples of strings could be user-names, full-names, item descriptions, etc. We will now demonstrate to assign a string to an object and how to display contents stored in a string object.
:'''Perform the following steps:'''
:#First, letCreate a python script (called lab1b.py) and first - start with a few simple things to try: :#Let's make a new variable object containing a value by issuing the following command:<sourcelang="python">name = 'Thomas'
</source>
:#Iss Print the following command value to inspect the value of the variable"screen:<sourcelang="python">print(name)
</source>
:#Print the value to the screenThink about why this does something different:<sourcelang="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.<sourcelang="python">
print('I have a friend named ' + name)
</source>
:#To gain practice, create a complete your python script (called lab1b.py) with the following content and details:::::* 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?"
::::*Sample run: <source>
% cd ~/ops435/lab1/%run ./lab1b.py How old are you Isaac?
</source>Try the checking script as you are working through a script to sometimes get hints.<br><br>
<ol><li value="67" style="margin-left:25px;">Download and run the checking script. Enter the following commands from the '''bash shell''':<sourcelang="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
</source></li>
<li style="margin-left:25px;"> Before proceeding, make certain that you identify any and all errors in "lab1alab1b.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li></ol>
==== Integer VariablesObjects====
Lets enter into IPython :In Python, integer objects are used to test out variablesstore an integer numbers that can be used for mathematical operations (discussed in the next section). Integers do NOT contain decimals, and they can be signed (+ or -) or unsigned. Here we will store integers in a object, perform math operations, and display the results.
:'''Perform the following steps:'''
:#Enter the following command Create a python script (called lab1c.py) and first - start with a few simple things to access the python prompttry:<source>ipython3</source>:#Lets create some new variables objects to play with.<sourcelang="python">
num1 = 5
num2 = 10
</source><br>In IPython we can inspect these variables by just typing the name of the variable. But in a python script this will not provide any output. This feature is useful however for debugging.<br><br>
:#Issue the following commands:<source>
num1
num2
</source>
:#You can print the values in those integer objects:<source lang="python">print(num1)print(num2)</source>:#Now we will make a new variable integer object and try some math:<sourcelang="python">
sum = num1 + num2
</source><br>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.<br><br>:#Enter the following command:<source>sum</source><br>Does this value look right? If we wanted to print this out to the screen we could use the following lang="python code.<br><br>:#Enter the following function:<source">
print(sum)
</source>Does this value look right? Are you sure?<br>:#Now lets try printing this sum out with a string:<sourcelang="python">
print('The sum is: ' + sum)
</source><br>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:<sourcelang="python">
print('The sum is: ' + str(sum))
</source><br>What did you notice this time?<br/>:#To gain practice, create a complete your python script (called lab1c.py) with the following content and detailsfeatures:
:::* 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: <source>
%cd ~/ops435/lab1/%run ./lab1c.py
Isaac is 72 years old!
</source><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:<sourcelang="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 lab1c
</source></li>
<li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1alab1c.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li><li style="margin-left:25px;">Make notes for observations in your lab log book, and proceed to part 3.</li></ol>
=== PART 5 - MATH OPERATORS ===
Python has :In the previous section, you performed a number couple of math operators simple mathematical operations. In this section, you can use in your programswill learn some additional mathematical operations.
:'''Perform the following steps:'''
:# Issue Try some of the following commands to see how they operate what happens in the ipython shellPython:<sourcelang="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:<sourcelang="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, create a python complete your script (called lab1d.py) with the following content and details:
::* The script should have a Shebang line.
::* The variable object '''x''' should contain a integer with the value '''10'''::* The variable object '''y''' should contain a integer with the value '''2'''::* The variable 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/%run ./lab1d.py
10 + 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 checking script. Enter the following commands from the bash shell:<sourcelang="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 lab1d</source>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.<br><br></li><li style="margin-left:25px;">Make notes for observations in your lab log book, and proceed to Lab 1 Instructor Sign-off.</li>
</ol>
<br><br>
= LAB 1 SIGN-OFF (SHOW INSTRUCTOR) =
:'''Have Ready to Show Your Instructor:'''
::<span style="color:green;font-size:1.5em;">&#x2713;</span> xOutput of: <code>./CheckLab1.py -f -v</code>::<span style="color:green;font-size:1.5em;">&#x2713;</span> xOutput of: <code>cat lab1a.py lab1b.py lab1c.py lab1d.py</code> :'''Be able to answer any questions about the lab to show that you understood it!'''<br>:'''For sections A & B:''' ::<span style="color:green;font-size:1.5em;">&#x2713;</span> Lab1 logbook notes completedSubmit your output and Python scrips via Blackboard instead. = LAB REVIEW = :# Write the command to change the hostname of your Linux machine to '''centos7'''.:# What is the purpose of '''git'''? How will git be used in our OPS435 course?:# Write the command to create an '''alias''' for the Linux command vim which will be called vi in your ipython3 session.:# Write the absolute pathname for the ipython3 alias configuration file.:# Write Python code that when run, will perform the following tasks:<ol type="a"><li>Contain a she-bang line</li><li>Display a greetings message to the user</li><li>display an empty line ('''hint:''' use the special character '''\n''' to print the a new-line character)</li><li>Display text, '''"Your current directory is:"''' (You are NOT required to display quotation marks)</li><li>Display the current working directory pathname (using an appropriate command)</li><li>Display another empty line</li></ol>:# How do you execute a Python script when you are within the <u>ipython3</u> shell?:# How do you execute a Python script when you are in the <u>Bash</u> Shell (i.e. NOT within the Ipython3 shell)?:# Write the pipeline command to check if the CheckLab1.py checking script exists, and download it from the location:<br>https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py [[Category:OPS435-Python]]

Navigation menu