Difference between revisions of "OPS435 Python Lab 4"

From CDOT Wiki
Jump to: navigation, search
(Part 1 - Tuple)
(Part 2 - Set)
Line 48: Line 48:
 
</pre>
 
</pre>
  
== Part 2 - Set ==
+
== PART 2 - Set ==
 
Sets are another very similar structure to lists, they can also be modified and changed, unlike the tuple. But sets have two unique characteristics, they are unordered, and they cannot have duplicate values. The unordered part provides a added performance from hashing the values, but also means we cannot pull out a specific value at a spefici position. Any duplicate entries will immediately be deleted. Sets however are great tools for doing comparisons, finding differences in multiple sets, or finding similarities. The best part about sets are, they are fast!
 
Sets are another very similar structure to lists, they can also be modified and changed, unlike the tuple. But sets have two unique characteristics, they are unordered, and they cannot have duplicate values. The unordered part provides a added performance from hashing the values, but also means we cannot pull out a specific value at a spefici position. Any duplicate entries will immediately be deleted. Sets however are great tools for doing comparisons, finding differences in multiple sets, or finding similarities. The best part about sets are, they are fast!
  

Revision as of 12:43, 19 May 2017

OBJECTIVES

The first investigation of this lab we will be working with different data structures. These are stored in a similar way to variables and lists, however they can contain a lot more information and are designed for specific purposes. Each structure has its own advantages and disadvantages, this lab will emphasize where those important differences lay. The second investigation will focus closely on strings. We have been using and storing strings since our first class, however in this lab we will dive into the more complex nature of string manipulation. Finally, this lab will cover how to use a variety of different regular expression functions, for searching and input validation.

Python Reference

INVESTIGATION 1 - DATA STRUCTURES

PART 1 - Tuple

A Python Tuple is a number of immutable Python values. This is similar to a list in a lot of ways, except that, you cannot change the values inside.

ipython
t1 = ('Prime', 'Ix', 'Secundus', 'Caladan')
t2 = (1, 2, 3, 4, 5, 6)

Values from a tuple can be retreived in the same way as a list.

t1[0]
t2[2:4]

Or check if a value exists inside a tuple.

'Ix' in t1
'Geidi' in t1

Try changing a tuple value.

t2[1] = 10

Did it work? Once created the tuple values will not be able to change. If you would like a tuple with different values than the tuple you currently have, you must create a new one.

t3 = t2[2:3]

You however can still use most of the basic operations you might expect from tuples.

len(t1)     # list the length of the tuple
t1 * 3      # repitition
t1 + t2     # concatenation, remember this is creating a new tuple, not modifying

Like lists, you can also loop through the values of tuples.

for item in t1:
    print('item: ' + item)

PART 2 - Set

Sets are another very similar structure to lists, they can also be modified and changed, unlike the tuple. But sets have two unique characteristics, they are unordered, and they cannot have duplicate values. The unordered part provides a added performance from hashing the values, but also means we cannot pull out a specific value at a spefici position. Any duplicate entries will immediately be deleted. Sets however are great tools for doing comparisons, finding differences in multiple sets, or finding similarities. The best part about sets are, they are fast!

Lets create a couple sets to work with.

s1 = {'Prime', 'Ix', 'Secundus', 'Caladan'}
s2 = {1, 2, 3, 4, 5}
s3 = {4, 5, 6, 7, 8}

First, try accessing a set through the index.

s1[0]

You should have received a error, this is not how you access data inside a set because they are unordered. Instead you can check to see if a value is inside.

'Ix' in s1
'Geidi' in s1

If you would like to combine sets together you can. Any duplicates that the 2 sets share, will be deleted. Take a close look at which items are shared between the sets.

s2
s3
s2 | s3         # returns a set containing all values from both sets
s2.union(s3)    # same as s2 | s3

Instead of combining sets, we can find out what values are in both sets. This is a intersection between the lists.

s2
s3
s2 & s3             # returns a set containing all values that s2 and s3 share
s2.intersection(s3) # same as s2 & s3


Lets look at how we can compare the values inside sets. First lets find out what items are in s2 but not in s3. This is also called a difference. But notice that it only shows values that s2 contains, specifically values that s3 doesn't have. So this isn't really the true difference between the sets.

s2
s3
s2 - s3             # returns a set containing all values in s2 that are not found s3
s2.difference(s3)   # same as s2 - s3

