OPS445 Lab 1: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
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.
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" Script =
= 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.
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.

Revision as of 00:47, 6 January 2025

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.
Idea.png
Reading errors
One of the things that makes a good programmer is debugging skills. The first and most important debugging technique is reading and understanding error messages. Try to understand what the errors are saying even if you think you already know what the problem is and already have some idea about how to fix it.

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:
  1. Create a python script (called lab1b.py) and first - start with a few simple things to try:
  2. Let's make a new object containing a value:
    name =  'Thomas'
    
  3. Print the value to the screen:
    print(name)
    
  4. Think about why this does something different:
    print('name')
    
  5. 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)
    
  6. 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:
     cd ~/ops435/lab1/
     ./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 https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py
    python3 ./CheckLab1.py -f -v lab1b
    
  2. 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:
  1. Create a python script (called lab1c.py) and first - start with a few simple things to try:
  2. Lets create some new objects to play with.
    num1 = 5
    num2 = 10
    
  3. You can print the values in those integer objects:
    print(num1)
    print(num2)
    
  4. Now we will make a new integer object and try some math:
    sum = num1 + num2
    
    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:
    print(sum)
    
    Does this value look right? Are you sure?
  5. 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 object, your object is still an interger object.

  6. Issue the following:
    print('The sum is: ' + str(sum))
    
    What did you notice this time?
  7. 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.
  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 https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py
    python3 ./CheckLab1.py -f -v lab1c
    
  2. 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:
  1. Try some of the following to see what happens in Python:
    print(10 + 5)    # addition
    print(10 - 5)    # subtraction
    print(10 * 5)    # multiplication
    print(10 / 5)    # division
    print(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:
    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
    
  3. 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:
cd ~/ops435/lab1/
./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 https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py
    python3 ./CheckLab1.py -f -v lab1d
    
    Before moving on to the next step make sure you identify any and all errors in "lab1d.py".

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



LAB 1 SIGN-OFF (SHOW INSTRUCTOR)

File:Lab1 signoff.png
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:
Output of: ./CheckLab1.py -f -v
Output of: cat lab1a.py lab1b.py lab1c.py lab1d.py
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

  1. Write the command to change the hostname of your Linux machine to centos7.
  2. What is the purpose of git? How will git be used in our OPS435 course?
  3. Write the command to create an alias for the Linux command vim which will be called vi in your ipython3 session.
  4. Write the absolute pathname for the ipython3 alias configuration file.
  5. Write Python code that when run, will perform the following tasks:
    1. Contain a she-bang line
    2. Display a greetings message to the user
    3. display an empty line (hint: use the special character \n to print the a new-line character)
    4. Display text, "Your current directory is:" (You are NOT required to display quotation marks)
    5. Display the current working directory pathname (using an appropriate command)
    6. Display another empty line
  6. How do you execute a Python script when you are within the ipython3 shell?
  7. How do you execute a Python script when you are in the Bash Shell (i.e. NOT within the Ipython3 shell)?
  8. 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