Skip to content

Commit 4d77cc4

Browse files
committed
Sample code to understand lists in python
1 parent 2ceb1d6 commit 4d77cc4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

samplelists.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# We now will build some lists using some loops and print them out:
2+
the_count = [1, 2, 3, 4, 5]
3+
fruits = ['apples', 'oranges', 'pears', 'apricots']
4+
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
5+
6+
# this first kind of for-loop goes through a list
7+
for number in the_count:
8+
print "This is count %d" % number
9+
10+
# same as above
11+
for fruit in fruits:
12+
print "A fruit of type: %s" % fruit
13+
14+
# also we can go through mixed lists too
15+
# notice we have to use %r since we don't know what's in it
16+
for i in change:
17+
print "I got %r" % i
18+
19+
# we can also build lists, first start with an empty one
20+
elements = []
21+
22+
# then use the range function to do 0 to 5 counts
23+
for i in range(0, 6):
24+
print "Adding %d to the list." % i
25+
26+
# append is a function that lists understand
27+
elements.append(i)
28+
29+
# now we can print them out too
30+
for i in elements:
31+
print "Element was: %d" % i

0 commit comments

Comments
 (0)