<jupyter_start><jupyter_text>Chapter 2  Exercise 15: Introduction to the Iterator  1. generate a list that will contain 10000000: ones<jupyter_code>big_list_of_numbers = [1 for x in range(0, 10000000)]
big_list_of_numbers<jupyter_output><empty_output><jupyter_text>2. Check the size of this variable<jupyter_code>from sys import getsizeof 

getsizeof(big_list_of_numbers)<jupyter_output><empty_output><jupyter_text>3. Use an iterator to reduce memory utilization<jupyter_code>from itertools import repeat 

small_list_of_numbers = repeat(1, times=10000000) 

getsizeof(small_list_of_numbers)<jupyter_output><empty_output><jupyter_text>4. Loop over the newly generated iterator<jupyter_code>for i, x in enumerate(small_list_of_numbers):  

    print(x) 

    if i > 10: 

        break<jupyter_output>1
1
1
1
1
1
1
1
1
1
1
1<jupyter_text>5. lookup several function definitions using  ? operator in jupyter notebook.<jupyter_code>from itertools import (permutations, combinations, dropwhile, repeat, 

zip_longest) 

permutations?
combinations?
dropwhile?
repeat?
zip_longest?<jupyter_output><empty_output><jupyter_text>Exercise 16: Implementing a Stack in Python  1.  Define an empty stack and load the json file.<jupyter_code>import pandas as pd
df = pd.read_json (r'Chapter02/users.json')
df
stack = []<jupyter_output><empty_output><jupyter_text>2. Append another value to the stack<jupyter_code>output = df.apply(lambda row : stack.append(row["email"]),  axis=1)
stack<jupyter_output><empty_output><jupyter_text>3. Use the append method to add an element in the stack.<jupyter_code>stack.append(“<EMAIL>”) 
stack<jupyter_output><empty_output><jupyter_text>4. Read a value from our stack using the pop method.<jupyter_code>tos = stack.pop() 
tos<jupyter_output><empty_output><jupyter_text>5. Append hello to the stack<jupyter_code>stack.append("Hello") 
stack<jupyter_output><empty_output><jupyter_text>Exercise 17: Implementing a Stack Using User-Defined Methods  1. First, define two functions, stack_push and stack_pop.<jupyter_code>def stack_push(s, value): 
    return s + [value] 

def stack_pop(s): 
    tos = s[-1] 
    del s[-1] 
    return tos 

 
url_stack = [] 
url_stack<jupyter_output><empty_output><jupyter_text>2. create a string with a few urls in it<jupyter_code>wikipedia_datascience = """Data science is an interdisciplinary 
field that uses scientific methods, 
processes, algorithms and systems to extract 
knowledge [https://en.wikipedia.org/wiki/Knowledge] and i
nsights from data [https://en.wikipedia.org/wiki/Data] in various
forms, both structured and unstructured,similar to data mining 
[https://en.wikipedia.org/wiki/Data_mining]"""<jupyter_output><empty_output><jupyter_text>3. Find the length of the string<jupyter_code>len(wikipedia_datascience)<jupyter_output><empty_output><jupyter_text>4. Convert this string into a list by using the split method from the string, and then calculate its length<jupyter_code>wd_list = wikipedia_datascience.split() 
wd_list
len(wd_list)<jupyter_output><empty_output><jupyter_text>5. Use a for loop to go over each word and check whether it is a URL<jupyter_code>for word in wd_list: 
    if word.startswith("[https://"): 
        url_stack = stack_push(url_stack, word[1:-1])
        print(word[1:-1])<jupyter_output>https://en.wikipedia.org/wiki/Knowledge
https://en.wikipedia.org/wiki/Data
https://en.wikipedia.org/wiki/Data_mining<jupyter_text>6. Print the value of the stack<jupyter_code>print(url_stack)<jupyter_output>['https://en.wikipedia.org/wiki/Knowledge', 'https://en.wikipedia.org/wiki/Data', 'https://en.wikipedia.org/wiki/Data_mining']<jupyter_text>7. Iterate over the list and print the URLs one by one by using the stack_pop function<jupyter_code>for i in range(0, len(url_stack)): 
    print(stack_pop(url_stack))<jupyter_output>https://en.wikipedia.org/wiki/Data_mining
https://en.wikipedia.org/wiki/Data
https://en.wikipedia.org/wiki/Knowledge<jupyter_text>8. Print it again to make sure that the stack is empty after the final for loop<jupyter_code>print(url_stack)<jupyter_output>[]<jupyter_text>Exercise 18: Lambda Expression   1. Import the math package<jupyter_code>import math<jupyter_output><empty_output><jupyter_text>2. Define two functions, my_sine and my_cosine<jupyter_code>def my_sine(): 
    return lambda x: math.sin(math.radians(x)) 

def my_cosine(): 
    return lambda x: math.cos(math.radians(x))<jupyter_output><empty_output><jupyter_text>3. Define sine and cosine for our purpose<jupyter_code>sine = my_sine() 
