-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_codes_module_3.py
More file actions
191 lines (156 loc) · 4.95 KB
/
class_codes_module_3.py
File metadata and controls
191 lines (156 loc) · 4.95 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
This are class examples ADVANCED DATA TYPES
@author: tiago
@date: Jan 24
"""
# STRINGS
# Add strings
my_string = "This sentence" + "is broken into" + "three parts!"
my_other_string = "My grammer is terrible."
my_final_string = my_string + my_other_string
# Converting
my_string = str(10)
my_string = str(True)
my_string = str([1, 2, 3, 4])
# Useful methods to format strings
my_string = " Python, is a great language. "
my_string.lower() # -> "python, is a great language."
my_string.upper() # -> "PYTHON, IS A GREAT LANGUAGE."
my_string.lstrip() # -> "Python, is a great language. "
my_string.rstrip() # -> " Python, is a great language."
my_string.strip() # -> "Python, is a great language."
my_string = "Violet is a colour in the rainbow."
my_string.find("colour") # -> 12
"rainbow" in my_string # -> True
"blue" not in my_string # -> True
my_string.startswith("Vio") # -> True
my_string.endswith(".") # -> True
my_string = "Python is an object oriented, interpreted language."
my_string.split(",") # -> ['Python is an object oriented', 'interpreted language.']
list(my_string) # -> ['P', 'y', 't', 'h', 'o', 'n', ' ', ...]
my_string.replace(",", "~") # -> "Python is an object oriented~ interpreted language."
my_string[7:] # -> "is an object oriented, interpreted language."
my_string[:6] # -> "Python"
my_string[-9:] # -> "language."
# LISTS
# Accessing
mylist = ['apples', 'oranges', 'pears']
mylist [1] # -> 'oranges'
# Changing
mylist [1] = 'bananas' # -> -> ['apples', 'bananas', 'pears']
# Adding items
mylist = [1, 2, 3]
mylist.append(4) # -> [1, 2, 3, 4]
mylist + [4, 5, 6] # -> [1, 2, 3, 4, 5, 6]
mylist.insert(0, 9) # -> [9, 1, 2, 3]
# Removing items
mylist = ['red', 'black', 'yellow']
# mylist.remove('black') # -> ['red', 'yellow]
del mylist[:1] # -> ['black', 'yellow']
mylist.clear() # -> []
# Slices
mylist = [1, 2, 3, 4, 5]
# mylist[start:stop:step]
mylist[1:3] # -> [2, 3]
mylist[:2] # -> [1, 2]
mylist[-1:] # -> [5]
mylist[::2] # -> [1, 3, 5]
# Membership
mylist = ['apple', 'orange', 'banana']
'apple' in mylist # -> True
"pinneapple" not in mylist # -> True
mylist.index('orange') # -> 1
# Lenght
len(mylist)
# Sorting
myslist = [5, 1, 2, 4, 3]
mylist.sort() # -> [1, 2, 3, 4, 5]
sorted(mylist) # -> [1, 2, 3, 4, 5]
# TUPLES
mytuple = (1, 2, 3, 4, 5)
mytuple[2] # -> 3
# To create a tuple
mytuple = (99,)
# Adding or removing
my_1st_tuple = (1, 2, 3, 4, 5)
my_2nd_tuple = (6, 7, 8, 9, 10)
my_3rd_tuple = my_1st_tuple + my_2nd_tuple # -> (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Another way to create
mytuple = (1, 2, 3, 4, 5)
mytuple = mytuple + (999, )
mytuple # -> (1, 2, 3, 4, 5, 999)
# Convert a list to a tuple
mylist = [1, 2, 3, 4, 5]
mytuple = tuple(mylist)
mytuple = (1, 2, 3, 4, 5)
# Slices
mytuple = (1, 2, 3, 4, 5)
# mytuple[start:stop:step]
mytuple[1:3] # -> (2, 3)
mytuple[:2] # -> (1, 2)
mytuple[1:] # -> (5)
mytuple[::2] # -> (1, 3, 5)
# Check if an item is in the tuple
mytuple = ('apple', 'orange', 'banana')
'apple' in mytuple # -> True
"pineapple" not in mytuple # -> True
# Find the index
mytuple.index('orange') # -> 1
# DICTIONARIES
my_fruit_dictionary = { 'apples': 52, 'oranges': 105, 'bananas': 9}
my_num_dictionary = {1: 2, 2: 3, 3: 4}
my_fancy_dictionary = {'fruit': ['apples', 'oranges', 'bananas'],
'bread': ("rye", "marble")
}
my_empty_dictionary = {}
# How to write a dictionary
mydictionary = {'Gru': 1001,
'Agnes': 3389,
'Kevin': 1276,
'Nefario': 1000}
# Accessing
mydictionary = {'Gru': 1001, 'Agnes': 3389, 'Kevin': 1276, 'Nefario': 1900}
mydictionary['Agnes'] # -> 3389
# Adding
mydictionary = {'Gru': 1001, 'Agnes': 3389, 'Kevin': 1276, 'Nefario': 1900}
mydictionary['Margo'] = 5647
# mydictionary
# -> {'Gru':
# 'Agnes': 3389,
# 'Kevin': 1276,
# 'Nefario': 1900,
# 'Margo': 5647}
# Initialize
mydictionary = {}
# Modifying
mydictionary = {'Gru': 1001, 'Agnes': 3389,' Kevin': 1276, 'Nefario': 1900}
mydictionary[' Kevin'] = 9999
# mydictionary
# -> {'Gru':
# 'Agnes': 3389,
# 'Kevin': 9999,
# 'Nefario': 1900}
# Sorting
mydictionary = {'Gru': 1001, 'Agnes': 3389, 'Kevin': 1276, 'Nefario': 1900}
sorted(mydictionary) # -> ['Agnes', 'Gru', 'Kevin', 'Nefario']
sorted(mydictionary.items())
# -> [('Agnes': 3389), ('Gru': 1001), ('Kevin': 1276), ('Nefario':1900)]
# Delete
mydictionary = {'Gru': 1001, 'Agnes': 3389, 'Kevin': 1276, 'Nefario': 1900}
del mydictionary['Agnes']
# mydictionary = {'Gru': 1001, 'Kevin': 1276, 'Nefario': 1900}
# SETS
myset1 = {1, 2, 3, 4, 5}
myset2 = {4, 5, 6, 7, 8, 9, 10}
# Intersection
myset1.intersection(myset2) # -> {4,5}
# Difference
myset1.difference(myset2) # -> {1, 2, 3}
myset2.difference(myset1) # -> {6, 7, 8, 9, 10}
# Union
myset1.union(myset2) # -> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
# Convert to a set
mygroceries = ['apples', 'oranges', 'bananas', 'apples', 'bananas']
set(mygroceries) # -> {'apples', 'oranges', 'bananas'}
# Then convert it back to a list
list(set(mygroceries)) # -> ['apples', 'oranges', 'bananas']