OPS445 Lab 1: Difference between revisions
Line 1: | Line 1: | ||
In this lab we will start writing python programs. 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. | |||
In this lab we will start writing | |||
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. |
Revision as of 14:11, 6 January 2025
In this lab we will start writing python programs. 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.
Values and Variables
In Python, a variable 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 data types in this lab.
Different data types are stored in memory in a different way, and the programming language treats them differently. That's true even for some values which seem identical to a human being.
Strings
String values contain text to be used in your program. Examples of strings could be user-names, full-names, item descriptions, etc.
We will now assign a string to a variable and display the contents stored in that variable.
- Create a python script (called lab1b.py) with the following contents:
personName = "Andrew"
There is a number of important things to understand in that simple line of code:
- personName is the name of a variable. It can contain any value you assign to it. Or no value at all.
- "Andrew" is a string value. Python knows it's a string value because it's in single quotes. In python single and double quotes do the same thing.
- The equals sign is used differently in programming compared to how it's used in mathematics. It does not mean that personName is equal to Andrew. Rather: it is an instruction to the computer to assign the value on the right side of the = to the variable on the left side of the =.
- In a programming language the = is called an assignment operator. It's an operator which is used to assign on thing to another.
This is the sort of stuff that programmers understand implicitly, and if it's not completely obvious to you: think long and hard about those points.
- Print the contents of the personName variable to the screen:
print(personName)
- Add this line, and think about why it does something different:
print("personName")
- In Python the + operator can be used to concatenate two strings (i.e. append the second one to the first one). Try this:
print('I have a friend named' + personName)
- The output will look a little weird, fix it. Note that to a computer a space (and a newline, and a tab) is just another character, like the letter "A".
- Why does this do something different?
print("I have a friend named " + "personName")
- To gain practice, complete your python script with the following:
- The script should have a Shebang line like you did for your lab1a.py python script
- The script should use a single variable called "personName"
- The value of the "personName" variable should be "John"
- The script, when executed, should print out "How old are you John?"
- Sample run:
cd ~/ops435/lab1/ ./lab1b.py How old are you John?
- Run the checking script:
pwd # Confirm that you are in the right directory ls lab1b.py # Confirm that you have the lab1b.py script in your directory ls CheckLab1.py || wget http://ops345.ca/ops445/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.
Integers
Integers objects are used to store whole numbers (positive or negative, but without anything after the decimal point). Integers can be used for mathematical operations.
- Create a python script called lab1c.py
- Create two variables to play with:
num1 = 5 num2 = 10
Note that neither the 5 nor the 10 have quotes around them. That's how Python knows they are numbers and not strings.
- You can print the values in those variables:
print(num1) print(num2)
- Create a new variable called "sum" and do some math:
sum = num1 + num2
Again, this is obvious to experienced programmers, but you might want to take some time to consider everything that's happening on that line of code:
- The value is retrieved from the num1 variable.
- The value is retrieved from the num2 variable.
- Those two values are added arythmetically, creating a third value.
- A new variable called sum is created.
- The third value (the sum of the first two values) is assigned to the variable named sum.
Notice that despite all that being done, there is no output. That's because you didn't instruct the computer to output the sum which was calculated.
- Let's inspect the value inside the sum variable:
print(sum)
- Now try printing this sum out with a string:
print('The sum is: ' + sum)
This will give you an apparently unwarranted error, but if you disagree with it: you probably don't yet fully understand the reasoning behind having types.
- When you add two strings: they are concatenated.
- When you add two numbers: they are arithmentcally added.
- What's supposed to happen when you add a string and a number?
Python will not assume on your behalf that the number should be converted to a string and concatenated to the first string, because that might not be what you want. If it is what you want, you have to be explicit about it.
- Try this instead:
print('The sum is: ' + str(sum))
- To gain practice, complete your lab1c.py 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 John
- 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!"
- Sample run:
cd ~/ops435/lab1/ ./lab1c.py John is 72 years old!
- Download and run the checking script:
pwd # Confirm that you are in the right directory ls lab1c.py # Confirm that you have the lab1c.py script in your directory ls CheckLab1.py || wget http://ops345.ca/ops445/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.
Arithmetic
The stuff in this section is self-evident, assuming you remember the math you were supposed to have learned in grade 5. But even if you don't: it's unlikely that you'll be doing anything that requires this knowledge.
- 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
Python decides on the order of operations based on the very common PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction).
- For the following examples: try to figure out in your head what the answer should be, and then add that line to your program to check your thinking.
print(10 + 5 * 2) # multiplication happens before addition print((10 + 5) * 2) # parentheses happen before multiplication 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 lab1d.py with the following:
- 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 (the printout should change if the values in the variables change)
- Sample run:
cd ~/ops435/lab1/ ./lab1d.py 10 + 2 * 5 = 20
- Run the checking script:
pwd # Confirm that you are in the right directory ls lab1d.py # Confirm that you have the lab1d.py script in your directory ls CheckLab1.py || wget http://ops345.ca/ops445/CheckLab1.py python3 ./CheckLab1.py -f -v lab1d
- 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.
Submit evidence of your work
Run the following command in a terminal:
cd ~/ops435/lab1
python3 ./CheckLab1.py -f -v
- The output of the lab check command must say OK.
- To show that you completed the lab, submit a screenshot of the terminal with that output.