Chapter 4: working with lists Flashcards

1
Q

what does a for loop do?

A

a for loop pulls each individual value and associates it with the singular version of the list variable and then prints. this is repeated for each value in the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

writ a simple for loop for a list

A

magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(magician)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

attach an f-string to a for loop

A

for magician in magicians:

print(f”{magician.title()}, that was a great trick!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write a for loop with multiple f-strings and then write code after the for loop to summarize the for loop

A

for magician in magicians:
print(f”\n{magician.title()}, that was a great trick!”)
print(f”I can’t wait to see your next trick,
{magician.title()}!”)
print(“\nThank you, everyone. That was a great magic show!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

use range in a for loop to generate a series of numbers

think of what value the range will stop at before you print

A

for value in range(1, 5):

print(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what number does the for loop stop at when using range?

A

the one before the number listed, if you go 1 to 5 it will stop at 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

make a list in a variable from the range function

A

numbers = list(range(1, 6))

print(numbers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

make a list in a variable from the range function, but only print the even numbers

A

even_numbers = list(range(0, 30, 2))

print(even_numbers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

make an empty list, and put the first 10 square numbers into that list

A
squares = []
for value in range(1, 11):
       square = value ** 2
       squares.append(square)
or 
       squares.append(value**2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

make a list of numbers, then print the sum,, min, and max of that list

A

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(sum(digits))
print(min(digits))
print(max(digits))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is a list comprehension?

A

allows you to generate a list with just one line of code.

it combines a for loop and the creation of new elements into one line, and automatically append new elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

write a list comprehension using the creation of the first 10 squared numbers

A

squares = [value**2 for value in range(1, 11)]

print(squares)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Use a for loop to print the numbers from 1 to 20 inclusive from a list, both ways of making the list are fine.

A

numbers = list(range(1, 21))

for number in numbers:
print(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Make a list of the multiples of 3 from 3 to 30. Use a for loop to print the numbers in your list.

A

threes = list(range(3, 31, 3))

for number in threes:
print(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Use a list comprehension to generate a list of the first 10 cubes.

A

cubes = [number**3 for number in range(1,11)]

for cube in cubes:
print(cube)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

make a list with 10 values and slice parts of it

A

players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘sam’, ‘eli’, ‘jake’, ‘sean’, ‘matt’, ‘syd’]

print(players[0:3]

17
Q

slice the 2nd, 3rd, and 4th items of the list

A

print(players[1:4])

18
Q

what does omitting the first element of a slice do?

A

python automatically starts the slice at the beginning

19
Q

what does omitting the last element of a slice do?

A

the slice will go to the end

20
Q

loop through a slice

A

print(“here are the first three players on my team”)
for player in players[:3]:
print(player.title())

21
Q

make a list, and then make a new list by copying it

A
my_foods = ['pizza', 'falafel', 'carrot cake']
friends_foods = my_foods[:]
22
Q

add new values to your list and the new list

A

my_foods.appemd(‘cannoli’)

friends_foods.append(‘ice cream’)

23
Q

what does immutable mean

A

python values that cannot change

24
Q

what is an immutable list

A

a tuple

25
Q

make a simple tuple

A

dimensions = (200, 50)

print(dimensions[0])

26
Q

define a tuple with one element

A

my_t = (3,)

27
Q

loop through all the values in a tuple

A

for dimension in dimensions:

print(dimension)

28
Q

write over the tuple

A
dimensions = (200, 50)
dimensions = (400, 50