<jupyter_start><jupyter_text>VGP 245  Class 2 - Collections & iterationcollections (list, dict, set, tuple) for loops, Manipulating listsList variables, which can contain multiple items of data, are widely used in Python programming and have several “methods” that can be “dot-suffixed” to the list name for manipulation:List Method - Description:+ list.append(x) -  Adds item x to the end of the list+ list.extend(L) -   Adds all items in list L to the end of the list+ list.insert(i,x) -  Inserts item x at index position i+ list.remove(x) -  Removes first item x from the list+ list.pop(i) -   Removes item at index position I and returns it+ list.index(x) -   Returns the index position in the list of the first item x+ list.count(x) -   Returns the number of times x appears in the list+ list.sort() -   Sort all list items, in place+ list.reverse() -   Reverse all list items, in placePython also have a useful len(L) function that returns the length of the list L as the total number of elements it contains.  Like the index() and count() methods, the returned value is numeric so cannot be directly concatenated to a text string for output.Individual list items can be delete by specifying their index number to the Python del(i) function.  Restricting lists TupleThe values in a regular list can be changed as the program proceeds (they are “mutable”), but a list can be created with fixed “immutable” values that cannot be changed by the program.A restrictive immutable Python list is known as a “tuple” and is created by assigning values as a comma-separated list between parentheses in a process known as “tuple packing”:colors-tuple = ( ‘red’, ‘green’, ‘red’, ‘blue’, ‘red’ )An individual tuple element can be referenced using the tuple name followed by square brackets containing that element’s index number.  Usefully, all values stored inside a tuple can be assigned to individual variables in a process known as “sequence unpacking”:a, b, c, d, e = colors-tuple SetThe values in a regular list can be repeated in its elements, as in the tuple above, but a list of unique values can be created where duplication is not allowed.  A restrictive Python list of unique values is known as a “set” and is created by assigning values as a comma-separated list between curly brackets (braces):phonetic-set = { ‘Alpha’, ‘Bravo’, ‘Charlie’ }Individual set elements cannot be referenced using the set name followed by square brackets containing an index number, but instead sets have methods that can be dot-suffixed to the set name for manipulation and comparison:  Repetition For loopsThere are two types of loops: for loops and while loops. For loops are structures as follows:for item in collection:    codeblock     The above for loop iterates through the list or collection of elements.  Unlike other languages, you don't need to use the index operator to use the element.  Think of this as the c++11 for (auto item : vector) syntax or in C the foreach loop.  To iterate through a tuple of numbers, you can use the __range__.  Range takes can take in 1, 2, or 3 arguments.  range(10) will return (0,1,2,3,4,5,6,7,8,9), and range(1,10) will return (1,2,3,4,5,6,7,8,9), and range(1,10,2) will return (2,4,6,8).  Note it returns a tuple not a list i.e. it's immutable.  If you want it to return a list.  You will need to cast / convert it to the list using list(range(10))for i in range(10):    codeblock<jupyter_code>#
# Example for working with loops
#


# define a while loop
count = 10
while count > 0:
    print(count)
    count -= 1

# define a for loop
for i in range(1,11):
    print(i)

print('-' * 50)
# use a for loop over a collection
bag3 = ['a','b','c','d','e']
for item in bag3:
    print(item)
print('*' * 50)

# use the break and continue statements
for i in range(1,10):
    if i == 5:
        continue
    print(i)
    
for i in range(1,10):
    if i == 5:
        break
    print(i)
    
#using the enumerate() function to get index 
for i, v in enumerate(bag3):
    print (i,v)<jupyter_output>10
9
8
7
6
5
4
3
2
1
1
2
3
4
5
6
7
8
9
10
--------------------------------------------------
a
b
c
d
e
**************************************************
1
2
3
4
6
7
8
9
1
2
3
4
0 a
1 b
2 c
3 d
4 e<jupyter_text>While loopsWhile loops, can be controlled using __break__, and __continue__.  Break gets out of the loop, and continue is like a shortcut and skips the execution of the rest of the codeblock and continues to the next iteration.  In Python, you can also use __else__.  The codeblock in the else statement is executed when the while loop condition is no longer met.  It is skipped if the __break__ is called first.<jupyter_code># simple fibonacci series
# the sum of two elements defines the next set

a, b = 0, 1
while b < 1000:
    print(b, end = ' ', flush = True)
    a, b = b, a + b

print() # line ending<jupyter_output><empty_output><jupyter_text>Example of while loop<jupyter_code>words = ['one', 'two', 'three', 'four', 'five']

n = 0
while(n < 5):
    print(words[n])
    n += 1<jupyter_output><empty_output><jupyter_text>SetThere are different methods included in the set objects:  __difference__: which shows the only the differences i.e. the elements that exist in set1 but not set2.  __intersect__: which show the similarities between the two sets i.e. element that are in both set1 and set2.Also sets does not contain repeated values.  Please note by defining a variable={}, does not give you a set, that returns a dictionary.  To create an empty set, you can create a list and convert it to a set i.e. set([]).<jupyter_code>bag = {'apple', 'apple', 'berries'}
bag
bag.add('cherries')
bag.add('durian')
bag.add('pineapple')
bag.add('apricot')
bag
print(bag)
bag2 = {'apple','water melon','honey dew', 'mango','berries'}
bag2
print(bag2)
bag.intersection(bag2)
bag.difference(bag2)
bag2.difference(bag)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09732856899778798, 'fraction_numerical': 0.02126935511315297, 'mean_word_length': 3.9478114478114477, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '318200', 'license': 'MIT', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1708, 'n_words': 886, 'path': 'Class 2/dictionaries.ipynb', 'repo_name': 'ikarusx/vgp245', 'size': '5877'}