Changes

Jump to: navigation, search

OPS435 Python Lab 1

220 bytes added, 13:18, 1 August 2017
PART 5 - MATH OPERATORS
ipython3
</source>
:#Let's make a new variable containing a value by issuing the following command:<sourcelang="python">
name = 'Thomas'
</source>
:#Type the variable name to inspect the value it contains<sourcesourc lang="python"e>
name
</source>
:#Print the value to the screen:<sourcelang="python">
print(name)
</source>
:#Think about why this does something different:<sourcelang="python">
print('name')
</source>
:#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 variable is always outside the quotes, or it will not resolve to a value.<sourcelang="python">
print('I have a friend named ' + name)
</source>
ipython3
</source>
:#Lets create some new variables to play with.<sourcelang="python">
num1 = 5
num2 = 10
</source><br>In IPython we can inspect these variables by just typing the name of the variable. But in a python script this will not provide any output. This feature is useful however for debugging.<br><br>
:#Issue the following commands:<sourcelang="python">
num1
num2
</source>
:#Now we will make a new variable and try some math:<sourcelang="python">
sum = num1 + num2
</source><br>This will add the values contained in the variables together, providing a sum. However you will note that there is no output. First lets inspect the new value.<br><br>
:#Enter the following command:<sourcelang="python">
sum
</source><br>Does this value look right? Are you sure? If we wanted to print this out to the screen we could use the following python code.<br><br>
:#Enter the following function:<sourcelang="python">
print(sum)
</source>
:#Now lets try printing this sum out with a string:<sourcelang="python">
print('The sum is: ' + sum)
</source><br>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.<br><br>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 variable, your variable is still a interger.<br><br>
:# Issue the following:<sourcelang="python">
print('The sum is: ' + str(sum))
</source><br>What did you notice this time?<br>
Isaac is 72 years old!
</source><br>Try the check script as you are working through a script to sometimes get hints.<br>
<ol><li value=10" style="margin-left:25px;">Download and run the checking script. Enter the following commands from the bash shell:<sourcelang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
:'''Perform the following steps:'''
:# Issue the following commands to see how they operate in the ipython shell:<sourcelang="python">
10 + 5 # addition
10 - 5 # subtraction
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:<sourcelang="python">
10 + 5 * 2 # multiplication happens before addition
(10 + 5) * 2 # parentheses happen before multiplication
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:<sourcelang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory

Navigation menu