Changes

Jump to: navigation, search

OPS435 Python Lab 1

236 bytes removed, 13:18, 1 August 2017
String Variables
ipython3
</source>
:#Let's make a new variable containing a value by issuing the following command:<source lang="python">
name = 'Thomas'
</source>
:#Type the variable name to inspect the value it contains<sourc lang="python"esource>
name
</source>
:#Print the value to the screen:<source lang="python">
print(name)
</source>
:#Think about why this does something different:<source lang="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.<source lang="python">
print('I have a friend named ' + name)
</source>
ipython3
</source>
:#Lets create some new variables to play with.<source lang="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:<source lang="python">
num1
num2
</source>
:#Now we will make a new variable and try some math:<source lang="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:<source lang="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:<source lang="python">
print(sum)
</source>
:#Now lets try printing this sum out with a string:<source lang="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:<source lang="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:<source lang="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:<source lang="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:<source lang="python">
10 + 5 * 2 # multiplication happens before addition
(10 + 5) * 2 # parentheses happen before multiplication
::* The variable '''z''' should contain a integer with the value '''5'''
::* The script, when executed, should print out "10 + 2 * 5 = 20"
:::Example run: <source lang="python">
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>
<ol><li style="margin-left:25px;" value="4">Download and run the checking script. Enter the following commands from the bash shell:<source lang="python">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory

Navigation menu