Changes

Jump to: navigation, search

OPS435 Python Lab 1

110 bytes added, 13:16, 1 August 2017
Integer Variables
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

Navigation menu