Lesson 3 Flashcards
What does it mean for something to be an iterable in Python?
it means that individual elements can be accessed, individually, making them amenable for use in a loop
General form of a for loop:
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
for loop take each element of _____, assigns it to _____, and then prints the value of _____
multi-element object (string or list), a variable, the variable
What are to important elements of the syntax of a for loop?
-the line defining the loop ends with a colon
-the code block executed by the loop is indented
What will the following program output:
my_seq = ‘GATCCTAG’
new_seq = ‘ ‘
for base in my_seq:
new_seq += base + ‘_’
print(new_seq)
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 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)
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
write a list that contains Alice, Jane, Mary, and Pat
my_list = [‘Alice’, ‘Jane’, ‘Mary’ , ‘Pat’]
What is the necessary syntax for lists?
The info is in square brackets and separated by commas
What types of data can be contained in a list?
strings (make sure to use quotes!), integers, floating point numbers, and even other lists
If you have a list in a list, what is the format of the output?
has double square brackets on either end and has single square brackets separating each list
[[‘a’,’b’,’c’][1,2,3,4]]
How to concatenate two lists
use + sign
If you concatenate [1,2,3,4] + [‘a’,’b’,’c’] what is the output?
[1,2,3,4, ‘a’,’b’,’c’]
What happens if you do 2*list2?
list2=[‘a’,’b’,’c’]
[‘a’,’b’,’c’,’a’,’b’,’c’]
Does order matter when multiplying lists?
no
if you do my_list[2:4] what do you get?
[‘Alice’, ‘Jane’, ‘Mary’ , ‘Pat’]
[‘Mary’,’Pat’]