1,760
edits
Changes
→Investigation 4 - exploring Python's built-in data objects
</ol>
= Investigation 4 - exploring Python's built-in Data Objects =:In Python, a data 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''' objects in this lab. You will learn and use other Python built-in data object types in future labs. == String Objects == :String 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 how to assign a string to an object and how to display contents stored in a string object. :'''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 object containing a value:<source lang="python">name = 'Thomas'</source>:#Print the value to the screen:<source lang="python">print(name)</source>:#Think about why this does something different:<source lang="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 two strings together. However, make sure that the name of your 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 object called "name"::::* The value of the "name" object should be "Isaac"::::* The script, when executed, should print out "How old are you Isaac?"::::*Sample run: <source> cd ~/ops435/lab1/ ./lab1b.py 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''':<source lang="bash">cd ~/ops435/lab1/pwd #confirm that you are in the right directoryls CheckLab1.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/CheckScripts/CheckLab1.pypython3 ./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 proceed to the next step.</li></ol> == Integer Objects== :In Python, integer objects are used to store 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 object, 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 two new objects, num1 and num2, to play with.<source lang="python">num1 = 5num2 = 10</source>:#You can print the values in those integer objects:<source lang="python">print(num1)print(num2)</source>:#Now we will make a new object called "sum", and try some math:<source lang="python">sum = num1 + num2</source>This will add the values contained in the integer objects together and assign the result to the object named "sum". However you will note that there is no data show up on the screen. Let's inspect the contents of the new object named "sum":<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 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 concatenate a sting and an integer object, we will have to use another builtin function called "str()" to convert an integer object to a string first. 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 object, your object is still an integer object.<br><br>:# Issue the following:<source lang="python">print('The sum is: ' + str(sum))</source>What did you notice this time?<br />:#To gain practice, complete your python script with the following features::::* The script should have a Shebang line.:::* The script should have an objectcalled '''name''':::* string The script should have an object called '''age''':::* The value of the '''name''' object should be '''Isaac''':::* The object '''age''' should contain a integer:::* The value of the '''age''' objectshould be '''72''':::* The script, when executed, should print out "Isaac is 72 years old!":::Example run: <source>cd ~/ops435/lab1/./lab1c.pyIsaac 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:<source lang="bash">cd ~/ops435/lab1/pwd #confirm that you are in the right directoryls CheckLab1.py || wget https://ict.senecacollege.ca/~raymond.chan/ops435/labs/LabCheckScripts/CheckLab1.pypython3 ./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 proceed to the next step.</li></ol>
= Investigation 5 - exploring on how to get Python to do maths =
:* Math operators