Difference between revisions of "OPS435 Python Lab 1"
(→Part 3 - Selecting a Text Editor to Use For Your OPS435 Labs) |
(→Part 3 - Selecting a Text Editor to Use For Your OPS435 Labs) |
||
Line 99: | Line 99: | ||
<table border="1" cellspacing="0" cellpadding="5" width="40%" > | <table border="1" cellspacing="0" cellpadding="5" width="40%" > | ||
<tr><td>'''Text Editor'''</td><td>'''Characteristics'''</td></tr> | <tr><td>'''Text Editor'''</td><td>'''Characteristics'''</td></tr> | ||
− | <tr><td valign="top"> | + | <tr><td valign="top">'''Vim'''</td><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"> | + | <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">''''''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> | <tr><td valign="top">'''Other Editors'''</td><td valign="top">x</td></tr></table> |
Revision as of 07:58, 8 March 2017
Contents
Lab Preparation
Purpose/Objectives
Selecting, installing, and setting up your Linux Distribution environment. Learning basics of interacting with python.
Investigation 1 - Installing Linux
Part 1 - Choosing Your Linux Distribution
Since Python runs independently 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.
Linux Distribution | Characteristics | Python Version |
Centos7 | The stable version of Centos 7 - release 1151 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. |
Fedora | Fedora will always be the slightly harder distribution to support for a course, so it would be great to have a alternative that is stable. The Fedora distribution has a much larger set of packages throughout its repositories, while this is usually not advantageous for a server context, it can work very well with developers. As students write code they may want more customized environments and coding applications, some of these applications may be harder to get on a server distribution. The other benefit to using Fedora is gaining experience using newer software, by practicing with newer software, students will be exposed to changes they will see in much later Centos releases. | The most recent version of Fedora is a great pick for python development since they've switched the default python installed to python3, which is primarily what this course is about. However,this may cause issues as we get to Ansible or Openstack so we will have to cover the differences between python2 and python3 including a python2 installation on Fedora. |
Perform the following steps:
- After choosing your Linux distribution to use for these labs, install the latest graphical version of that Linux OS.
This course is designed with a unit testing suite, which can be used to look at the scripts you write 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 the right track, and show progress. - Download the check 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 python3 ./CheckLab1.py -f -v lab0a
- 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.
Part 2 - Setting Up Your Linux Environment For OPS435 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 first step is to update your entire system. Fedora uses a new package manager: it is based on yum, yet it contains newer code and has more maintainer features as also contains improved features. It should work very similarly to the yum command, but check the man pages if you get confused.
dnf update
Perform the following steps:
- Lets start installing applications we need, first Python version 3 and version 2:
dnf install python3 python2 # Install python3.5 and python2.7
- 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:
dnf install screen tmux && ln -s /usr/bin/true /etc/sysconfig/bash-prompt-screen
- You will now set your hostname to the Linux Distribution we are using:
hostnamectl set-hostname fedora24 # Set hostname to distribution-name
- Installing vim(Vi IMproved) will give us syntax highlighting and allow for advanced customization for terminal editing:
dnf install vim-common vim-enhanced # Install vim
- 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:
dnf install python-pip python3-pip # Install pip
Git is 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. - Issue the following command to install git:
dnf install git # Install git command line tool
- 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:
dnf install python-ipython python3-ipython # Install enhanced interactive python
Part 2 - Lab Check lab0b
cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py python3 ./CheckLab1.py -f -v lab0b
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.
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.
Text Editor | Characteristics |
Vim | 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. |
Atom | "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. |
'Sublime' | Sublime is a popular text editor with tons of customizations and themes. |
Other Editors | x |
Vim Editor
Atom Editor
Sublime
Sublime is a popular text editor with tons of customizations and themes.
More
Suggest some more.
Part 3 - Lab Check lab0c
cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py python3 ./CheckLab1.py -f -v lab0c
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.
Investigation 2 - IPython
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 - Using IPython
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.
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.
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.
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 command 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.
Next lets add a new bash command that seems to be missing from this list:
%alias vim vim
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 inbetween sessions.
Exit your current IPython session:
exit
Create a new file and add the following content to it:
vim ~/.ipython/profile_default/startup/00-alias.ipy <pre> Place our alias inside: <blockquote> %alias vim vim </blockquote> Save and quit the file. Now lets return to our IPython shell and confirm that our alias is available right away. <pre> ipython3 %vim
At this point vim should open successfully and you should now understand how to create new IPython aliases and store them persistently. Exit vim now and head back to the IPython shell.
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
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.
Part 1 - Lab Check lab1a
cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.py python3 ./CheckLab1.py -f -v lab1a
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.
Investigation 3 - Writing a Python Script
During this investigation we will start writing our very first python scripts. These will be very basic and help us practice syntax and foundational skills, such as: outputing text to the screen, storing data inside variables, and using math operators.
Part 1 - Printing
Lets start IPython interpreter and start writing some python code.
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.
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.
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.
Part 2 - Hello World
Next, we will make our first script with the above function. Open a new text file called "lab1a.py".
%vim ~/ops435/lab1/lab1a.py
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')
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.
Now lets try running the script directly from the IPython shell.
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.
Exit out of IPython. 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.
exit
chmod 755 ~/ops435/lab1/lab1a.py python3 ~/ops435/lab1/lab1a.py
Part 2 - Lab Check lab1a
This course is designed with a unit testing suite, which can be used to look at the scripts you write 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 the write track, and show progress.
Download the check script. 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 python3 ./CheckLab1.py -f -v lab1a
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.
Part 3 - Variables
A variable is used to store data for use later in the program. This data can be a string, integer, decimal, etc.
Part 3 - strings
First make a new variable containing a value.
name = 'Thomas'
Inspect the value.
name
Print the value to the screen.
print(name)
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)
Part 3 - Evaluation
Create a python script: lab1b.py
- 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?"
Example run:
%cd ~/ops435/lab1/ %run ./lab1b.py How old are you Isaac?
Try the check script as you are working through a script to sometimes get hints.
Part 3 - Lab Check lab1b
This course is designed with a unit testing suite, which can be used to look at the scripts you write 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 the write track, and show progress.
Download the check 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 python3 ./CheckLab1.py -f -v lab1b
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.
Part 4 - Integers
Lets enter into IPython to test out variables.
ipython3
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.
num1 num2
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.
sum
Does this value look right? If we wanted to print this out to the screen we could use the following python code.
print(sum)
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.
print('The sum is: ' + str(sum))
Part 4 - Evaluation
Create a python script: lab1c.py
- 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.
Part 4 - Lab Check lab1c
This course is designed with a unit testing suite, which can be used to look at the scripts you write 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 the write track, and show progress.
Download the check 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 python3 ./CheckLab1.py -f -v lab1c
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.
Part 5 - Math Operators
Python has a number of math operators you can use in your programs.
10 + 5 # addition 10 - 5 # subtraction 10 * 5 # multiplication 10 / 5 # division 10 ** 5 # exponents
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.
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
Part 5 - Evaluation
Create a python script: lab1d.py
- 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 check script as you are working through a script to sometimes get hints.
Part 5 - Lab Check lab1d
This course is designed with a unit testing suite, which can be used to look at the scripts you write 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 the write track, and show progress.
Download the check 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 python3 ./CheckLab1.py -f -v lab1d
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.