tuples, lists, dicts, and sets
()
brackets or 'tuple'tup = ((1, 2, 3, 4, 5, 6), (7, 8, 9))
tup
((1, 2, 3, 4, 5, 6), (7, 8, 9))
my_list = [2, 4, 6]
type(my_list)
list
tup2 = tuple(my_list)
tup2
(2, 4, 6)
my_string = "Hello, world!"
type(my_string)
str
tup3 = tuple(my_string)
tup3
('H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!')
tup3[0:5]
('H', 'e', 'l', 'l', 'o')
# tuples are immutable
tup3[0] = "h"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-aa3a9878c7a9> in <module>() 1 # tuples are immutable ----> 2 tup3[0] = "h" TypeError: 'tuple' object does not support item assignment
tup4 = tuple(["foo", (2, 4, 6), range(5)])
tup4
('foo', (2, 4, 6), range(0, 5))
tup4[1].append(3)
tup4
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-19-8ad460f439e0> in <module>() ----> 1 tup4[1].append(3) 2 tup4 AttributeError: 'tuple' object has no attribute 'append'
tup4.count("foo")
1
seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
seq
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
for a, b, c in seq:
print("a = {0}, b = {1}, c = {2}".format(a, b, c))
a = 1, b = 2, c = 3 a = 4, b = 5, c = 6 a = 7, b = 8, c = 9
values = ((a, b, c), (range(20)))
values
((7, 8, 9), range(0, 20))
y, *rest = values
y
(7, 8, 9)
rest
[range(0, 20)]
[]
bracketsmy_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
type(my_list)
list
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
new_list = [10, 11, 12, 14, 15]
my_list.append(new_list)
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, [10, 11, 12, 14, 15]]
my_list + new_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, [10, 11, 12, 14, 15], 10, 11, 12, 14, 15]
my_list.pop(9)
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
my_list.remove(8)
my_list
[1, 2, 3, 4, 5, 6, 7, 9]
new_list + my_list
[10, 11, 12, 14, 15, 1, 2, 3, 4, 5, 6, 7, 9]
a = [7, 2, 5, 1, 3]
a.sort()
a
[1, 2, 3, 5, 7]
type(sort_list)
NoneType
sort_list is None
True
{}
and colonsmy_info = {"name" : "daniel",
"age" : 30,
"degree" : "phd",
"skills" : ["R", "Python", "C++"]}
my_info
{'age': 30, 'degree': 'phd', 'name': 'daniel', 'skills': ['R', 'Python', 'C++']}
my_info["skills"]
['R', 'Python', 'C++']
"name" in my_info
True
my_info["colleges"] = ["UDSM", "TSU", "UNLV"]
my_info
{'age': 30, 'colleges': ['UDSM', 'TSU', 'UNLV'], 'degree': 'phd', 'name': 'daniel', 'skills': ['R', 'Python', 'C++']}
set1 = {1, 2, 3, 4}
set2 = {5, 6, 7, 8, 9, 4, 4}
set1.union(set2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
set1 | set2
{1, 2, 3, 4, 5, 6, 7, 8, 9}
set1.intersection(set2)
{4}
set1 & set2
{4}
def my_func():
a = 4
b = 6
c = 8
return{"a" : a, "b" : b, "c" : c}
my_func()
{'a': 4, 'b': 6, 'c': 8}
states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda', 'south carolina##', 'West virginia?']
import re
def clean_strings(strings):
result = []
for value in strings:
value = value.strip()
value = re.sub('[!#?]', '', value)
value = value.title()
result.append(value)
return result
clean_strings(states)
['Alabama']
A way of writing functions consisting of a single statement, the result of which is a return value
anon_func = lambda x: x * 2
anon_func(2)
4
path = "examples/segismundo.txt"
f = open(path)
lines = [x.rstrip() for x in f]
lines
['Sueña el rico en su riqueza,', 'que más cuidados le ofrece;', '', 'sueña el pobre que padece', 'su miseria y su pobreza;', '', 'sueña el que a medrar empieza,', 'sueña el que afana y pretende,', 'sueña el que agravia y ofende,', '', 'y en el mundo, en conclusión,', 'todos sueñan lo que son,', 'aunque ninguno lo entiende.', '']
f.close()
with open(path) as f:
lines = [x.rstrip() for x in f]
lines
['Sueña el rico en su riqueza,', 'que más cuidados le ofrece;', '', 'sueña el pobre que padece', 'su miseria y su pobreza;', '', 'sueña el que a medrar empieza,', 'sueña el que afana y pretende,', 'sueña el que agravia y ofende,', '', 'y en el mundo, en conclusión,', 'todos sueñan lo que son,', 'aunque ninguno lo entiende.', '']
with open(path) as f:
lines = f.readlines()
lines
['Sueña el rico en su riqueza,\n', 'que más cuidados le ofrece;\n', '\n', 'sueña el pobre que padece\n', 'su miseria y su pobreza;\n', '\n', 'sueña el que a medrar empieza,\n', 'sueña el que afana y pretende,\n', 'sueña el que agravia y ofende,\n', '\n', 'y en el mundo, en conclusión,\n', 'todos sueñan lo que son,\n', 'aunque ninguno lo entiende.\n', '\n']