1+2
1+'2'
'1'+'2'
AC+GT
'AC'+'GT'
'AC'+1
'AC'+'1'+'GT'
1/2
2/2
3/2
4/2
'4'/2
'4'/'2'
'4/2'
1/2.0
str
) to figure out what 'ACACAGT'.count('A')
would return and what 'ACACAGT'.count('AC')
would return. Based on this, what should 'ACACAGT'.count('C') + 6
return? Try using the python documentation to learn about the function called len()
.
str
and list
.
len('ACACAGT')
? With this, use count
and len
to calculate the AT content of the above phrase.
(1 + '2/4') / 6
##Exercises 2: variables and types (45 minutes):
int
, str
, and float
that Python can use to convert values between data types. See if you can predict the output of the following commands. Note that Python follows the mathematical order of operations as well as the order established in math for parentheses. Try some of your own also.str(1)
int('bear')
str(1) + str(2)
'1' + '2' + 5
int('1') + int('2')
int('1' + '2') + 5
int(str(1 + 2) + '6') / 2
float(1) / 2
float(1 / 2)
1 / float(2) + 1
float(1) / float(2)
'='
sign to store some values as variables. Note that this '='
is not a math sign anymore! 1 = 0 + 1
is not valid in Python, and x = 3
is valid but 3 = x
is not.
x = 1; y = 2
x + y
a = '1'; b = 2
a + b
first_part = 'AC'; second_part = '8'
second_part + first_part
m = n
n = 3
m = n
m + first_part
m = 'n'
m + first_part
m + m
z = x + y
.x = x + y
. What is x
now? (type the variable name followed by the alt + enter to see what's currently stored in any variable). What happens to x
if you do x = x + y
again and again? If you initially chose strings for x
and y
, try it with integers. If you chose integers, try it with strings.
str
and the entry under replace
, have Python convert a string of DNA into a string of RNA and store the result as a variable.
##Exercises 3: conditionals and scripts (1 hour)
3 < 5
3 = 5
3 == 3
3 == 5
3 <= 3
3 =< 3
type(3) == int
type('3') != int
type('here') == str
'AT' in 'ATG'
'AG' in 'ATG'
'AA' < 'AC'
'AA' not in 'GCT'
'GA' < 'AC'
'AA' != 'AT'
'A' in 'AAC' and 'T' in 'AAC'
'A' in 'AAC' or 'T' in 'AAC'
'AA' == 'aa'
'aa' > 'AA'
'a' in 'AA'
'A' in 'AAC' or 'AA' in 'AAC' and 'ac' in 'AAC'
('A' in 'AAC' or 'AA' in 'AAC') and 'ac' in 'AAC'
'A' not in 'AAC' or 'AA' in 'AAC' and 'ac'.upper() in 'AAC'
# (use the Python documentation on str
to figure out what upper
is doing)
if 3 < 5:
print '3 is smaller than 5'
if 9999999 < 'AA':
print 'AA must be big!'
if 'AACC' < 'AAAC':
smaller = 'AACC'
else:
smaller = 'AAAC'
print 'smaller is ' + smaller
index
from the Python documentation on str
to figure out how to determine the location of a character or short substring within a longer string). The result may not be what you expect. Try the same thing searching for the character 'A'.
if
statement needs to be revised so that it finds 'Q' and doesn't give an error when it searches for 'M'.