1- You’ve been given a list of the numbers between 0 and 10. We created this list using the range
function! Create a new list named squares
that contains the square of every number in this list.
nums = range(11)
print(nums)
squares = [x**2 for x in nums]
print(squares)
2- Create a new list named
add_ten
that adds ten to every element in the list nums
.nums = [4, 8, 15, 16, 23, 42]
add_ten = [x+10 for x in nums]
print(add_ten)
3- Create a new list named
divide_by_two
that contains half of every element in the list nums
. Make sure to divide by 2
, not 2.0
!nums = [4, 8, 15, 16, 23, 42]
divide_by_two = [x/2 for x in nums]
print(divide_by_two)
4- Create a new list named
parity
that contains a 1
or a 0
for each element of nums
. For each element, if that element was even, the new list should contain a 0
. If the element was odd, the new list should contain a 1
.nums = [4, 8, 5, 16, 23, 42]
parity = [num % 2 for num in nums]
print(parity)
5- Create a new list named
greetings
that adds "Hello, "
in front of each name in the list names
.names = ["Elaine", "George", "Jerry", "Cosmo"]
greetings = ["Hello, "+x for x in names]
print(greetings)
6- Create a new list named
first_character
that contains the first character from every name in the list names
names = ["Elaine", "George", "Jerry", "Cosmo"]first_character = [x[0] for x in names]print 1- (first_character)
lengths
that contains the size of each name in the list of namesnames = ["Elaine", "George", "Jerry", "Cosmo"]
lengths = [len(x) for x in names]
print(lengths)
8- Create a new list named
opposite
that contains the opposite boolean for each element in the list booleans
.ooleans = [True, False, True]
opposite = [not x for x in booleans]
print(opposite)
9-Create a new list called
is_Jerry
, in which an entry at position i
is True
if the entry in names
at position i
equals "Jerry"
. The entry should be False
otherwisenames = ["Elaine", "George", "Jerry", "Cosmo"]
is_Jerry = [name == "Jerry" for name in names]
print(is_Jerry)
10-Create a new list called
greater_than_two
, in which an entry at position i
is True
if the entry in nums
at position i
is greater than 2
.nums = [5, -10, 40, 20, 0]
greater_than_two = [x>2 for x in nums]
print(greater_than_two)
11- Create a new list named
product
that contains the product of each sub-list of nested_lists
.nested_lists = [[4, 8], [15, 16], [23, 42]]
product = [x1*x2 for (x1,x2)in nested_lists]
print(product)
12-Create a new list named
greater_than
that contains True
if the first number in the sub-list is greater than the second number in the sub-list, and False
otherwise.nested_lists = [[4, 8], [16, 15], [23, 42]]
greater_than = [x[0]>x[1] for x in nested_lists]
print(greater_than)
13-Create a new list named
first_only
that contains the first element in each sub-list of nested_lists
.nested_lists = [[4, 8], [16, 15], [23, 42]]
first_only = [x[0] for x in nested_lists]
print(first_only)
14- Use list comprehension and the zip function to create a new list named
sums
that sums corresponding items in lists a
and b
. For example, the first item in the new list should be 5
from adding 1
and 4
together.a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]
sums = [item1 + item2 for (item1, item2)
in zip(a, b)]
#happen+ to whom+ where
15- Use list comprehension and the zip function to create a new list named
quotients
that divides the elements in list b
by those in list a
. For example, the second item in the new list should be 2.5
from dividing 5.0
by 2.0
.a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]
quotients = [element_b/element_a for (element_b,element_a) in zip(b,a)]
print(quotients)
16- You’ve been given two lists: a list of capitals and a list of countries. Create a new list named
locations
that contains the string "capital, country"
for each item in the original lists. For example, if the 5th item in the capitals list is "Lima"
and the 5th item in the countries list is "Peru"
, then the 5th item in the new list should be "Lima, Peru"
capitals = ["Santiago", "Paris", "Copenhagen"]
countries = ["Chile", "France", "Denmark"]
locations = [x+", "+y for (x,y) in zip(capitals,countries)]
print(locations)
17- You’ve been given two lists: a list of names and a list of ages. Create a new list named
users
that contains the string "Name: name, Age: age"
for each pair of elements in the original lists. For example, if the 5th item in the names
list is "Aiyana"
and the 5th item in ages
is 42
, then the 5th item in the new list should be "Name: Aiyana, Age: 42"
.As you did in the previous exercise, concatenate your strings together using +
. Make sure to add proper capitalization and spaces.
names = ["Shilah", "Arya", "Kele"]
ages = [14, 9, 35]
users = ["Name: " + name + ", Age: " + str(age) for (name, age) in zip(names, ages) ]
print(users)
18- Create a new list named
greater_than
that contains True
or False
depending on whether the corresponding item in list a
is greater than the one in list b
. For example, if the 2nd item in list a
is 3
, and the 2nd item in list b
is 5
, the 2nd item in the new list should be False
.a = [30, 42, 10]
b = [15, 16, 17]
greater_than = [x>y for (x,y) in zip(a,b)]
print(greater_than)
19-
0 Comments:
Post a Comment