Python Data Science Toolbox (Part 2) By/With Kyesswa Steven
Run the hidden code cell below to import the data used in this course.
Notes Taken By Kyesswa Steven
On 10/27/2023
I Shall be adding notes on the concepts i've learned and code cells with code i want to keep.
Introduction To Iterators & Iterables In Python Data Science ToolBox Part 2.
Iterators & Iterables
Iterable:
An iterable
is an object
that can be looped over, meaning you can iterate through its elements one at a time.
Iterables can be sequences, collections, or any object that can be used in a loop.
They are typically data structures that contain a collection of values, such as lists, tuples, strings, dictionaries, and more.
Example Python Code Of `iterables':
In the example below, my_Steven_list is an iterable, and we can loop through its elements.
my_Steven_list = [1, 2, 3, 4, 5] for item in my_Steven_list: print(item)
`Iterator:
An iterator is an object that allows you to access elements of an iterable one at a time in a sequential manner. It maintains a state to keep track of the current element and can be used with the next() function. Iterators are typically created from iterables and provide a way to lazily generate values on-the-fly, which can be memory-efficient for large datasets.`
In the example below, my_Steven_iterator is an iterator created from an iterable, and we use next() to access its elements sequentially.
my_Steven_iterator = iter( [ 1, 2, 3, 4, 5 ] ) print( next( my_Steven_iterator ) ) # Output: 1 print( next( my_Steven_iterator ) ) # Output: 2
Code Below:
# Code By Kyesswa Steven
#
# 10/27/2023
#
# Example Python Code Of `iterables' & Iterators As Noted As Explained Above:
# Starting with `iterable(s)'
my_Steven_list = [ 1, 2, 3, 4, 5 ]
for item in my_Steven_list:
print( "Starting with `iterable(s) ", item )
# Ending with `iterator(s)'
my_Steven_iterator = iter( [ 1, 2, 3, 4, 5 ] )
print( "Ending with `iterator(s) ", next( my_Steven_iterator ) ) # Output: 1
print( "Ending with `iterator(s) ",next( my_Steven_iterator ) ) # Output: 2
Iterators as function arguments
You've been using the iter()
function to get an iterator object, as well as the next()
function to retrieve the values one by one from the iterator object.
There are also functions that take iterators and iterables as arguments. For example, the list()
and sum()
functions return a list and the sum of elements, respectively.
In this exercise, you will use these functions by passing an iterable from range()
and then printing the results of the function calls.
Instructions
- Create a range object that would produce the values from 10 to 20 using
range()
. Assign the result to values. - Use the
list()
function to create a list of values from the range object values. Assign the result to values_list. - Use the
sum()
function to get the sum of the values from 10 to 20 from the range object values. Assign the result to values_sum.
Code Below:
# Code By DataCamp Obviously, Maybe Modified By Kyesswa Steven
#
# 10/27/2023
#
# Create a range object: values
values = range( 10, 21 )
# Print the range object
print( "values ", values )
# Create a list of integers: values_list
values_list = list( values )
# Print values_list
print( "values_list ", values_list )
# Get the sum of values: values_sum
values_sum = sum( values )
# Print values_sum
print( "values_sum ", values_sum )
Using enumerate Exercise
You're really getting the hang of using iterators, great job!
You've just gained several new ideas on iterators from the last video and one of them is the enumerate()
function. Recall that enumerate()
returns an enumerate object that produces a sequence of tuples, and each of the tuples is an index-value pair.
In this exercise, you are given a list of strings mutants and you will practice using enumerate()
on it by printing out a list of tuples and unpacking the tuples using a for loop.
Instructions
- Create a list of tuples from mutants and assign the result to mutant_list. Make sure you generate the tuples using
enumerate()
and turn the result from it into a list usinglist()
. - Complete the first for loop by unpacking the tuples generated by calling
enumerate()
on mutants. Use index1 for the index and value1 for the value when unpacking the tuple. - Complete the second for loop similarly as with the first, but this time change the starting index to start from 1 by passing it in as an argument to the start parameter of
enumerate()
. Use index2 for the index and value2 for the value when unpacking the tuple.
Code Below:
# Code By DataCamp Obviously, Maybe Modified By Kyesswa Steven
#
# 10/27/2023
#
# Create a list of strings: mutants
mutants = ['charles xavier',
'bobby drake',
'kurt wagner',
'max eisenhardt',
'kitty pryde']
# Create a list of tuples: mutant_list
mutant_list = list( enumerate(mutants) )
# Print the list of tuples
print( "Printing list of tuples ", "from mutant_list ", mutant_list )
# Unpack and print the tuple pairs
for index1, value1 in enumerate(mutants):
print( "Unpack and print the tuple pairs ", index1, value1 )
# Change the start index
for index2, value2 in enumerate(mutants, start=1):
print( "Change the start index ", index2, value2 )
Using zip Exercise
Another interesting function that you've learned is zip()
, which takes any number of iterables and returns a zip object that is an iterator of tuples. If you wanted to print the values of a zip object, you can convert it into a list and then print it. Printing just a zip object will not return the values unless you unpack it first. In this exercise, you will explore this for yourself.
Three lists of strings are pre-loaded: mutants, aliases, and powers. First, you will use list()
and zip()
on these lists to generate a list of tuples. Then, you will create a zip object using zip()
. Finally, you will unpack this zip object in a for loop to print the values in each tuple. Observe the different output generated by printing the list of tuples, then the zip object, and finally, the tuple values in the for loop.
Instructions
- Using
zip()
withlist()
, create a list of tuples from the three lists mutants, aliases, and powers (in that order) and assign the result to mutant_data. - Using
zip()
, create a zip object called mutant_zip from the three lists mutants, aliases, and powers. - Complete the for loop by unpacking the zip object you created and printing the tuple values. Use value1, value2, value3 for the values from each of mutants, aliases, and powers, in that order.
Code Below:
# Code By DataCamp Obviously, Maybe Modified By Kyesswa Steven
#
# 10/27/2023
#
# Define the lists of mutants, aliases, and powers
mutants = ['Mutant1', 'Mutant2', 'Mutant3']
aliases = ['Mr.Nimbus', 'Rick-Sanchez', 'Noob-noob']
powers = ['Ocean', 'Superior-Intellect', 'Cool-Guy']
# Create a list of tuples: mutant_data
mutant_data = list(zip(mutants, aliases, powers))
# Print the list of tuples
print("list of tuples ", mutant_data)
# Create a zip object using the three lists: mutant_zip
mutant_zip = zip(mutants, aliases, powers)
# Print the zip object
print("zip object ", mutant_zip)
# Unpack the zip object and print the tuple values
for value1, value2, value3 in mutant_zip:
print("tuple values", value1, value2, value3)
Using * and zip to 'unzip' Exercise
You know how to use zip()
as well as how to print out values from a zip object. Excellent!
Let's play around with zip() a little more. There is no unzip function for doing the reverse of what zip() does. We can, however, reverse what has been zipped together by using zip() with a little help from *! * unpacks an iterable such as a list or a tuple into positional arguments in a function call.
In this exercise, you will use * in a call to zip()
to unpack the tuples produced by zip()
.
Two tuples of strings, mutants and powers have been pre-loaded.
Instructions
- Create a zip object by using
zip()
on mutants and powers, in that order. Assign the result to z1. - Print the tuples in z1 by unpacking them into positional arguments using the * operator in a
print()
call. - Because the previous
print()
call would have exhausted the elements in z1, recreate the zip object you defined earlier and assign the result again to z1. - 'Unzip' the tuples in z1 by unpacking them into positional arguments using the * operator in a
zip()
call. Assign the results to result1 and result2, in that order. - The last
print()
statements prints the output of comparing result1 to mutants and result2 to powers. Click Submit Answer to see if the unpacked result1 and result2 are equivalent to mutants and powers, respectively.
Code Below:
# Code By DataCamp Obviously, Maybe Modified By Kyesswa Steven
#
# 10/27/2023
#
# Create a zip object from mutants and powers: z1
z1 = zip( mutants, powers )
# Print the tuples in z1 by unpacking with *
print( *z1 )
# Re-create a zip object from mutants and powers: z1
z1 = zip( mutants, powers )
# 'Unzip' the tuples in z1 by unpacking with * and zip(): result1, result2
result1, result2 = zip( *z1 )
# Check if unpacked tuples are equivalent to original tuples
print(result1 == mutants)
print(result2 == powers)
Writing list comprehensions Exercise
You now have all the knowledge necessary to begin writing list comprehensions! Your job in this exercise is to write a list comprehension that produces a list of the squares of the numbers ranging from 0 to 9.
Instructions
Using the range of numbers from 0 to 9 as your iterable and i as your iterator variable, write a list comprehension that produces a list of numbers consisting of the squared values of i.
Code Below
# Code By DataCamp Obviously, Maybe Modified By Kyesswa Steven
#
# 10/29/2023
#
# Create list comprehension: squares
squares = [i ** 2 for i in range(10)]
# Print the list of squared values
print(squares)