<jupyter_start><jupyter_text>In-class exercise Module 2 Answers (Python Basics)<jupyter_code># Q.1
import numpy as np
array1 = np.random.randn(100)
# Q.2
import matplotlib.pyplot as plt
plt.plot(array1)
plt.show()        # use plt.show() to show the plot
# Q.3
import re
re.search(r"apple", "I love eating apples")
# Q.4
def multiply(a,b):
    return a*b

multiply(3,10)    
# Q.5
multiply = lambda a,b : a*b
print(multiply(3,10))
# Q.6
list1 = list(range(6,31))      # create a list from the range function. To include 30, it should go up to 31
list2 = list(range(16,41))
list2.reverse()                # reverse the order of list2

for a,b in zip(list1,list2):   # use zip function to put the combine the two lists     
    if b>a:
        print(b)
# Q.7
names = [("Thomson", "Alex"), ("Wright", "Bob"), ("White", "Carol"), ("Young", "Tim"), ("Payne", "Sam"), ("Brown", "Mary")]
last_names=[]
for a,b in names:
    last_names.append(a)

last_names
# Q.8
list1 = []
for x in range(1,11):
    for y in range(2,12):
        list1.append((x,y))

list1

# Q.9
names = ["Alice", "Bob", "Charlie", "Debbie", "John", "Sam", "Cathy", "Linda", "Yoyo"]

for i,j in enumerate(names):
    if len(j)>4:
        print(i,j)
    
# Q.10
names = ["Alice", "Bob", "Charlie", "Debbie", "John", "Sam", "Cathy", "Linda", "Yoyo"]
import re

for i,j in enumerate(names):
    if re.search(r"[aeiou]$",j):
        print(i,j)
    
# Q.11
first_name=["Mary","James","Peter","Yoyo"]
last_name=["Brown","White","Reed","Holt"]

names = zip(first_name, last_name)
list(names)
# Q.12
info = [('Mary', 45), ('James', 30), ('Peter', 65), ('Yoyo', 5), ('Tom',25)] 
list1 = []
for i,j in info:
    if j > 28:
        list1.append((i,j))

list1
# Q.13
list1 = list(range(100))
print(list1[1:6])
print((list1[1],list1[5]))
print(list1[-5:])
print(list1[2::2])
print(list1[::-1])
# Q.14
list1 = [i**2 for i in range(1,11)]
list1.reverse()
print(list1)
print(25 in list1)
print(45 in list1)
# Q.15
import numpy as np
list1 = np.random.randint(0,30,100)
list2 = []
for i in list1:
    if i%3 == 0:            # use % to find the remainder
        list2.append(i)

print(list1)
print(list2)
list2.sort()
print(list2)

# Q.16
dict1 ={"Name" : ["John","Sam"],
        "Age": [45,50],
        "Income": [50000, 60000],
        "Children": [("Peter","Sam") , ("Tom","Mary")],
        "Years of experience":[16.5,20]}

for k,v in dict1.items():
    print(k,v[0])
    
print()           # print a blank line

for k,v in dict1.items():
    print(k,v[1])
# Q.17
{i:i**2 for i in range(10)}
# Q.18
str_1 = "Hello World! I am learning data wrangling"
print(str_1[2:10])
print(str_1.find("data"))
print(str_1.replace("Hello","Hi"))
list1 = str_1.split(" ")
print(list1)
print("|".join(list1))<jupyter_output>llo Worl
27
Hi World! I am learning data wrangling
['Hello', 'World!', 'I', 'am', 'learning', 'data', 'wrangling']
Hello|World!|I|am|learning|data|wrangling
{'fraction_non_alphanumeric': 0.20604395604395603, 'fraction_numerical': 0.057348901098901096, 'mean_word_length': 3.76759410801964, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '306981', 'license': 'Apache-2.0', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1182, 'n_words': 307, 'path': 'inclass-exercises/module-2.ipynb', 'repo_name': 'RealDogDad/ad450-data-science', 'size': '2912'}