cosine = my_cosine() 
math.pow(sine(30), 2) + math.pow(cosine(30), 2)<jupyter_output><empty_output><jupyter_text>Exercise 19: Lambda Expression for Sorting  1. define a list of tuples<jupyter_code>capitals = [("USA", "Washington"), ("India", "Delhi"), ("France", "Paris"), ("UK", "London")] 
capitals<jupyter_output><empty_output><jupyter_text>2. Sort this list by the name of the capitals of each country, using a simple lambda expression.<jupyter_code>capitals.sort(key=lambda item: item[1]) 
capitals<jupyter_output><empty_output><jupyter_text>Exercise 20: Multi-Element Membership Checking  1. Create a list_of_words list with words scraped from a text corpus<jupyter_code>list_of_words = ["Hello", "there.", "How", "are", "you", "doing?"] 
list_of_words<jupyter_output><empty_output><jupyter_text>2. Verify whether this list contains all the elements from another list<jupyter_code>check_for = ["How", "are"] 
check_for<jupyter_output><empty_output><jupyter_text>3. Use the in keyword to check membership in the list list_of_words:<jupyter_code>all(w in list_of_words for w in check_for)<jupyter_output><empty_output><jupyter_text>Exercise 21: Implementing a Queue in Python  1. Create a Python queue with the plain list methods<jupyter_code>%%time 
queue = [] 

for i in range(0, 100000): 
    queue.append(i) 
print("Queue created") 
queue<jupyter_output>Queue created
CPU times: user 8.78 ms, sys: 0 ns, total: 8.78 ms
Wall time: 8.47 ms<jupyter_text>2. Use the pop function to empty the queue and check items in it<jupyter_code>for i in range(0, 100000): 
    queue.pop(0) 

print("Queue emptied") 
queue<jupyter_output>Queue emptied<jupyter_text>3. Implement the same queue using the deque data structure from Python's collection package<jupyter_code>%%time 

from collections import deque 

queue2 = deque() 
for i in range(0, 100000): 
    queue2.append(i) 
    
print("Queue created") 
for i in range(0, 100000): 
    queue2.popleft() 
    
print("Queue emptied")<jupyter_output>Queue created
Queue emptied
CPU times: user 17.2 ms, sys: 0 ns, total: 17.2 ms
Wall time: 16.6 ms<jupyter_text>Exercise 22: File Operations  1. Import the os module.<jupyter_code>import os<jupyter_output><empty_output><jupyter_text>2. Set few environment variables<jupyter_code>os.environ['MY_KEY'] = "MY_VAL" 
os.getenv('MY_KEY')<jupyter_output><empty_output><jupyter_text>3. Print the environment variable when it is not set<jupyter_code>print(os.getenv('MY_KEY_NOT_SET'))<jupyter_output>None<jupyter_text>4.  Print the os environmen<jupyter_code>print(os.environ)<jupyter_output><empty_output><jupyter_text>Exercise 23: Opening and Closing a File  1. Open a file in binary mode:<jupyter_code>f = open("Alice's Adventures in Wonderland, by <NAME>", "w")
n = f.write('')
f.close()
fd = open("Alice's Adventures in Wonderland, by <NAME>", 

          "rb") 
fd.close()<jupyter_output><empty_output><jupyter_text>Exercise 24: Reading a File Line by Line  1. Open a file and then read the file line by line and print it as we read it<jupyter_code>with open("Alice's Adventures in Wonderland, by <NAME>", 
          encoding="utf8") as fd: 
    for line in fd: 
            print(line)<jupyter_output><empty_output><jupyter_text>2. Duplicate the same for loop, just after the first one<jupyter_code>with open("Alice's Adventures in Wonderland, by <NAME>", 
          encoding="utf8") as fd: 
    for line in fd: 
        print(line) 
    print("Ended first loop") 
    for line in fd: 
        print(line)<jupyter_output>Ended first loop<jupyter_text>Exercise 25: Write to a File  1. Use the write function from the file descriptor object<jupyter_code>data_dict = {"India": "Delhi", "France": "Paris", "UK": "London", 
             "USA": "Washington"} 

with open("data_temporary_files.txt", "w") as fd: 
    for country, capital in data_dict.items(): 
        fd.write("The capital of {} is {}\n".format( 
            country, capital))<jupyter_output><empty_output><jupyter_text>2. Read the file using<jupyter_code>with open("data_temporary_files.txt", "r") as fd: 
    for line in fd: 
        print(line)<jupyter_output>The capital of India is Delhi

The capital of France is Paris

The capital of UK is London

The capital of USA is Washington<jupyter_text>3. Use the print function to write to a file<jupyter_code>data_dict_2 = {"China": "Beijing", "Japan": "Tokyo"} 
with open("data_temporary_files.txt", "a") as fd: 
    for country, capital in data_dict_2.items(): 
        print("The capital of {} is {}".format( 
            country, capital), file=fd)<jupyter_output><empty_output><jupyter_text>4. Read the file<jupyter_code>with open("data_temporary_files.txt", "r") as fd: 
    for line in fd: 
        print(line)<jupyter_output>The capital of India is Delhi

The capital of France is Paris

The capital of UK is London

The capital of USA is Washington

The capital of China is Beijing

The capital of Japan is Tokyo
{'fraction_non_alphanumeric': 0.10864040660736976, 'fraction_numerical': 0.018742058449809404, 'mean_word_length': 5.509303928325293, 'pattern_counts': {'":': 6, '<': 154, '<?xml version=': 0, '>': 155, 'https://': 13, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '40', 'dir': 'jupyter-structured-clean-dedup', 'id': '512336', 'license': 'MIT', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3255, 'n_words': 876, 'path': 'Chapter02/.ipynb_checkpoints/chapter2-checkpoint.ipynb', 'repo_name': 'arifmudi/The-Data-Wrangling-Workshop', 'size': '9481'}