lfalanga
12/9/2019 - 12:16 AM

## For loop in Python

``````# Example 1: Simmple For loop
names = ["Adam","Alex","Mariah","Martine","Columbus"]

for x in names:
print x

# Example 2: Using For loop on dictionaries
webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}

# Add your code below!
for key in webster:
print webster[key]

# Example 3: Looping lists using conditionals
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for x in a:
if x % 2 == 0:
print x

# Example 4: Using For loop inside a function
def fizz_count(x):
count = 0
for s in x:
if s == 'fizz':
count += 1
return count

print fizz_count(["fizz","cat","fizz"])

# Example 5: Looping trough strings
for letter in "Codecademy":
print letter

# Empty lines to make the output pretty
print
print

word = "Programming is fun!"
for letter in word:
# Only print out the letter i
if letter == "i":
print letter

# Example 6: Loop trough two dictionaries with same keys
prices = {
"banana" : 4,
"apple"  : 2,
"orange" : 1.5,
"pear"   : 3,
}
stock = {
"banana" : 6,
"apple"  : 0,
"orange" : 32,
"pear"   : 15,
}

total = 0
for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total += prices[key] * stock[key]

print
print total

# Example 7: Shopping list example
shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

# Write your code below!
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total

print compute_bill(shopping_list)

# Example 8: Looping using range function
"""
range(stop)
range(start, stop)
range(start, stop, step)
NOTE: If omitted, start defaults to 0 and step defaults to 1.

range(6) # => [0, 1, 2, 3, 4, 5]
range(1, 6) # => [1, 2, 3, 4, 5]
range(1, 6, 3) # => [1, 4]
"""
n = [3, 5, 7]

def print_list(x):
for i in range(0, len(x)):
print x[i]

print_list(n)

# Example 9: Comparing for looping methods

# Method 1 - for item in list:
for item in list:
print item

# Method 2 - iterate through indexes:
for i in range(len(list)):
print list[i]

"""
NOTE:
Method 1 is useful to loop through the list, but it’s not possible to modify the list this way.
Method 2 uses indexes to loop through the list, making it possible to also modify the list if needed.
"""

# Example 10: Enumerate three of your favourite hobbies
hobbies = []

# Add your code below!
for i in range(3):
hobby = raw_input("Your hobby: ")
hobbies.append(hobby)

print ", ".join(hobbies)

# Example 11: Looping trough a dictionary
d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
# Your code here!
print d[key], key

# Example 12: Using For loop while enumerating index using 'enumerate' function
choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
print index + 1, item

# Example 13: Looping trough many lists using 'zip'
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

"""
NOTE:
zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.
zip can handle three or more lists as well!
"""
for a, b in zip(list_a, list_b):
# Add your code here!
print max(a, b)

# Example 14: For / Else
for objetivo in objeto:
código1
else:
código2``````