Changes

Jump to: navigation, search

OPS435 Python Lab 4

720 bytes added, 04:40, 13 June 2017
no edit summary
== 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 and to do it faster than simply looping over lists.
: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 and to do it faster than simply looping over lists.''Perform the Following Steps'''
:#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.<source>
l1 = [1, 2, 3, 4, 5]
for item in l1:
print(item ** 2)
</source>
 If we would like to :#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 This is using an existing list to create a new list.<source>
l1 = [1, 2, 3, 4, 5]
l2 = []
l2
</source>
 Lets take another step here. Lets move :#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 function that does a lot more processing on each item in the list.<source>
def square(number):
return number ** 2
l2
</source>
  :#The map function can be used to apply a function on each item in a list. This is exactly what we did happened above, however it gives us much better syntax, removes the loop, including the variable we had to create to do that was created inside the loop. This will make our work the script a little more efficient while performing the same task.<source>
def square(number):
return number ** 2
l2
</source>
 :#The above map function required us to provide it with requires a function, and a list. This meant that before we map() could use map we be used a function needed to define a function be defined earlier in the script. We This entire process can avoid this entire function definition be avoided 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.
<source>
square = lambda x: x ** 2
l2
</source>
 :#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 Fix this and remove by removing 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.<source>
l1 = [1,2,3,4,5]
l2 = list(map(lambda x: x ** 2, l1))
l2
</source>
:#Using the list comprehensions above our code will be faster and more efficient than using multiple variables and loops.
= INVESTIGATION 2: STRINGS =
== 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.
<source>
str1 = 'Paul'
str2 = 'Atreides'
str3 = str1 + ' ' + str2
str3
</source>
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.
<source>
str1 = 'Paul'
str2 = 'Atreides'
str3 = str1 + ' ' + str2 + ' ' + 'I'
str3
str3 = str1 + ' ' + str2 + ' ' + 'I'*3
str3
</source>
-->
== PART 1 - String Manipulation ==
198
edits

Navigation menu