OPS445 Lab 1
THIS LAB IS NOT READY YET!!!
In this lab 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 objects, and using math operators.
The tasks in this lab appear to be overly simple because I'm assuming you don't have any programming experience and I need to spend a lot of time explaining basic concepts.
Creating your "Hello World" program
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 python3 shell as well as learn how to run the python script from the bash shell.
- 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 built-in Python print() function. A function is code that has been defined in another location. Later in the course we will create our own function.
Functions can take arguments, use these arguments in some way, and then usually return a result. The print() function's sole purpose is to output information to the screen.
- Add the following line into your source code file:
print()
- Run your program from the command-line:
python3 ./lab1a.py
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.
- Modify your call to print() to inlcude an argument ('hello world'):
print('hello world')
This time the python function "print()" has outputted to the screen the words 'hello world'. In a programming lanuages a bunch of characters grouped together like 'hello world' is called a 'string'. In the above example, a string was passed as a argument to the print function. These words are important for understanding and talking about different aspects of code.
- 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.
You can execute python programs from the command-line without using the python3 program.
- Give your program execute permissions first:
chmod a+x lab1a.py
- And try to run it:
./lab1a.py
You will get some bizarre error messages. The problem is that your program is not a binary (like /bin/ls), it's a script. And a script needs to be executed by an interpreter program.
Since you're running your script in bash, and you haven't told bash otherwise: bash assumes your script is a bash script and tries to execute it as such. That's why you're getting the weird errors.
- Add a shebang line at the top, to tell bash that this is meant to be executed by python3:
#!/usr/bin/env python3 print('hello world')
- Run your program again, this time it should work:
./lab1a.py
- Download the check script and check your work.
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 http://ops345.ca/ops445/CheckLab1.py python3 ./CheckLab1.py -f -v lab1a
- Before moving on to the next part of the lab: make sure you identify any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may proceed.
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 variables 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 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 2 strings together. However, make sure that 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://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/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 some new objects 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 integer object and try some math:This will add the values contained in the integer objects together, providing a sum. However you will note that there is no output. Let's inspect the new value:
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 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.
print('The sum is: ' + sum)
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 object, your object is still an interger 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://raw.githubusercontent.com/Seneca-CDOT/ops435/master/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://raw.githubusercontent.com/Seneca-CDOT/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!
- For sections A & B:
- ✓ Submit your output and Python scrips via Blackboard instead.
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://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py