Difference between revisions of "OPS435 Python Lab 1"

From CDOT Wiki
Jump to: navigation, search
(PART 2 - Setting up Your Python Environment For Labs)
(PART 2 - Setting up Your Python Environment For Labs)
Line 84: Line 84:
 
</source>
 
</source>
  
<blockquote style="margin-left:35px;">{{Admon/tip|style="padding-left:25px"|Comparison of Common Text Editors|'''Vim''' (recommended text editor for labs)<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>'''Atom'''<br>This text editor is easy to use and contains powerful tools that comes with everything your need immediately. Atom allows for deep customisation from everything from complete functionality changes to just changing the theme.<br><br>'''Sublime'''<br>Insert discussion here}}</blockquote>
+
<blockquote style="margin-left:35px;">{{Admon/tip|style="padding-left:25px"|Selecting an Appropriate Text Editor|'''Vim''' (recommended text editor for labs)<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>'''Atom'''<br>This text editor is easy to use and contains powerful tools that comes with everything your need immediately. Atom allows for deep customisation from everything from complete functionality changes to just changing the theme.<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>
 
<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>

Revision as of 08:23, 17 May 2017

LAB OBJECTIVES

In this lab, you will first select and install a current distribution of Linux to be used as a host machine. You will NOT be required 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 the python libraries and will also include 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 variables, and performing math operations.

INVESTIGATION 1: INSTALLING LINUX VM

PART 1 - Installing Your Linux Distribution

Since Python runs independently regardless the of the Linux distribution, you have some flexibility of which Linux OS to use.
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 course. However not teaching python 2.7 would be a mistake since so many programs and operating systems still depend on python 2. It would be good to note some changes and encourage new work to be done in python3 while paying attention to specific projects that work only with python 2.


Centos 7 VM Details / Minimum Requirements:
  • Name: centos7
  • Boot media / Installation: CentOS7 Full Install DVD
  • Disk space: XGB
  • CPUs: X
  • Internal Memory: X


Perform the following steps:
  1. 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 la your VM, refer to the Details / Minimum Requirements chart displayed above. When you have finished the installation of Centos7, you may proceed to the next step.
Important.png
Unit Evaluation 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 Script, which can be used to look at a student's Python scripts that was created in order to provide "real-time feedback".
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 Python scripts. These unit evaluation scripts can also be used to confirm that the student's Python script is on the right track, and provide a consist record of their Python scripting progress throughout their labs.
  1. Issue the following commands in order to setup, download and run the first unit evaluation script:
    mkdir -p ~/ops435/lab1/
    cd ~/ops435/lab1/
    pwd  #   <-- i.e. confirm that you are in the correct directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab0a
  2. 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.

  3. Make notes for all of your Investigation 1 (part1) observations in your lab log book, and proceed to part 2.

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.


Perform the following steps:
  1. The first step is to update your entire system.
    yum update
  2. Install extra packages for enterprise linux
    yum install epel-release
  3. Next install applications we need, first Python version 3 and version 2:
    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
  4. 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:
    yum install screen tmux && ln -s /usr/bin/true /etc/sysconfig/bash-prompt-screen
  5. You will now set your hostname to the Linux Distribution we are using in case you did not set it correctly during the install process:
    hostnamectl set-hostname centos7 # Set your hostname to distribution-name
Idea.png
Selecting an Appropriate Text Editor
Vim (recommended text editor for labs)
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.

Atom
This text editor is easy to use and contains powerful tools that comes with everything your need immediately. Atom allows for deep customisation from everything from complete functionality changes to just changing the theme.

Sublime
Insert discussion here
  1. Installing vim(Vi IMproved) will give us syntax highlighting and allow for advanced customization for terminal editing:
    yum install vim-common vim-enhanced # Install vim
  2. 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:
    yum install python-pip # Install python2.7 pip
    yum install python34-pip # Install python3.4 pip
  3. Issue the following command to install git:
    yum install git # Install git command line tool
Idea.png
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 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 for backing up your code onto the internet. Check out bitbucket for a free private code repository.
  1. 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:
    yum install python-ipython # Install ipython for python2.7
    pip3.4 install ipython  # Install ipython for python3.4
  2. Issue the following commands in your Ipython shell to check your work for this section:
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab0b
  3. Before moving on to the next step, make sure you identify and correct any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may proceed to the next step.
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab0c
    
  4. 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 procede to the next step.

  5. Make notes for all of your Investigation 1 (part1) observations in your lab log book, and proceed to investigation 2.

INVESTIGATION 2: USING THE "IPYTHON" CLI

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 some python code in a 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:
  1. To get into the ipython shell type:
    ipython3
    Now we are inside the IPython environment. We can run some basic bash commands in here, this is done through by using IPython magic functions.
  2. Lets try a few commands out now:
    %ls
    %pwd
    %cd ~/
    %ls
    Now hold on. You are not using Python here. These are aliases, that IPython gives you access to. What you are actually using is bash, but not all bash commands are available in the IPython environment.

  3. Lets find out which ones are available, type the following command into the IPython shell:
    %alias
    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.

  4. Next lets add a new bash command that seems to be missing from this list:
    %alias vim vim
  5. 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:
    exit
    ipython3
    %vim
    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.

  6. Exit your current IPython session:
    exit
  7. Now, create a new file and add the following content to it:
    vim ~/.ipython/profile_default/startup/00-alias.ipy
  8. Place our alias inside:
    %alias vim vim
  9. Save and quit the file. Now lets return to our IPython shell and confirm that our alias is available right away:
    ipython3
    %vim

    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.

  10. Exit vim now and head back to the IPython shell.

  11. Lets setup a directory structures for completing and organizing labs. These should be the locations to store your lab scripts.
    %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
  12. If you are interested in finding more information about magic functions in IPython, try entering the IPython shell and typing the following:
    %magic


This should show you a OVERWHELMING amount of information, as we move through 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 to interactively inspect the Python we are writing and running. Lets move on for now.


  1. Perform the following steps to evaluation 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 these check scripts regularly as you work through the labs, they may give you hints if you get stuck.
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab1a
    
  2. Make notes for observations in your lab log book, and proceed to investigation 3.


INVESTIGATION 3: WRITING PYTHON SCRIPTS

During 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 variables, and using math operators.

PART 1 - Common Introductory Python Functions

Printing Text

Lets start IPython interpreter and start writing some python code.


Perform the following steps:
  1. Issue the following commands:
    ipython3
    %cd ~/ops435/lab1
    %pwd
    %ls
    Our first python code we will write is 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.

  2. At the prompt, issue the following python function:
    print()
    You will notice that nothing happened when we ran this "print()" function. This is because we didn't pass any arguments to it, lets try again.

  3. Issue the following function:
    print('hello world')
    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 above is, passed a string as a argument to the print function. These words are important for understanding and talking about different aspects of code.

Creating the introductory "Hello World" Script

Let's use the information we learned in the previous section to create a simple Python script.

Perform the following steps:
  1. Using a text editor, open a new text file called lab1a.py:
    %vim ~/ops435/lab1/lab1a.py
  2. Write the following code into our python file.
    #!/usr/bin/env python3
    
    # Any line that starts with a "#" is also known as a comment,
    # these lines are ignored by the python interpreter even if
    # they contain code. The very first line is called a Shebang line, 
    # it is used to tell the system which interpreter to 
    # use(python2, python3, bash, etc). 
    
    # Description: This program will output "hello world" to the screen
    
    print('Hello world')
  3. Save the file and quit vim. We will now go over the process of manually running this python script. Both in the Bash shell and in the IPython shell.
  4. Now lets try running the script directly from the IPython shell by issuing the follwoing commands:
    ipython3
    %run ~/ops435/lab1/lab1a.py


    Your python script should have run, if you have any errors you should check that you typed the script in exactly. Be careful of extra spaces, symbols, letters, or lowercase/uppercase differences.

  5. Exit out of IPython.
  6. Now, from the Bash shell we will give it the correct linux permissions and run it. This is just showing the multiple ways you can use this python script. You are not required to have IPython running on a system, however hopefully we can use IPython's powerful features to our advantage.
  7. Issue the following command to exit your ipython session:
    exit
  8. Run your python script by issuing the following commands:
    chmod 755 ~/ops435/lab1/lab1a.py
    python3 ~/ops435/lab1/lab1a.py
  9. Download the check script and check your work. Enter the following commands from the bash shell.
    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 matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab1a
  10. 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.
  11. Make notes for observations in your lab log book, and proceed to part 2.

PART 2 - Working with Variables

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 Variables

Perform the following steps:
  1. First, let's make a new variable containing a value by issuing the following command:
    name = 'Thomas'
  2. Iss the following command to inspect the value of the variable"
    name
  3. Print the value to the screen:
    print(name)
  4. 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 is always outside the quotes, or it will not resolve to a value.
    print('I have a friend named ' + name)
  5. To gain practice, create a python script (called lab1b.py) with the following content and details:
  • The script should have a Shebang line
  • The script should use a single variable called "name"
  • The value of the "name" variable should be "Isaac"
  • The script, when executed, should print out "How old are you Isaac?"
  • Sample run:
    %cd ~/ops435/lab1/
    %run ./lab1b.py
    How old are you Isaac?
    Try the checking script as you are working through a script to sometimes get hints.

  1. Download and run the checking script. Enter the following commands from the bash shell:
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab1b
  2. Before proceeding, make certain that you identify any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may procede to the next step.

Integer Variables

Lets enter into IPython to test out variables.

Perform the following steps:
  1. Enter the following command to access the python prompt:
    ipython3
  2. Lets create some new variables to play with.
    num1 = 5
    num2 = 10

    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.

  3. Issue the following commands:
    num1
    num2
  4. Now we will make a new variable and try some math:
    sum = num1 + num2

    This will add the values contained in the variables together, providing a sum. However you will note that there is no output. First lets inspect the new value.

  5. Enter the following command:
    sum

    Does this value look right? If we wanted to print this out to the screen we could use the following python code.

  6. Enter the following function:
    print(sum)
  7. Now lets try printing this sum out with a string:
    print('The sum is: ' + sum)

    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.

    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 variable, your variable is still a interger.

  8. Issue the following:
    print('The sum is: ' + str(sum))

    What did you notice this time?
  9. To gain practice, create a python script (called lab1c.py) with the following content and details:
  • The script should have a Shebang line.
  • The script should have a variable called name
  • The script should have a variable called age
  • The value of the name variable should be Isaac
  • The variable age should contain a integer
  • The value of the age variable should be 72
  • The script, when executed, should print out "Isaac is 72 years old!"
Example run:
%cd ~/ops435/lab1/
%run ./lab1c.py
Isaac is 72 years old!

Try the check script as you are working through a script to sometimes get hints.
  1. Download and run the checking script. Enter the following commands from the bash shell:
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab1c
  2. 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 procede to the next step.
  3. Make notes for observations in your lab log book, and proceed to part 3.

PART 5 - MATH OPERATORS

Python has a number of math operators you can use in your programs.

Perform the following steps:
  1. Issue the following commands to see how they operate in the ipython shell:
    10 + 5    # addition
    10 - 5    # subtraction
    10 * 5    # multiplication
    10 / 5    # division
    10 ** 5   # exponents
    NOTE: 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.

  2. Go over the below examples and see if you understand each situation:
    10 + 5 * 2		# multiplication happens before addition
    (10 + 5) * 2 		# parentheses happen before multiplication
    10 + 5 * 2 - 10 ** 2	# first exponents, then multiplication, then addition and subtraction from left-to-right
    15 / 3 * 4		# division and multiplication happen from left-to-right
    100 / ((5 + 5) * 2)	# the inner most parentheses are first performing addition, then parentheses again with multiplication, finally the division
  3. To gain practice, create a python script (called lab1d.py) with the following content and details:
  • The script should have a Shebang line.
  • The variable x should contain a integer with the value 10
  • The variable y should contain a integer with the value 2
  • The variable z should contain a integer with the value 5
  • The script, when executed, should print out "10 + 2 * 5 = 20"
Example run:
%cd ~/ops435/lab1/
%run ./lab1d.py
10 + 2 * 5 = 20
Try the checking script as you are working through a script to sometimes get hints.

  1. Download and run the checking script. Enter the following commands from the bash shell:
    cd ~/ops435/lab1/
    pwd #confirm that you are in the right directory
    ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py
    python ./CheckLab1.py -f -v lab1d
    Before moving on to the next step make sure you identify any and all errors in "lab1a.py".

  2. When the check script tells you everything is "ok", you may procede to the next step.

  3. Make notes for observations in your lab log book, and proceed to Lab 1 Instructor Sign-off.

LAB 1 SIGN-OFF (SHOW INSTRUCTOR)

Students should be prepared with all required commands (system information) displayed in a terminal (or multiple terminals) prior to calling the instructor for signoff.


Have Ready to Show Your Instructor:
x
x
Lab1 logbook notes completed