Introduction To Python - Learn Python the hard way v2.0
Title: Exercise 32
SOURCE CODE ALONG WITH EXTRA CREDITS
====================================
prasad@prasad-desktop:~/programs/python/diy/ex-32$ cat ./ex32.py
#!/usr/bin/python
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-loop goes through a list
for number in the_count:
print "This is count %d" % number
# same as above
for fruit in fruits:
print "A fruit of type: %s" % fruit
# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print "I got %r" % i
# we can also build lists, first start with an empty one
elements = []
# then use the range function to do 0 to 5 counts
for i in range(0, 6):
print "Adding %d to the list." % i
# append is a function that lists understand
elements.append(i)
# now we can print them out too
for i in elements:
print "Element was: %d" % i
# instead of looping to copy few elements from list we could use list slicing
# to copy selected elements from list.
# copy on two items from elements list
eles = elements[1:3]
print eles
prasad@prasad-desktop:~/programs/python/diy/ex-32$ chmod +x ex32.py
prasad@prasad-desktop:~/programs/python/diy/ex-32$ ./ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5
[1, 2]
### EXTRA CREDITS 2 ###
range is a builtin function of Python. The function returns a list of integers.
The function declaration of range is
range([start,] stop[, step])
range(4) ==> this will assume start=0, stop=4, and step=1. The list would be
[0, 1, 2, 3, 4]
range(4, 6) ==> start=4, stop=6, and step=1. List ==> [4, 5, 6]
range(1, 5, 3) ==> start=1, stop=5, and step=2. List ==> [1, 3, 5]
prasad@prasad-desktop:~/programs/python/diy/ex-32$ python
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> rang(0, 5)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'rang' is not defined
>>> range(0, 5)
[0, 1, 2, 3, 4]
>>> range(0, 5, 2)
[0, 2, 4]
>>>
>>>
>>>
>>> range(6)
[0, 1, 2, 3, 4, 5]
>>> range(6, 2)
[]
>>> range(2, 6, 2)
[2, 4]
>>> range(2, 6, 3)
[2, 5]
### ETXRA CREDITS 3 ###
1. append: append object to end
2. count: return number of occurrences of value
3. insert: insert object before index
4. pop: remove and return item at index (default last).
5. remove(value): remove first occurrence of value.
6. sort(): sort elements
Declaration of sort is
sort(cmp=None, key=None, reverse=False)
Where,
cmp ==> must be user defined compression function
reverse ==> if True perform descending sort
Here is an example with user defined sort function
>>> def func(x, y):
... return -1
...
>>> a=range(6)
>>> a
[0, 1, 2, 3, 4, 5]
>>> a.sort(comp=func)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'comp' is an invalid keyword argument for this function
>>> a.sort(cmp=func)
>>> a
[5, 4, 3, 2, 1, 0]
>>> a.sort(cmp=func, resverse=True)
Traceback (most recent call last):
File "", line 1, in
TypeError: 'resverse' is an invalid keyword argument for this function
>>> a.sort(cmp=func, reverse=True)
>>> a
[0, 1, 2, 3, 4, 5]
