Welcome to Lecture 16:
Lists + Mutability + List Comprehension
Today’s Topics
Announcements
Review
Review
Lists
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Lists in Python
Guess that Output!
Guess that Output!
A nested list!
Guess that Output!
Guess that Output!
Throws an error! We cannot index an integer. But, we can stringify an integer
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
Guess that Output!
So when we say word_list[i][:3], we are calling the first three characters in each string
If i == 1, then word_list[i] is “Victoria’s” and word_list[i][:3] is “Vic”
Write the function:
Write a Python function sum_of_squares that takes a list of numbers as input and returns the sum of the squares of each number in the list. For example, if the input list is [1, 2, 3, 4], then:
sum_of_squares([1, 2, 3, 4]) and it should return 30:
1*1 + 2*2 + 3*3 + 4*4 = 30.
Write the function:
List Comprehension
List Comprehension with Strings
List Comprehension
Function
Variable
List
List Comprehension
List Comprehension
Function
Variable
List
Condition
List Comprehension
List Comprehension
Function 1
Variable
List
Condition
Function 2
What about ‘Combine’
Write the function:
Write a Python function filter_and_double_odd_numbers that takes a list of numbers as input and returns a new list containing only the odd numbers from the input list, each multiplied by 2. For example, if the input list is [1, 2, 3, 4, 5], the function should return [2, 6, 10].
Write the function:
Write the function:
Write the function:
Write a Python function capitalize_long_words that takes a list of strings as input and returns a new list where only the strings with more than 3 characters are capitalized. You MUST use list comprehension
For example, if the input list is ["apple", "cat", "banana", "dog"], the function should return:
["APPLE", "cat", "BANANA", "dog"].
To capitalize words, you can use the “upper” function. Example:
print(“string”.upper())
>>> “STRING”
Write the function:
Write the function: