-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson3.py
142 lines (92 loc) · 3.83 KB
/
lesson3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Data Structures
# 1. Lists
# 1.1 List indexing
month = 8
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
# use list indexing to determine the number of days in month
num_days=days_in_month[month + 1]
print(num_days)
# Slicing lists
eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003',
'March 29, 2006', 'August 1, 2008', 'July 22, 2009',
'July 11, 2010', 'November 13, 2012', 'March 20, 2015',
'March 9, 2016']
# TODO: Modify this line so it prints the last three elements of the list
print(eclipse_dates[-3:])
# Functions for lists
# len, max, min and Lists
a = [1, 5, 8]
b = [2, 6, 9, 10]
c = [100, 200]
print(max([len(a), len(b), len(c)]))
print(min([len(a), len(b), len(c)]))
# sorted, join and Lists
names = ["Carol", "Albert", "Ben", "Donna"]
print(" & ".join(sorted(names)))
# append and Lists
names = ["Carol", "Albert", "Ben", "Donna"]
names.append("Eugenia")
print(sorted(names))
print(" & ".join(sorted(names)))
# Quiz
arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(arr[0])
# Print ['c', 'd', 'e', 'f']
print(arr[2:6])
# Print ['a', 'b', 'c']
print(arr[:3])
# Print ['e', 'f', 'g']
print(arr[4:])
# Dictionaries
# Define a Dictionary, population,
# that provides information
# on the world's largest cities.
# The key is the name of a city
# (a string), and the associated
# value is its population in
# millions of people.
# Key | Value
# Shanghai | 17.8
# Istanbul | 13.3
# Karachi | 13.0
# Mumbai | 12.5
population={'Shanghai':17.8,
'Istanbul':13.3,
'Karachi':13.0,
'Mumbai':12.5}
# Compound data structures
elements = {'hydrogen':
{'number': 1, 'weight': 1.00794, 'symbol': 'H', 'is_noble_gas': False},
'helium':
{'number': 2, 'weight': 4.002602, 'symbol': 'He', 'is_noble_gas': True}}
# todo: Add an 'is_noble_gas' entry to the hydrogen and helium dictionaries
# hint: helium is a noble gas, hydrogen isn't
print(elements['hydrogen']['is_noble_gas'])
print(elements['helium']['is_noble_gas'])
# Practice questions
verse = "if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don't deal in lies or being hated don't give way to hating and yet don't look too good nor talk too wise"
print(verse, '\n')
# split verse into list of words
verse_list = verse.split()
print(verse_list, '\n')
# convert list to a data structure that stores unique elements
verse_set = set(verse_list)
print(verse_set, '\n')
# print the number of unique words
num_unique = len(verse_set)
print(num_unique, '\n')
verse_dict = {'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, 'don\'t': 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1}
print(verse_dict, '\n')
# find number of unique keys in the dictionary
num_keys = len(verse_dict)
print(num_keys)
# find whether 'breathe' is a key in the dictionary
contains_breathe = 'breathe' in verse_dict
print(contains_breathe)
# create and sort a list of the dictionary's keys
sorted_keys = sorted(verse_dict.keys())
print(sorted_keys)
# get the first element in the sorted list of keys
print(sorted_keys[0])
# find the element with the highest value in the list of keys
print(sorted_keys[-1])