Lesson 3 Flashcards

1
Q

What does it mean for something to be an iterable in Python?

A

it means that individual elements can be accessed, individually, making them amenable for use in a loop

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

General form of a for loop:

A

for my_value in my_seq
code bloc

where my_value represents individual elements in my_seq and my_seq represents multi-element data object, such as a text string or list

code block represents one or more python statements that are executed one time for each element in my_seq

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

for loop take each element of _____, assigns it to _____, and then prints the value of _____

A

multi-element object (string or list), a variable, the variable

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

What are to important elements of the syntax of a for loop?

A

-the line defining the loop ends with a colon
-the code block executed by the loop is indented

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

What will the following program output:

my_seq = ‘GATCCTAG’
new_seq = ‘ ‘
for base in my_seq:
new_seq += base + ‘_’
print(new_seq)

A

G_A_T_C_C_T_A_G
so each time the code runs, the current new_seq gets the base and and underscore added

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

How is the output from this:

my_seq = ‘GATCCTAG’
new_seq = ‘’
for base in my_seq:
new_seq += base + ‘_’
print(new_seq)

different from the output for this:
my_seq = ‘GATCCTAG’
new_seq = ‘’
for base in my_seq:
new_seq += base + ‘_’
print(new_seq)

A

Because in the first example, the print statement is not in the loop, new_seq is not set back to the empty string at each iteration of the loop, so everything just gets printed out in one line

In the second example, because the print statement is part of the loop, new_seq gets reset to the empty string each time, resulting in each base being printed on a new line with an underscore beside it

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

write a list that contains Alice, Jane, Mary, and Pat

A

my_list = [‘Alice’, ‘Jane’, ‘Mary’ , ‘Pat’]

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

What is the necessary syntax for lists?

A

The info is in square brackets and separated by commas

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

What types of data can be contained in a list?

A

strings (make sure to use quotes!), integers, floating point numbers, and even other lists

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

If you have a list in a list, what is the format of the output?

A

has double square brackets on either end and has single square brackets separating each list

[[‘a’,’b’,’c’][1,2,3,4]]

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

How to concatenate two lists

A

use + sign

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

If you concatenate [1,2,3,4] + [‘a’,’b’,’c’] what is the output?

A

[1,2,3,4, ‘a’,’b’,’c’]

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

What happens if you do 2*list2?
list2=[‘a’,’b’,’c’]

A

[‘a’,’b’,’c’,’a’,’b’,’c’]

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

Does order matter when multiplying lists?

A

no

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

if you do my_list[2:4] what do you get?
[‘Alice’, ‘Jane’, ‘Mary’ , ‘Pat’]

A

[‘Mary’,’Pat’]

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

if you do my_list[0], what do you get? [‘Alice’, ‘Jane’, ‘Mary’ , ‘Pat’]

A

‘Alice’

17
Q

When a single element is specified in brackets after a list what happens? What about when you specify something in a slice even if it returns only one element?

A

first part: you get just that element, ex: ‘Alice’
second part: you get the element, but as a list [‘Mary’]

18
Q

How would you interpret list5 [1][1:4]?

A

Look for elements 1-3 in whatever element 1 in list 5 is. Even if list 5 is another list, you still look at element 1 in it!

19
Q

Are lists mutable?

A

YES

20
Q

How can you change an element in a list?
list4= [1,2,3, ‘a’,’b’,’c’]

A

list4 [3] = [‘A’]
this replaces ‘a’ with ‘A’

21
Q

How do you replace more than one element in a list at the same time?

A

list4[3:6] = [‘A’,’B’,’C’] can also do list4[3:6] = [‘A’] and a single ‘A’ will be put in

22
Q

what does the method append do?

A

adds an item to the end of a list

23
Q

how to use the append method

A

list1.append(‘six’)

24
Q

What happens if a list is used as the argument in the append method?

A

the list is added in brackets and everything so would be added as [6,7,8] exactly even if its being added to another list- would look like [1,2,3,4,5,[6,7,8]]

25
Q

How do you remove items from a list?

A

use the pop method- list.pop()

26
Q

What happens if no argument is provided in list.pop()?

A

the last element in the list is removed at the list.pop() method will return that last element by itself

27
Q

How do you insert elements into a list?

A

use the .insert() method

28
Q

Describe the syntax for the .insert(method)

A

the first argument in parentheses is the position before the element to be inserted

the second argument is the element to be inserted

so list1.insert(3, ‘two’) puts ‘two’ into position 4 (start counting with 1, so this gets put after the third element)