In [0]:
In [0]:
Try some values of your own to see if you can reconstruct how negative indices and left off start and end arguments work.
In [ ]:
In [0]:
In [0]:
In [0]:
In [0]:
In [ ]:
In [0]:
In [0]:
type(x)
type(x2)
x[0]
x2[0]
x[0:2]
x2[0:2]
x[1:4][1:3]
x2[1:4][1:3]
type(x2[1:3])
type(x2[1:3][1])
x2[1:3][1][3]
x[1:3][1][3]
In [0]:
In [0]:
x = 'abcdefgh'.split('c')
print(x)
print('abcdefgh'.split('r'))
a = 'hi, my name is Bob, this is Una'
print(a.split(','))
z = a.split(' ')
print(z)
print(z[1])
l = a.split() # this is a very useful trait of split, look up what happens when you split with empty parentheses
print(l)
caveman = a.split(' is ') # notice that multiple characters can be used as the delimiter
print(caveman)
In [0]:
'ACCGCGU,LLMNAQR,2.4'
'gene1 gene2 gene3'
'gene1, gene2, gene3'
In [0]:
x = [1, 2, 3]
y = 'ABCDE'
x[1] = 'ab'
print(x)
x[1:3] = 'R'
print(x)
y[1] = 'L'
print(y)
y = y[0:1] + 'hello' + y [2:]
print(y)
In [0]:
In [0]:
x = ['abcdefgh', 'cd']
y = x[0].split(x[1])
y = x[0].split(x[0][2])
y = x[0].split(x[0][4])[1]
y = x[0].split(x[0][4])[x[1].index('c')]
y = x[0].split(x[0][4])[1].split('g')
In [1]:
In general, you can make your code compact by putting the content of one operation into the input fields of the next, or readable by storing each step as a variable. Here is an alternate version of the final statement:
{python}
first_string = x[0]
delimiter = x[0][4]
first_list = first_string.split(delimiter)
new_string = first_list[1]
final_list = new_string.split('g')
'boring_genes; gene1:2.6, gene2:3.8, interesting_genes; gene4:1.9, gene5:8.2, gene6:9.1'
In [1]:
In [1]:
In [1]:
In [1]:
In [2]:
In [2]:
{python}
for hamster_plan in 'ALMJKLKJ':
print hamster_plan
for horse_vitamin in ['frosted', 'berry', 'cereal']:
print horse_vitamin
In [2]:
{python}
for horse_vitamin in ['frosted', 'berry', 'cereal']:
for bean_juice in horse_vitamin:
print bean_juice
In [2]:
In [2]:
In [ ]:
In [2]:
x = [[['gene1', 'heart'], ['gene2', 'brain']], [['gene4', 'appendix'], ['gene5', 'stomach'], ['gene6', 'esophagus']]]
In [ ]:
In [2]:
In [2]:
In [ ]:
dna = 'AATTACCGCATTCCACGGGACCTACGAATTATAGTACCTAAA'
i = 0
while i < 10:
....
print(dna[i])