OPS445 Lab 1: Difference between revisions
(10 intermediate revisions by the same user not shown) | |||
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. | ||
Line 7: | Line 5: | ||
= Creating your "Hello World" program = | = Creating your "Hello World" program = | ||
You will learn to create a simple python | You will learn to create a simple python program in this section. This python program 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 program in the python3 shell as well as learn how to run the python program from the bash shell. | ||
* Create a new python file in your '''~/ops435/lab1''' directory. Call it '''lab1a.py''' | * Create a new python file in your '''~/ops435/lab1''' directory. Call it '''lab1a.py''' | ||
Line 70: | Line 68: | ||
We will now assign a string to a variable and display the contents stored in that variable. | We will now assign a string to a variable and display the contents stored in that variable. | ||
* Create a python | * Create a python program (called lab1b.py) with the following contents:<syntaxhighlight lang="python"> | ||
personName = "Andrew" | personName = "Andrew" | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 97: | Line 95: | ||
print("I have a friend named " + "personName") | print("I have a friend named " + "personName") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* To gain practice, complete your python | * To gain practice, complete your python program with the following: | ||
# The | # The program should have a '''Shebang line''' like you did for your lab1a.py python program | ||
# The | # The program should use a single variable called "personName" | ||
# The value of the "personName" variable should be "John" | # The value of the "personName" variable should be "John" | ||
# The | # The program, when executed, should print out "How old are you John?" | ||
* Sample run: <syntaxhighlight lang="bash"> | * Sample run: <syntaxhighlight lang="bash"> | ||
cd ~/ops435/lab1/ | cd ~/ops435/lab1/ | ||
Line 120: | Line 118: | ||
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. | 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 | * Create a python program called lab1c.py | ||
* Create two variables to play with:<syntaxhighlight lang="python"> | * Create two variables to play with:<syntaxhighlight lang="python"> | ||
num1 = 5 | num1 = 5 | ||
Line 169: | Line 167: | ||
* To gain practice, complete your lab1c.py with the following features: | * To gain practice, complete your lab1c.py with the following features: | ||
# The | # The program should have a Shebang line. | ||
# The | # The program should have a variable called '''personName''' | ||
# The | # The program should have a variable called '''age''' | ||
# The value of the ''' | # The value of the '''personName''' object should be '''John''' | ||
# The | # The variable '''age''' should contain a integer | ||
# The value of the '''age''' | # The value of the '''age''' variable should be '''72''' | ||
# The | # The program, when executed, should print out "John is 72 years old!" | ||
* Sample run:<syntaxhighlight lang="bash"> | * Sample run:<syntaxhighlight lang="bash"> | ||
cd ~/ops435/lab1/ | cd ~/ops435/lab1/ | ||
Line 189: | Line 187: | ||
* 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. | * 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:<syntaxhighlight lang="python"> | |||
print(10 + 5) # addition | print(10 + 5) # addition | ||
print(10 - 5) # subtraction | print(10 - 5) # subtraction | ||
print(10 * 5) # multiplication | print(10 * 5) # multiplication | ||
print(10 / 5) # division | print(10 / 5) # division | ||
</syntaxhighlight> | |||
</ | |||
: | Python decides on the order of operations based on the very common '''PEMDAS''' ('''P'''arentheses, '''E'''xponents, '''M'''ultiplication and '''D'''ivision, '''A'''ddition and '''S'''ubtraction). | ||
print(10 + 5 * 2) | |||
print((10 + 5) * 2) | * 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.<syntaxhighlight lang="python"> | ||
print(10 + 5 * 2) # multiplication happens before addition | |||
print(15 / 3 * 4) | print((10 + 5) * 2) # parentheses happen before multiplication | ||
print(100 / ((5 + 5) * 2)) # the inner most parentheses are first performing addition, then parentheses again with multiplication, finally the division | 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 | ||
</syntaxhighlight> | |||
* To gain practice, complete your lab1d.py with the following: | |||
# The program 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 program, when executed, should print out '''10 + 2 * 5 = 20''' (the printout should change if the values in the variables change) | |||
* Sample run: <syntaxhighlight lang="bash"> | |||
cd ~/ops435/lab1/ | cd ~/ops435/lab1/ | ||
./lab1d.py | ./lab1d.py | ||
10 + 2 * 5 = 20 | 10 + 2 * 5 = 20 | ||
</ | </syntaxhighlight> | ||
* Run the checking script:<syntaxhighlight lang="bash"> | |||
pwd # | pwd # Confirm that you are in the right directory | ||
ls CheckLab1.py || wget | 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 | python3 ./CheckLab1.py -f -v lab1d | ||
</ | </syntaxhighlight> | ||
* 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.</li></ol> | |||
= Submit evidence of your work = | |||
Run the following command in a terminal:<syntaxhighlight lang="bash"> | |||
cd ~/ops435/lab1 | |||
python3 ./CheckLab1.py -f | |||
</syntaxhighlight> | |||
* 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. | |||
[[Category: | [[Category:OPS445]] |
Latest revision as of 16:03, 15 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 program in this section. This python program 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 program in the python3 shell as well as learn how to run the python program 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 program (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 program with the following:
- The program should have a Shebang line like you did for your lab1a.py python program
- The program should use a single variable called "personName"
- The value of the "personName" variable should be "John"
- The program, 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 program 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 program should have a Shebang line.
- The program should have a variable called personName
- The program should have a variable called age
- The value of the personName object should be John
- The variable age should contain a integer
- The value of the age variable should be 72
- The program, when executed, should print out "John 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 program 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 program, 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
- 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.