- Log on to one of the sage servers at
https://131.212.65.98:8000
https://131.212.65.100:8000
https://131.212.66.214:5233
It may be helpful to have a browser tab open to the some sage/python documentation, in particular the tutorial by the creator of Python.
- Create a worksheet called "Assignment 1".
- Enter your first name as a string called my_name by typing
my_name = "Marshall Hampton"
(with your name instead). We can "slice" this string of letters by using the array index notation my_name[start:stop:increment]. For example, try the following: my_name[0:2]
my_name[0:]
my_name[1:2]
my_name[::-1]
As an exercise in "list comprehension", type [letter.upper() for letter in my_name]
You can split a string into a list: try my_name.split()
This is very useful in manipulating data from other files. For example, if you have an Excel file, you can export it to CSV (comma seperated values), read it into python, and split the lines with .split(',')
.
- For our next Python exercise, you will define a function. Here is an example function that computes the number of As and Ts in a string, after first converting all letters to uppercase:
def ATcounter(dna_string):
'''Computes the number of "A"s and "T"s in a string'''
temp_string = dna_string.upper() #first convert to uppercase
A_num = temp_string.count('A')
T_num = temp_string.count('T')
return A_num + T_num
The triple-quoted string after the first line is called a docstring; including a docstring is not mandatory but highly recommended.
Write a function "namesplitter" that takes your name as a string (i.e. like my_name above) and returns a list consisting of your first name and last name. So if my_name = "Marshall Hampton"
then name_splitter(my_name)
would return ['Marshall','Hampton']
- Our last exercise is about Python's dictionary data type. In other languages this might be called a hash table, associative array, or lookup table. Dictionaries have keys associated with values. For example, here is a dictionary of endonuclease patterns:
endo_dict = {'EcoRI' : 'gaattc', 'BamHI' : 'ggatcc', 'HindIII' : 'aagctt'}
The keys of endo_dict
are the strings EcoRI, BamHI, and HindIII.
We can access the values through the keys: try endo_dict['BamHI']
You can get all the keys of a dictionary with the .keys()
, and all the values with .values()
.
Write a function name_to_dict
that takes a name string as input and returns a dictionary where the key is the first name, and the value is the full name. For example, name_dict('Marshall Hampton')
should return {'Marshall':'Marshall Hampton'}
.