Changes

Jump to: navigation, search

OPS435 Python Lab 1

1,744 bytes added, 9 May
LAB OBJECTIVES
= LAB OBJECTIVES =
:In advance of this lab, you will 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, including the setup the of '''python''' libraries and also 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>
<!-- Try to use tables and simplify the layers of WIKI to make top links less verbose to students -->
:Centos 7 - release 1161 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.
: Most of you will likely use the school's Windows machines with the Vmware hypervisor. It is also possible to use your own computer, but check with your professor before you do that.
::*'''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-16111810.iso:::*Download outside Seneca College: http://mirror.csclub.uwaterloo.ca/centos/7/isos/x86_64/CentOS-7-x86_64-EverythingDVD-17081810.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 that is used for this course, these lab will be using and referring to the current version of '''Centos7CentOS 7''' (Graphical Desktop) for our main Linux machine. When creating your VM, refer to the '''Details / Minimum Requirements''' section above for assistance. :#Install Centos7 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 provides the OPS435 student "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 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 consistent record of their Python scripting progress throughout their labs.}}</blockquote>
<!--<ol><li value="3" 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><source lang="bash">
mkdir -p ~/ops435/lab1/
cd ~/ops435/lab1/
=== 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).
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 /bin
ln -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:<source lang="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''':<source lang="bash">
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 shell to check your work for this section:<source lang="bash">mkdir -p ~/ops435/lab1/
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
:'''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="margin-left: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 1 - Common Ipython Commands and Features ===
:'''Perform the following steps:'''
:#Make sure you're using your regular user login and not using the root user for the following section.
:#To access the '''ipython''' shell, issue the following command:<source>
ipython3
= INVESTIGATION 3: WRITING PYTHON SCRIPTS =
: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 ===
====Creating the introductory "Hello World" Script====
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 Brian Kernighan. You will learn how to run the python script in the ipython3 python3 shell as well as learn how to run the python script from the bash shell.
:'''Perform the following steps:'''
:#Add the following line into your source code file:<source>
print()
</source>And run it from the command-line: <source>python3 ./testlab1a.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')
</ol>
=== PART 2 - Working with Variables Python Objects ===
:A variable 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 variable python object types in future labs.
==== String Variables 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 a variable an object and how to display contents stored in a string variablesobject.
:'''Perform the following steps:'''
:#Create 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:<source lang="python">
name = 'Thomas'
</source>
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.<source lang="python">
print('I have a friend named ' + name)
</source>
:#To gain practice, complete your python script 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>
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 "lab1b.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li></ol>
==== Integer VariablesObjects====
:Integer variables In Python, integer objects are used to store a 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 variableobject, perform math operations, and display the results.
:'''Perform the following steps:'''
:#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.<source lang="python">
num1 = 5
num2 = 10
</source>
:#You can print the values in those variablesinteger objects:<source lang="python">
print(num1)
print(num2)
</source>
:#Now we will make a new variable integer object and try some math:<source lang="python">
sum = num1 + num2
</source>This will add the values contained in the variables integer objects together, providing a sum. However you will note that there is no output. Let's inspect the new value:<source lang="python">
print(sum)
</source>Does this value look right? Are you sure?<br>
:#Now lets try printing this sum out with a string:<source lang="python">
print('The sum is: ' + sum)
</source>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:<source lang="python">
print('The sum is: ' + str(sum))
:#To gain practice, complete your python script with the following features:
:::* 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>
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 "lab1c.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li>
</ol>
:'''Perform the following steps:'''
:# Issue Try some of the following commands to see how they operate what happens in the ipython shellPython:<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, 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>
:'''Be able to answer any questions about the lab to show that you understood it!'''
<br>:'''For sections A & B:''' ::<brspan style=color:green;font-size:1.5em;">&#x2713;</span> Submit your output and Python scrips via Blackboard instead.
= LAB REVIEW =

Navigation menu