<jupyter_start><jupyter_text>Ex. 5 Complete the script so that it prints out the second item of the list.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[1])<jupyter_output>b<jupyter_text>Ex. 6 Please complete the script so that it prints out a list slice containing items d , e , and f.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[3:6])<jupyter_output>['d', 'e', 'f']<jupyter_text>Ex. 7  Please complete the script so that it prints out the first three items of list letters.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[:3])<jupyter_output>['a', 'b', 'c']<jupyter_text>Ex. 8 Complete the script so that it prints out the letter i  using negative indexing.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[-2])<jupyter_output>i<jupyter_text>Ex. 9 Complete the script so that it prints out a list slice containing the last three items of the list letters.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[-3:])<jupyter_output>['h', 'i', 'j']<jupyter_text>Ex. 10 Complete the script so that it prints out a list slice containing letters a, c, e, g, and i.<jupyter_code>letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[0::2])  #The complete syntax of list slicing is [start:end:step]<jupyter_output>['a', 'c', 'e', 'g', 'i']<jupyter_text>Ex. 11. Ranges<jupyter_code>#help(range)
#range(start, stop[, step])
my_range = range(1, 21)
print(list(my_range))<jupyter_output>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]<jupyter_text>Ex. 12. Ranges<jupyter_code>my_range = range(1, 21)
print([10 * x for x in my_range]) # list compreention<jupyter_output>[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]<jupyter_text>Ex. 13. Ranges<jupyter_code>my_range = range(1, 21)
# print([str(x) for x in my_range]) #or even better:
print(list(map(str, my_range))) # map applies a funtion to an iterable list<jupyter_output>['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']<jupyter_text>Ex. 14. Complete the script so that it removes duplicate items from the list a.<jupyter_code>a = ["1", 1, "1", 2]

#help(set)
print(list(set(a)))<jupyter_output>[1, 2, '1']<jupyter_text>Ex. 15. Create a dictionary that contains the keys a  and b  and their respective values 1  and 2<jupyter_code>my_dict = {"a": 1, "b": 2} #or
my_dict = dict(a=1, b=2)
print(my_dict)<jupyter_output>{'a': 1, 'b': 2}<jupyter_text>Ex. 16. Accessing Dictionary Items<jupyter_code>d = {"a": 1, "b": 2}
print(d["b"])<jupyter_output>2<jupyter_text>Ex. 17. Calculate the sum of the values of keys a  and b<jupyter_code>d = {"a": 1, "b": 2, "c": 3}
print(d["a"] + d["b"])<jupyter_output>3<jupyter_text>Ex. 18. Dictionary<jupyter_code>d = {"Name": "John", "Surname": "Smith"}
print(d["Surname"])<jupyter_output>Smith<jupyter_text>Ex. 19. Dictionary<jupyter_code>d = {"a": 1, "b": 2}
d["c"] = 3
print(d)<jupyter_output>{'a': 1, 'b': 2, 'c': 3}<jupyter_text>Ex. 20. Dictionary<jupyter_code>d = {"a": 1, "b": 2, "c": 3}
v = d.values()
print(sum(v))<jupyter_output>6<jupyter_text>Ex. 21. Dictionary Compreencion<jupyter_code>d = {"a": 1, "b": 2, "c": 3}

print({k:v for (k,v) in d.items() if v <= 1})<jupyter_output>{'a': 1}<jupyter_text>Ex. 22.<jupyter_code>from pprint import pprint
d = {"a": list(range(1, 11)), "b": list(range(11, 21)), "c": list(range(21, 31))}
pprint(d)<jupyter_output>{'a': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 'b': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
 'c': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]}<jupyter_text>Ex. 23. Multilevel Indexing. \ Access the third value of key b  from the dictionary.<jupyter_code>d = {"a": list(range(1, 11)), "b": list(range(11, 21)), "c": list(range(21, 31))}
print(d["b"][2])<jupyter_output>13<jupyter_text>Ex. 24.<jupyter_code>d = dict(a = list(range(1, 11)), b = list(range(11, 21)), c = list(range(21, 31)))
for k, v in d.items():
    print(k, " has value ", v)<jupyter_output>a  has value  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b  has value  [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
c  has value  [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]<jupyter_text>Ex. 25.<jupyter_code>import string
print([x for x in string.ascii_lowercase])<jupyter_output>['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']<jupyter_text>Ex. 26.<jupyter_code>for i in range(1, 11):
    print(i)<jupyter_output>1
2
3
4
5
6
7
8
9
10<jupyter_text>Ex. 27.Create a function that calculates acceleration given initial velocity v1, final velocity v2, start time t1, and end time t2.<jupyter_code>def accel(v1, v2, t1, t2):
    return (v2 - v1)/(t2 - t1)

accel(0, 10, 0, 20)<jupyter_output><empty_output><jupyter_text>Ex. 28.<jupyter_code>s = "a"<jupyter_output><empty_output><jupyter_text>Ex. 29.<jupyter_code>from math import pi

def volume(h, r=10):
    return (4 * pi * r**3 - pi * h**2 * (3 * r - h))/3

print(volume(2))<jupyter_output>4071.5040790523713<jupyter_text>Ex. 30.<jupyter_code>len(("teste 123").split())<jupyter_output><empty_output><jupyter_text>Ex. 34.- [x] teste<jupyter_code>def foo(): 
        global c
        c = 1 
        return c 
    foo() 
    print(c)<jupyter_output>1<jupyter_text>35. Split a sentence<jupyter_code>my_phrase = input("type a phrase: ")

def w_counter(my_phrase):
   return len(my_phrase.split())

w_counter(my_phrase)<jupyter_output>type a phrase: qwe qwe<jupyter_text>36. Read a txt file and count words<jupyter_code>def w_counter():    
    with open("words1.txt", "r") as file:
        return len(file.read().split())

w_counter()<jupyter_output><empty_output><jupyter_text>37.<jupyter_code>l =[]
def w_counter():    
    with open("words2.txt", "r") as file:
        my_file = str(file.read()).replace(",", " ")
        print(my_file)
        return len(my_file.split())

w_counter()
# solution 2 with "re" (regular expression matching operations)

import re
     
def count_words_re(filepath):
    with open(filepath, 'r') as file:
        text = file.read()
    string_list = re.split(",| ", text)
    return len(string_list)
     
print(count_words_re("words2.txt"))<jupyter_output>10<jupyter_text>38.<jupyter_code>import math

math.sqrt(9)<jupyter_output><empty_output><jupyter_text>39.<jupyter_code>import math
print(math.cos(1))
help(math.pow)<jupyter_output>Help on built-in function pow in module math:

pow(x, y, /)
    Return x**y (x to the power of y).
{'fraction_non_alphanumeric': 0.2079806529625151, 'fraction_numerical': 0.06590084643288996, 'mean_word_length': 5.155348837209302, 'pattern_counts': {'":': 23, '<': 103, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '32', 'dir': 'jupyter-structured-clean-dedup', 'id': '464583', 'license': 'MIT', 'n_tokens_mistral': 3203, 'n_tokens_neox': 2829, 'n_words': 753, 'path': '100_exercises.ipynb', 'repo_name': 'arielfrozza/100_Python3_Problems', 'size': '6616'}