Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 1

95 bytes added, 22:55, 16 January 2018
PART 5 - MATH OPERATORS
:'''Perform the following steps:'''
:# Issue Try some of the following commands to see how they operate what happens in the ipython shellPython:<source lang="python">print(10 + 5 ) # additionprint(10 - 5 ) # subtractionprint(10 * 5 ) # multiplicationprint(10 / 5 ) # divisionprint(10 ** 5 ) # exponents
</source>NOTE: You must be careful when combining more complex math operators 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 additionprint((10 + 5) * 2 ) # parentheses happen before multiplicationprint(10 + 5 * 2 - 10 ** 2 ) # first exponents, then multiplication, then addition and subtraction from left-to-rightprint(15 / 3 * 4 ) # division and multiplication happen from left-to-rightprint(100 / ((5 + 5) * 2)) # the inner most parentheses are first performing addition, then parentheses again with multiplication, finally the division
</source>
:#To gain practice, create a python complete your script (called lab1d.py) with the following content and details:
::* 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)
:::Example run: <source>
cd ~/ops435/lab1/
run ./lab1d.py
10 + 2 * 5 = 20
</source>Try the checking script as you are working through a script to sometimes get hints.<br><br>