Selecting, installing, and setting up your Linux Distribution environment. Learning basics of interacting with python.
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.
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.
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.
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.
Vim Editor | <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><td valign="top">Atom Editor</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 Editor</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>
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.