#### You've reached the end of your free preview.

Want to read the whole page?

**Unformatted text preview: **Num
Description
For loop
Equivalent list
comprehension
Output of both programs
my_list = [5, 20, 50]
for i in
my_list = [5, 20, 50]
7
Add 10 to every element.
range (len (my_list) ) :
my_list = [(i+10) for i
my_list [i] += 10
in my_list]
[15, 30, 60]
print (my_list)
print (my_list)
my_list = [5, 20, 50]
for i in
my_list = [5, 20, 50]
2
Convert every element to a string.
range (len (my_list) ) :
my_list = [str(i) for i
my_list[i ] =
in my_list ]
[ '5' , '20', '50']
str(my_list[i])
print (my_list)
print (my_list)
inp = input ( 'Enter
numbers : ' )
inp = input ( 'Enter
3
Convert user input into a list of
my_list = integers.
for i in inp. split() :
numbers : ' )
my_list = [int(i) for i
Enter numbers : 7 9 3
my_list . append ( int (i) )
in inp. split( ) ]
print (my_list)
print (my_list)
[7, 9, 3]
my_list = [[5, 10, 15],
[2, 3, 16], [100]]
sum_list = my_list = [[5, 10, 15],
4
Find the sum of each row in a two-
[2, 3, 16], [100]]
dimensional list.
for row in my list:
sum_list = [sum (row)
for row in my_list ]
[30, 21, 100]
sum_list . append ( sum (row) )
print (sum_list)
print (sum_list
my_list = [[5, 10, 15],
Find the sum of the row with the
[2, 3, 16], [100]]
sum list = my_list = [[5, 10, 15],
[2, 3, 16], [100]]
5
smallest sum in a two-dimensional
for row in my_list:
min_row = min ( [sum(row)
21
table.
sum_list . append ( sum (row) )
for row in my_list])
min_row = min (sum_list)
print (min_row)
print (min_row)...

View
Full Document

- Spring '16