OPS435 Python3 Lab 1
LAB OBJECTIVES
- In this lab, you will install a current distribution of Linux in a Virtual Machine on your external SSD. You will do most of your future work using this VM (including quizzes!). You will setup your Python scripting environment on this VM, including the setup of the python libraries and also the setup of a user-friendly interactive Python environment called ipython. You will also install additional utility 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 explore basic operations such as: sending text to screen, manipulate python objects, and performing math operations.
INVESTIGATION 1: INSTALLING LINUX VM
PART 1 - Installing Your Linux Distribution
- 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.
- Most of you will likely use the school's Windows machines with the Vmware hypervisor. It is also possible to use your own laptop computer, but check with your professor before you do that.
- Centos 7 comes with python 2.7, for this course we will be primarily using Python3. However, not practicing python 2.7 would be a mistake, since so many programs and operating systems still depend on Python2. It is recommended that students note general improvements/differences in Python2 and Python3.
- Centos 7 VM Details / Minimum Requirements:
- Name: c7-[student-id] --> replace the place holder [student-id] with you Seneca user name
- 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-1810.iso
- Download outside Seneca College: http://mirror.csclub.uwaterloo.ca/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
- Disk space: 50GB
- CPUs: 4 (Number of processors: 1, Number of cores per processor: 4)
- Internal Memory: 8GB
- Environment: GNOME Desktop
- 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 CentOS 7 (Graphical Desktop) for our main Linux machine. When creating your VM, refer to the Details / Minimum Requirements section above for assistance.
- Install CentOS 7 VM selecting the default partitions. When you have finished the installation of CentOS 7, you may proceed to the next step.
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 an interactive 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.
yum update
- Install extra packages for enterprise linux:
yum install epel-release
- Next install applications that we required, first Python version 3 and version 2:
yum install python36 python36-devel # Install python3.6 and python3.6 development libraries yum install python python2-devel # Install python2.7 and python2.7 development libraries
- 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
- 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 c7-[student_id] # please replace the place holder [student_id] with your Seneca user name
- The first step is to update your entire system.
- 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.
- 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 python36-pip # Install python3.6 pip
- Upgrade python36-pip with the following command:
python3 -m pip install --upgrade pip
- Issue the following command to install git:
yum install git # Install git command line tool
- IPython is one of the most useful tools for Python beginners. Lets install it. You will learn more about it in the next section:
yum install python-ipython # Install ipython for python2.7 python3 -m pip install ipython # Install ipython for python3.6
- Exit from the superuser shell and enter to your regular user shell and issue the following commands to check your work for this section:
mkdir -p ~/ops435/lab1/ cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls CheckLab1.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab0a python3 ./CheckLab1.py -f -v lab0b
- Before moving on to the next step, make sure you identify and correct any and all errors in "lab0b" output. When the check script tells you everything is "ok", you may proceed to the next step.
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 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.
PART 1 - Common Ipython Commands and Features
Using Magic Functions
- Lets begin by running some python code in 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 access the ipython shell, issue the following command:Now we are now inside the IPython environment. We can run some basic bash commands within this shell by issuing IPython magic functions.
ipython3
- Lets try a few commands out now:Remember: You are not using Python here, 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.
ls pwd cd ~/ ls
- Lets find out which ones are available, type the following command into the IPython shell:We should now have a list of all the bash commands available in IPython.
alias
- Lets setup a directory structures for completing and organizing labs. These should be the locations to store your lab scripts.
mkdir ~/ops435/lab2 mkdir ~/ops435/lab3 mkdir ~/ops435/lab4 mkdir ~/ops435/lab5 mkdir ~/ops435/lab6 mkdir ~/ops435/lab7 mkdir ~/ops435/lab8 mkdir ~/ops435/lab9
- If you are interested in finding more information about other magic functions in IPython, then within the IPython shell, and enter the following (press the letter q to exit this function):
magic
- 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.
- Issue the following command to exit your ipython session:
exit
- In the future see if ctrl-d works instead of issuing the exit command to quit the ipython shell.
- Perform the following steps to evaluate this unit. 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 https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab0c
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: sending text to the screen, storing data inside objects, and using math operators.
PART 1 - Introduction to Python builtin Functions
Creating the "Hello World" Python Script
You will learn to create a simple python script in this section. This python script will just call the Python builtin function print() to send the text "hello world" to the screen. The "hello world" is 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 Python3 shell as well as to learn how to run the Python script from the bash shell.
- Perform the following steps:
- Create a new Python file in your ~/ops435/lab1 directory. Call it lab1a.py. The first Python code we will write is going to call the print() function. A function is code that has been defined somewhere. Functions can take arguments, use these arguments in some way, and then usually, but not always, return a result. The first function we will use is the "print()" functions, it's sole purpose is to send data to the screen.
- Add the following line into your source code file:And run it from the command-line:
print()
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.python3 ./lab1a.py
- Modify your call to print() to include an argument ('hello world'):This time we should now see that the python function "print()" has sent something to the screen - the words 'hello world'. In Python a word or a bunch of characters like 'hello world' is called a 'string'. In the above example, a string was passed as an argument to the print function. These words are important for understanding and talking about different aspects of code.
print('hello world')
- 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.
- Create a new Python file in your ~/ops435/lab1 directory. Call it lab1a.py. The first Python code we will write is going to call the print() function. A function is code that has been defined somewhere. Functions can take arguments, use these arguments in some way, and then usually, but not always, return a result. The first function we will use is the "print()" functions, it's sole purpose is to send data to the screen.
- 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.
#!/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')
- Another way of running a Python program is executing it directly, e.g.: Note that the file will need execute permissions even though you ran it just fine earlier. Why is that?
./lab1a.py
- 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 https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/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 2 - Working with Python Objects
- 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 objects in this lab. You will learn and use other Python object types in future labs.
String Objects
- String 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 how to assign a string to an object and how to display contents stored in a string object.
- 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 object containing a value:
name = 'Thomas'
- Print the value to the screen:
print(name)
- Think about why this does something different:
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 two strings together. However, make sure that the name of your object is always outside the quotes, or it will not resolve to a value.
print('I have a friend named ' + name)
- 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 object called "name"
- The value of the "name" object should be "Isaac"
- The script, when executed, should print out "How old are you Isaac?"
- Sample run: Try the checking script as you are working through a script to sometimes get hints.
cd ~/ops435/lab1/ ./lab1b.py How old are you Isaac?
- 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 https://ict.senecacollege.ca/~raymond.chan/ops435/labs/CheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1b
- 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 proceed to the next step.
Integer Objects
- In Python, integer objects are used to store 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:
- Create a python script (called lab1c.py) and first - start with a few simple things to try:
- Lets create two new objects, num1 and num2, to play with.
num1 = 5 num2 = 10
- You can print the values in those integer objects:
print(num1) print(num2)
- Now we will make a new object called "sum", and try some math:This will add the values contained in the integer objects together and assign the result to the object named "sum". However you will note that there is no data show up on the screen. Let's inspect the contents of the new object named "sum":
sum = num1 + num2
Does this value look right? Are you sure?print(sum)
- Now lets try printing this sum out with a string:What happened? Did you receive an error? This 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.
print('The sum is: ' + sum)
In order concatenate a sting and an integer object, we will have to use another builtin function called "str()" to convert an integer object to a string first. 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 object, your object is still an integer object. - Issue the following:What did you notice this time?
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 an object called name
- The script should have an object called age
- The value of the name object should be Isaac
- The object age should contain a integer
- The value of the age object should be 72
- The script, when executed, should print out "Isaac is 72 years old!"
- Example run:
cd ~/ops435/lab1/ ./lab1c.py Isaac is 72 years old!
Try the check script as you are working through a script to sometimes get hints.
- 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 https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1c
- 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 proceed to the next step.
PART 5 - MATH OPERATORS
- In the previous section, you performed a couple of simple mathematical operations. In this section, you will learn some additional mathematical operations.
- Perform the following steps:
- Try some of the following to see what happens in Python: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.
print(10 + 5) # addition print(10 - 5) # subtraction print(10 * 5) # multiplication print(10 / 5) # division print(10 ** 5) # exponents
- Go over the below examples and see if you understand each situation:
print(10 + 5 * 2) # multiplication happens before addition print((10 + 5) * 2) # parentheses happen before multiplication print(10 + 5 * 2 - 10 ** 2) # first exponents, then multiplication, then addition and subtraction from left-to-right print(15 / 3 * 4) # division and multiplication happen from left-to-right print(100 / ((5 + 5) * 2)) # the inner most parentheses are first performing addition, then parentheses again with multiplication, finally the division
- To gain practice, complete your script with the following content and details:
- The script should have a Shebang line.
- The object x should contain a integer with the value 10
- The object y should contain a integer with the value 2
- The 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: Try the checking script as you are working through a script to sometimes get hints.
cd ~/ops435/lab1/ ./lab1d.py 10 + 2 * 5 = 20
- Try some of the following to see what happens in Python:
- Download and run the checking script. Enter the following commands from the bash shell:Before moving on to the next step make sure you identify any and all errors in "lab1d.py".
cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls CheckLab1.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/master/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1d
- When the check script tells you everything is "ok", you may proceed to the next step.
LAB 1 SIGN-OFF (SHOW INSTRUCTOR)
- Have Ready to Show Your Instructor:
- ✓ Output of:
./CheckLab1.py -f -v
- ✓ Output of:
cat lab1a.py lab1b.py lab1c.py lab1d.py
- ✓ Output of:
- Be able to answer any questions about the lab to show that you understood it!
- ✓ Submit your output and Python scrips via Blackboard by the due date for Lab 1.
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:
- Contain a she-bang line
- Display a greetings message to the user
- display an empty line (hint: use the special character \n to print the a new-line character)
- Display text, "Your current directory is:" (You are NOT required to display quotation marks)
- Display the current working directory pathname (using an appropriate command)
- Display another empty line
- How do you execute a Python script when you are within the ipython3 shell?
- How do you execute a Python script when you are in the Bash 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:
https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.py