cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
# Goal: to make every element title case
print("Original element:", cities[0])
print("Title case:", cities[0].title())
Original element: kuala lumpur
Title case: Kuala Lumpur
# Goal: to make every element title case
print("Original element:", cities[1])
print("Title case:", cities[1].title())
Original element: jakarta
Title case: Jakarta
# To modify every element in the list one by one, use for loop
for city in cities:
print(city)
print(city.title())
kuala lumpur
Kuala Lumpur
jakarta
Jakarta
singapore
Singapore
sydney
Sydney
capitalized_cities = []
for city in cities:
capitalized_cities.append(city.title())
print(capitalized_cities)
['Kuala Lumpur', 'Jakarta', 'Singapore', 'Sydney']
cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
for index in range(len(cities)):
cities[index] = cities[index].title()
print(cities)
['Kuala Lumpur', 'Jakarta', 'Singapore', 'Sydney']
print("The length of the list:", len(cities))
print(list(range(len(cities))))
for index in range(len(cities)):
print(index)
The length of the list: 4
[0, 1, 2, 3]
0
1
2
3
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)
1
2
3
cities = ['kuala lumpur', 'jakarta', 'singapore', 'sydney']
for city in cities:
print(city)
city = city.title()
print(city)
#print(cities)
kuala lumpur
Kuala Lumpur
jakarta
Jakarta
singapore
Singapore
sydney
Sydney
my_dict = {"a": 0, "b":1, "c":2}
for key in my_dict:
print(key)
a
b
c
my_dict = {"a": 0, "b":1, "c":2}
for key in my_dict:
print("Key:", key)
print("Value: ",my_dict[key])
Key: a
Value: 0
Key: b
Value: 1
Key: c
Value: 2
my_dict = {"a": 0, "b":1, "c":2}
for (key, value) in my_dict.items():
print("Key:", key)
print("Value: ",value)
Key: a
Value: 0
Key: b
Value: 1
Key: c
Value: 2
# 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)
the
quick
brown
fox
jumped
over
the
lazy
dog
5
10
15
20
25
30
%run -i students.py
Unsupported output type: clearOutput
The chosen one is: Caryn
# 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)
['antonio_ruiz']
['antonio_ruiz', 'joey_joey']
['antonio_ruiz', 'joey_joey', 'leslie_leslie']
['antonio_ruiz', 'joey_joey', 'leslie_leslie', 'radita_liem']
['antonio_ruiz', 'joey_joey', 'leslie_leslie', 'radita_liem']
lowercase_name = "Kevin Siswandi".lower()
lowercase_name.replace(" ", "_")