← Older edit
Newer edit →
OPS435 Online Lab1
2,498 bytes added
21:48, 12 May 2020
Investigation 5 - exploring on how to get Python to do maths
= Investigation 5 - exploring on how to get Python to do maths =
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:<source lang="python">
print(10 + 5) # addition
print(10 - 5) # subtraction
5) # multiplication
print(10 / 5) # division
print(10 ** 5) # exponents
</source>NOTE: You must be careful when combining more complex math
together. Python uses '''PEMDAS''' ('''P'''arentheses, '''E'''xponents, '''M'''ultiplication and '''D'''ivision, '''A'''ddition and '''S'''ubtraction) to resolve math.<br><br>
:# Go over the below examples and see if you understand each situation:<source lang="python">
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: <source>
10 + 2 * 5 = 20
</source>Try the checking script as you are working through a script to sometimes get hints.<br><br>
<ol><li style="margin-left:25px;" value="4">Download and run the checking script. Enter the following commands from the bash shell:<source lang="bash">
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
</source>Before moving on to the next step make sure you identify any and all errors in "lab1d.py".<br><br></li>
<li style="margin-left:25px;">When the check script tells you everything is "ok", you may proceed to the next step.<br><br></li>
=LAB 1 SIGN-OFF Upload the following files individually to Blackboard=
Retrieved from "
SICT AR Meeting Area
get involved with CDOT
as a Student
as an Open Source Community Member
as a Company
Real World Mozilla
Course Project List
Potential Course Projects
About CDOT Wiki