-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path1.py
More file actions
28 lines (22 loc) · 702 Bytes
/
1.py
File metadata and controls
28 lines (22 loc) · 702 Bytes
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
i = [4, 2, 3, 5, 6]
j = [5, 2, 1, 4, 7]
"""
Using for loops to perform operations. This is the usual stuff. Multiplies
every number by every other number and prints a result out.
"""
for c1 in range(0, len(i)):
for c2 in range(0, len(j)):
print i[c1] * j[c2]
"""
Doing the same multiplication above but with list comprehensions instead.
"""
print "\nMultiplication using list comprehensions\n"
result = [x1 * y1 for x1 in i for y1 in j]
print result
"""
Using dictionary comprehensions to do the same thing as above but with a dictionary instead.
"""
print "Creating a new dictionary\n"
d1= {'a':1, 'b':2, 'c':3}
d2= {key: key+str(value) for key, value in d1.items()}
print d2.keys()