-
Print every value, on a separate line: this is the simplest
recipe. Example:
for value in my_list:
print value
-
Print all the values on the same line:
for value in my_list:
print value,
print # this starts a new line, when we're finished
-
Print 10 values per line:
column = 1
for value in my_list:
print value,
if column == 10:
print # starts a new line
column = 0
column += 1
if column < 10: # if last line had less than 10 values,
print # finish it off
-
Print 10 values per line, using remainders to detect multiples of 10:
column = 1
for value in my_list:
print value,
if column % 10 == 0:
print # starts a new line
column += 1
if column % 10 != 0: # if last line had less than 10 values,
print # finish it off
-
Print 10 values per line, using list indexes and remainders:
for index in range(len(my_list)):
print value,
if index % 10 == 0:
print # starts a new line
if len(my_list) % 10 != 0:
print
-
Accumulate values in a list. Begin with an empty accumulator, and
update it for each value. For example, add them all up:
total = 0
for value in my_list:
total += value
By the way, total = sum(my_list) does the same thing for you!
-
Average of values in a list. Get their sum, and divide by the
number of values:
total = 0
for value in my_list:
total += value
average = total / len(my_list)
-
Find the biggest value: you will need to keep track of the biggest
value so far. Initially, make it the first value. Then the loop
should begin at the second value (you already saw the first
one):
biggest = my_list[0]
for value in my_list[1:] :
if value > biggest:
biggest = value
By the way, biggest = max(my_list) does the same thing for you!
-
Find the smallest value: very similar to the above recipe:
smallest = my_list[0]
for value in my_list[1:] :
if value < smallest:
smallest = value
By the way, smallest = min(my_list) does the same
thing for you!
-
Does every value in the list have some property? Begin by
assuming they all do, and try to find values that don't
have the property. If you find one, stop looking. For example,
are all values even?
all_even = True:
for value in my_list:
if value % 2 != 0:
all_even = False
break
-
Does some value in the list have some property? Begin by
assuming not, and try to find values that do
have the property. If you find one, stop looking. For example,
does the list have a string?
has_string = False:
for value in mylist:
if type(value) == type(''):
has_string = True
break
-
What is the index of the value that has some property? Like the
previous case, but now we will use indexes. For example: where
does an odd number occur?:
has_odd = False
for index in range(len(my_list)):
value = my_list[index]
if value % 2 == 1:
has_odd = True
odd_index = index
break
Notice that this exits upon finding the first odd value. There
may be other odd values in the list.
-
Let's combine some of the above. Here is the sum of all the odd
values:
has_odd = False
odd_total = 0
for value in my_list:
if value % 2 == 1:
has_odd = True
odd_total += value
-
How many values have some property? For example, how many odd
values?
odd_count = 0
for value in my_list:
if value % 2 == 1:
odd_count += 1
-
Another combination: Find the biggest odd value. We can't begin
with
biggest_odd = my_list[0], because the first
value may not be odd. In fact, there may not be any odd values!
So, we will initialize biggest_odd the first
time we find an odd value. Hence we need a flag to know if it's
the first time:
has_odd = False
first = True
for value in my_list:
if value % 2 == 1:
if first:
biggest_odd = value
first = False
elif value > biggest_odd:
biggest_odd = value
if has_odd:
print 'Biggest odd value:', biggest_odd
else:
print 'No odd values'
-
Is the list sorted? Compare adjacent values, to see if they're in
order. Since we're comparing each value to the one before
it, we can't compare the first one to the one before it.
So we start at the second value:
is_sorted = True
for index in range(1, len(my_list)): # index starts at second entry
if my_list[index] < my_list[index - 1]:
is_sorted = False
break
-
Is the list sorted? We can do this another way: remember the
previous value in the loop. At the end of the loop,
set
previous_value = value, to prepare for the next
time through the loop:
is_sorted = True
previous_value = my_list[0]
for value in my_list[1:] :
if value < previous_value:
is_sorted = False
break
previous_value = value
-
Are all the values the same?
all_same = True
first_value = my_list[0]
for value in my_list[1:] :
if value != first_value
all_same = False
break
-
Does the list read the same forwards and backwards (why would
you ever need this outside of a homework exercise)?
is_palindromic = True
for index in range(len(my_list)):
if my_list[index] != my_list[-(index + 1)]:
is_palindromic = False
break