Changes

Jump to: navigation, search

OPS435 Python Lab 1

144 bytes added, 13:15, 1 August 2017
String Variables
<li style="margin-left:25px;">
Perform the following steps to evaluate this unit. Run these check scripts regularly as you work through the labs, they may give you hints if you get stuck.</li>
:<sourcelang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
:'''Perform the following steps:'''
:# Using a text editor, open a new text file called '''lab1a.py''':<sourcelang="bash">
vim ~/ops435/lab1/lab1a.py
</source>
:#Write the following code into our python file. Note the she-bang line at the top of the file to run this script in the python3 environment. You will need to add this she-bang line for all python scripts you create for this course.<sourcelang="python">
#!/usr/bin/env python3
</source>
:#Now, from the Bash shell we will give it the correct Linux permissions in order to run your newly-created python script. This is just showing the multiple ways you can use this python script. You are not required to have IPython running on a system, however hopefully we can use IPython's powerful features to our advantage.
:# Run your python script by issuing the following commands:<sourcelang="bash">
chmod 755 ~/ops435/lab1/lab1a.py
python3 ~/ops435/lab1/lab1a.py
</source>
:#Download the check script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
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>
How old are you Isaac?
</source>Try the checking script as you are working through a script to sometimes get hints.<br><br>
<ol><li value="7" 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