Changes

Jump to: navigation, search

OPS435 Python Lab 1

32 bytes added, 02:32, 7 January 2019
INVESTIGATION 3: WRITING PYTHON SCRIPTS
= INVESTIGATION 3: WRITING PYTHON SCRIPTS =
:In this investigation we will start writing our very first python scripts. These will be very basic and help us practice syntax and foundation skills, such as: outputting text to the screen, storing data inside variablesobjects, and using math operators.
=== PART 1 - Common Introductory Python Functions ===
====Creating the introductory "Hello World" Script====
You will learn to create a simple python script in this section. This python script will just print the text "hello world". The "hello world" an old traditional first program students usually are taught to create, which is based on the first programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and Brian Kernighan. You will learn how to run the python script in the ipython3 python3 shell as well as learn how to run the python script from the bash shell.
:'''Perform the following steps:'''
</ol>
=== PART 2 - Working with Variables Python Objects ===
:A variable In Python, an object is used to store data for use later in the program. This data can be a string, integer, decimal number, characters, etc. We will only be covering '''string''' and '''integer''' variables in this lab. You will learn and use other variable python object types in future labs.
==== String Variables Objects ====
:String variables objects contain text to be used in your program. Examples of strings could be user-names, full-names, item descriptions, etc. We will now demonstrate to assign a string to a variable an object and how to display contents stored in a string variablesobject.
:'''Perform the following steps:'''
:#Create a python script (called lab1b.py) and first - start with a few simple things to try:
:#Let's make a new variable object containing a value:<source lang="python">
name = 'Thomas'
</source>
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 object is always outside the quotes, or it will not resolve to a value.<source lang="python">
print('I have a friend named ' + name)
</source>
:#To gain practice, complete your python script with the following content and details:
::::* The script should have a '''Shebang line''' like you did for your lab1a.py python script
::::* The script should use a single variable object called "name"::::* The value of the "name" variable object should be "Isaac"
::::* The script, when executed, should print out "How old are you Isaac?"
::::*Sample run: <source>
python3 ./CheckLab1.py -f -v lab1b
</source></li>
<li style="margin-left:25px;"> Before proceeding, make certain that you identify any and all errors in "lab1b.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li></ol>
==== Integer VariablesObjects====
:Integer variables In Python, integer objects are used to store a an integer numbers that can be used for mathematical operations (discussed in the next section). Integers do NOT contain decimals, and they can be signed (+ or -) or unsigned. Here we will store integers in a variableobject, perform math operations, and display the results.
:'''Perform the following steps:'''
:#Create a python script (called lab1c.py) and first - start with a few simple things to try:
:#Lets create some new variables objects to play with.<source lang="python">
num1 = 5
num2 = 10
</source>
:#You can print the values in those variablesinteger objects:<source lang="python">
print(num1)
print(num2)
</source>
:#Now we will make a new variable integer object and try some math:<source lang="python">
sum = num1 + num2
</source>This will add the values contained in the variables integer objects together, providing a sum. However you will note that there is no output. Let's inspect the new value:<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>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 variableobject, your variable object is still a an intergerobject.<br><br>
:# Issue the following:<source lang="python">
print('The sum is: ' + str(sum))
:#To gain practice, complete your python script with the following features:
:::* The script should have a Shebang line.
:::* The script should have a variable an object called '''name''':::* The script should have a variable an object called '''age''':::* The value of the '''name''' variable object should be '''Isaac''':::* The variable object '''age''' should contain a integer:::* The value of the '''age''' variable object should be '''72'''
:::* The script, when executed, should print out "Isaac is 72 years old!"
:::Example run: <source>
python3 ./CheckLab1.py -f -v lab1c
</source></li>
<li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1c.py". When the check script tells you everything is "ok", you may procede proceed to the next step.</li>
</ol>
:#To gain practice, complete your script with the following content and details:
::* The script should have a Shebang line.
::* The variable object '''x''' should contain a integer with the value '''10'''::* The variable object '''y''' should contain a integer with the value '''2'''::* The variable object '''z''' should contain a integer with the value '''5'''::* The script, when executed, should print out "10 + 2 * 5 = 20" (the printout should change if the values in the variables objects change)
:::Example run: <source>
cd ~/ops435/lab1/
1,760
edits

Navigation menu