= ["Inception", "The Matrix", "Interstellar", "The Shawshank Redemption", "The Godfather"]
movies for movie in movies:
print(movie)
Inception
The Matrix
Interstellar
The Shawshank Redemption
The Godfather
Task: Create a list of 5 of your favorite movies and write a loop to print each movie.
Instructions:
movies
with 5 movie titles.for
loop that iterates over the list and prints each movie.Task: Create a list of numbers and write a loop to calculate and print the sum of all numbers in the list.
Instructions:
numbers
with at least 5 integer values.Task: Create a list of words and write a loop to count how many characters (i.e., letters) the list contains in total. Print the count.
Instructions:
words
with several words.len()
. Sum the count.Task: Create a loop that doubles each number in a list and prints the new list.
Instructions:
numbers
with some integer values.doubled_numbers
where each element is double the corresponding element in numbers
.Task: Write a function that counts the total number of vowels in a list of words.
Instructions:
count_vowels(word_list)
that takes a list of strings word_list
.["NLP", "is", "fun"]
) and print the result.def count_vowels(word_list):
vowels = 'aeiouAEIOU'
total_vowels = 0
for word in word_list:
for char in word:
if char in vowels:
total_vowels += 1
return total_vowels
# Sample usage
words = ["NLP", "is", "fun"]
result = count_vowels(words)
print("Total number of vowels:", result)
Total number of vowels: 2
Task: Create a function that filters out words longer than a specified length from a list.
Instructions:
filter_long_words(word_list, length)
that takes a list of strings word_list
and an integer length
.length
.["Natural", "Language", "Processing", "is", "fun"]
, length=4) and print the result.def filter_long_words(word_list, length):
short_words = []
for word in word_list:
if len(word) <= length:
short_words.append(word)
return short_words
# Sample usage
words = ["Natural", "Language", "Processing", "is", "fun"]
result = filter_long_words(words, 4)
print("Filtered words:", result)
Filtered words: ['is', 'fun']
Task: Write a function that creates a frequency dictionary from a list of words.
Instructions:
word_frequency(word_list)
that takes a list of strings word_list
.["NLP", "is", "fun", "NLP"]
) and print the result.def word_frequency(word_list):
frequency_dict = {}
for word in word_list:
if word in frequency_dict:
frequency_dict[word] += 1
else:
frequency_dict[word] = 1
return frequency_dict
# Sample usage
words = ["NLP", "is", "fun", "NLP"]
result = word_frequency(words)
print("Word frequency:", result)
Word frequency: {'NLP': 2, 'is': 1, 'fun': 1}
Task: Create a function that reverses each word in a list.
Instructions:
reverse_words(word_list)
that takes a list of strings word_list
.["NLP", "is", "fun"]
) and print the result.