cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
# Goal: to make every element title case
print("Original element:", cities[0])
print("Title case:", cities[0].title())
# Goal: to make every element title case
print("Original element:", cities[1])
print("Title case:", cities[1].title())
# To modify every element in the list one by one, use for loop
for city in cities:
print(city)
print(city.title())
capitalized_cities = []
for city in cities:
capitalized_cities.append(city.title())
print(capitalized_cities)
cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
for index in range(len(cities)):
cities[index] = cities[index].title()
print(cities)
print("The length of the list:", len(cities))
print(list(range(len(cities))))
for index in range(len(cities)):
print(index)
list(range(7))
# iterator object
range(4)
list(range(1, 4))
list(range(7, 10))
list(range(10, 20, 2))
# accessing element in a set one by one with a for loop
for element in {1, 2, 3}:
print(element)
cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
for city in cities:
print(city)
city = city.title()
print(city)
#print(cities)
my_dict = {"a": 0, "b":1, "c":2}
for key in my_dict:
print(key)
If you assign your result back to the iteration variable, your result will be discarded at the next iteration!!
In the previous example, the iteration variable is city:
- In the first iteration, city has the value 'kuala lumpur'
- After you assign the result back to city, it has the value 'Kuala Lumpur'
- At the next iteration, city automatically replaces its value with the next element, which is 'jakarta'
my_dict = {"a": 0, "b":1, "c":2}
for key in my_dict:
print("Key:", key)
print("Value: ",my_dict[key])
my_dict = {"a": 0, "b":1, "c":2}
for (key, value) in my_dict.items():
print("Key:", key)
print("Value: ",value)
# Exercise 1
# Use a for loop to take a list and print each element of the list in its own line.
sentence = ["the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]
# TODO: Write a for loop to print out each word in the sentence list, one word per line
for word in sentence:
print (word)
# Exercise 2
# Write a for loop that will print out every whole number
# that is a multiple of 5 and less than or equal to 30.
# [5, 10, 15, 20, 25, 30]
# TODO: Write a for loop using range() to print out multiples of 5 up to 30 inclusive
for number in range(5, 31, 5):
print (number)
%run -i students.py
# Write a for loop that iterates over the names list to create a usernames list.
# To create a username for each name, make everything lowercase and replace spaces with underscores.
names = ["Antonio Ruiz", "Joey Joey", "Leslie Leslie", "Radita Liem"]
# TODO: create a new list called 'usernames'
# print(usernames) should return
# ["antonio_ruiz", "joey_joey", "leslie_leslie", "radita_liem"]
result = []
# Use a for loop to iterate over every element and convert it to username
for usernames in names:
result.append(usernames.lower().replace(" ", "_"))
print(result)
print(result)
lowercase_name = "Kevin Siswandi".lower()
lowercase_name.replace(" ", "_")