Changes

Jump to: navigation, search

OPS435 Python Lab 1

323 bytes added, 09:06, 16 May 2017
Integer Variables
Lets enter into IPython to test out variables.
 '''Perform the following steps:''' :#Enter the following command to access the python prompt:<presource>
ipython3
</presource:#Lets create some new variables to play with.<presource>
num1 = 5
num2 = 10
</presource><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:<presource>
num1
num2
</presource:#Now we will make a new variable and try some math. :<presource>
sum = num1 + num2
</presource><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.
 :#Enter the following command:<presource>
sum
</presource><br> Does this value look right? If we wanted to print this out to the screen we could use the following python code.<br> :#Enter the following function:<presource>
print(sum)
</presource:#Now lets try printing this sum out with a string.:<presource>
print('The sum is: ' + sum)
</presource><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> :# Issue the following:<presource>
print('The sum is: ' + str(sum))
</presource><br>What did you notice this time?<br>
=== Part 4 - Evaluation ===
13,420
edits

Navigation menu