Changes

Jump to: navigation, search

OPS435 Python Lab 1

402 bytes removed, 23:46, 16 January 2018
Integer Variables
:'''Perform the following steps:'''
:#Enter the following command Create a python script (called lab1c.py) and first - start with a few simple things to access the python prompttry:<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 You can print the following commandsvalues in those variables:<source lang="python">print(num1)print(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 Let's 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>Does this value look right? Are you sure?<br>
:#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/>:#To gain practice, create a complete your python script (called lab1c.py) with the following content and detailsfeatures:
:::* The script should have a Shebang line.
:::* The script should have a variable called '''name'''
:::Example run: <source>
cd ~/ops435/lab1/
run ./lab1c.py
Isaac is 72 years old!
</source><br>Try the check script as you are working through a script to sometimes get hints.<br>

Navigation menu