Changes

Jump to: navigation, search

OPS435 Python Lab 4

126 bytes added, 23:01, 16 August 2017
PART 1 - Tuples
== PART 1 - Tuples ==
:Many often confuse a '''tuple''' with a '''list''' (which you learned about in a previous lab). A '''tuple''' is a type of list whose values cannot be changeschanged. In fact, the structure of nothing in a tuple cannot can be changed after it's created (like adding, removing list elements).
:There are many advantages to using tuples when creating Python scripts:
::*'''Data protection''' (eg. values are are NOT allowed to change like income tax rate, social insurance number, etcso you won't modify them accidentally)
::*The data structure in a tuple cannot be changed (eg. '''structure cannot be corrupted''')
::*Tuples can be used as '''keys in data dictionaries''' (which are NOT allowed to change)
::*Tuples allow for '''faster access''' than lists
:Term The term to indicate that a data structure cannot be changed is called '''immutable''' (as opposed to ''"mutable"'' which means the data structure can be changed).
:'''Perform the Following Steps:'''
ipython3
</source>Let's create two tuples, so we can learn how to use them and learn how they differ from lists.<br><br>Note: '''tuples are defined by using parenthesis ( )''' as opposed to '''lists are defined by using square brackets [ ]'''<br><br>
:#Issue the following:<sourcelang="python">
t1 = ('Prime', 'Ix', 'Secundus', 'Caladan')
t2 = (1, 2, 3, 4, 5, 6)
</source>
:#Values from a tuple can be retrieved in the same way as a list. For example, issue the following:<sourcelang="python">
t1[0]
t2[2:4]
</source>
:#You can also check to see whether a value exists inside a tuple or not. To demonstrate, issue the following:<sourcelang="python">
'Ix' in t1
'Geidi' in t1
</source>Let's now see how a tuple differs from a list. We will now create a list and note the difference between them.<br><br>
:#Issue the following to create a list:<sourcelang="python">
list2 = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
</source>
:#See if you can change the value of your list by issuing the following:<sourcelang="python">
list2[0]= 'ica100'
list2[0]
print(list2)
</source>.You should have been successful in changing the value of your list.<br><br>:#Now, try changing the value of your previously-created tuple by issuing:<sourcelang="python">
t2[1] = 10
</source>Did it work? Once created the tuple values will not be able to change.<br><br>If you would like a tuple with different values than the tuple you currently have, then you must create a new one.<br><br>
:#To create a new tuple, issue the following:<sourcelang="python">
t3 = t2[2:3]
</source>
:#You can use most of the basic operations with tuples as you did with lists.<br><br>
:#To demonstrate, issue the following:<sourcelang="python">
len(t1) # list the length of the tuple
t1 * 3 # repetition
t1 + t2 # concatenation, remember this is creating a new tuple, not modifying
</source>
:#Also, as with lists, you can use loops with tuples. Issue the following to demonstrate:<sourcelang="python">
for item in t1:
print('item: ' + item)

Navigation menu