If we want to see every difference between both sets, we can find the symmetric difference. This will return a set that shows all numbers that both sets do not share together.

s2
s3
s2 ^ s3                     # returns a set containing all values that both sets DO NOT share
s2.symmetric_difference(s3) # same as s2 ^ s3

Since these powerful features can be so useful and efficient, you may want to try applying them to lists. There are some added steps required if you want to use these functions on lists. First convert the list to a set, perform the set comparison or function, convert the set back to a list.

l2 = [1, 2, 3, 4, 5]
l3 = [4, 5, 6, 7, 8]
new_list = list(set(l2).intersection(set(l3)))  # set() can make lists into sets. list() can make sets into lists
new_list

PART 3 - Dictionary

PART 4 - List Comprehension

We've already covered lists to a degree. Lets move into more advanced functions to use and generate lists. This is a very common practice in Python, understanding how to generate, manipulate, and apply functions to items inside a list can be incredibly useful. List comprehension is a way to build new lists from existing list.

Lets start with creating a list and applying some function to each item in the list. The below will print out the square of each item.

l1 = [1, 2, 3, 4, 5]
for item in l1:
    print(item ** 2)

If we would like to store these squares for later use, we can create a new list and append the squares to it. This will generate a new list that contains squared values in the same positions of the first list. What we are doing is using an existing list to create a new list.

l1 = [1, 2, 3, 4, 5]
l2 = []
for item in l1:
    l2.append(item ** 2)
l1
l2

Lets take another step here. Lets move the squaring of numbers out into it's own separate function. While the squaring example is a simple function, this example could include a more complex functions that does a lot more processing on each item in the list.

def square(number):
    return number ** 2

l1 = [1, 2, 3, 4, 5]
l2 = []
for item in l1:
    l2.append(square(item))

l1
l2


The map function can be used to apply a function on each item in a list. This is exactly what we did above, however it gives us much better syntax, removes the loop, including the variable we had to create to do the loop. This will make our work a little more efficient while performing the same task.

def square(number):
    return number ** 2

l1 = [1,2,3,4,5]
l2 = list(map(square, l1))

l1
l2

The above map function required us to provide it with a function, and a list. This meant that before we could use map we needed to define a function earlier in the script. We can avoid this entire function definition through the use of anonymous functions. This is the ability to create a simple function without defining it, and pass it off for use. Below we will use lambda, which will return a function, and we can use that function immediately. The function takes 1 argument x, and it will perform a single operation on x, square it.

square = lambda x: x ** 2
l1 = [1,2,3,4,5]
l2 = list(map(square, l1))

l1
l2

The above code is actually not particularly good, the whole purpose of using lambda here is we were avoiding the function definition and just quickly returning a function. However this does break down exactly what lambda does, it returns a function for use. Lets fix this and remove the square function and just use the return function from lambda. Now remember what map requires? map's first argument is a function, and map's second argument is a list. Here lambda will return a function and provide it as the first argument.

l1 = [1,2,3,4,5]
l2 = list(map(lambda x: x ** 2, l1))

l1
l2

Investigation 2 - Strings

Strings are in their most basic form a list of characters, or a bit of text. Strings store text so that we can use them later. In this section we will cover more than just displaying that text to the screen. Here, we will go over cutting strings into sub-strings, joining strings together, searching through strings, and matching strings against patterns.

Investigation 2 - Part 1 - String Basics

We can concatenate strings using the plus sign. Combining strings together to create a brand new string, strings are immutable just like tuples. This means everytime you change a string, you are actually creating a new string.

str1 = 'Paul'
str2 = 'Atreides'
str3 = str1 + ' ' + str2
str3

Repetition is also a useful tool that can be used with strings. Repetition repeats the string over and over a specific amount of times. This is useful anytime you would manually be typing the same thing over again.

str1 = 'Paul'
str2 = 'Atreides'
str3 = str1 + ' ' + str2 + ' ' + 'I'
str3
str3 = str1 + ' ' + str2 + ' ' + 'I'*3
str3


Investigation 2 - Part 1 - String Manipulation

Investigation 2 - Part 1 - Regular Expressions

LAB 1 SIGN-OFF (SHOW INSTRUCTOR)

Students should be prepared with all required commands (system information) displayed in a terminal (or multiple terminals) prior to calling the instructor for signoff.


Have Ready to Show Your Instructor:
x
x
Lab1 logbook notes completed


Practice For Quizzes, Tests, Midterm & Final Exam

  1. x
  2. x
  3. x