Python Exercises
Python Exercises
All exercises should be completed in Spyder unless anything else is specified – enjoy!
Exercise 0: Se,ng up the conda environment
0a)
To start the exercises, ac1vate the conda environment python_day2 from the command line.
1b)
Print the sentence:
"A python ate my mother-in-law, what should I do?"
1c)
Define three variables (VarA, VarB, VarC), and print the statement:
"Hello VarA, I am sorry I’m late. My VarB ate my VarC"
1d)
From the variable below, print only the posi1ve part of the sentence using slicing:
VarD = "He's lazy, he's ugly, but he smells good"
2b)
Use one of the integrated Python func1ons to convert the integer 3 into a float
2c)
Which type/class is True and False?
2d)
Test out the following statements in Spyder and note if they are defined as True or False:
1 + 1 == 2
2 + 4 == 5
1 == 1.0
2 != 2
1
Exercise 3: Lists
3a)
Create a list of five things that should be in a burger, along with five of your favorite numbers
(these can be floats and/or integers). Call that list burger_list and print it.
Hint: “1” is not an integer
3b)
Using indexing, print out the third, seventh, and tenth item of burger_list
4b)
Make an if statements, that tests if the third, seventh and tenth element in burger_list is a
string.
Hint: Use indexing
5b)
Combining a for loop with an if statement, print every string (X) in burger_list.
Let the print statement be “X really goes in a burger!”.
Hint: type(X) == str:
Add comments inside the script at all loca1ons where you see a #.
Think about the following:
• What does the “a” mean on line 10?
• Why is header assigned as line[1:] and not just line?
• Why is fasta_headers.txt not closed?
2
Run the script from the command line. Is the outcome as you expected?
Exercise 7: Biopython
7a)
Download the file called get_fasta_length.py from EVA and place it in
/home/student/BTG_intro/Scripts. Open the script in Spyder. Read through the script and
note what you expect to happen when execu1ng it. Run the script from the command line
and observe the output.
7b)
In Spyder, add # in the beginning of line 13 and 14 and note what you expect to happen
when running the script now. Run the script from the command line and observe the output.
7c)
Using nano, replace line 5 with the following:
fasta_file = sys.argv[1] #Takes the first argument as input
Run the script from the command line and use MRSA2.fasta as argument for the script.
Extra exercises
While Loop
a)
Set a variable sheep to 0. Using a while loop, print the amount of sheep and increase the
number of sheep by 1 un1l you reach 23.
b)
Create the duck list below:
duck_list=["duck", "duck", "duck", "duck", "duck", "duck", "duck", "goose",
"duck", "duck", "duck"]
Using a for loop, count the ducks and stop when you reach the goose. Print “Goose!” and
the number of ducks you counted before the goose.
3
Making a funcRon
a)
Improve the if statement from exercise 4a and make it into a func1on so that running
higher_than(X) will return one of the following:
"X is not higher than 68"
"X is higher than 68"
"both numbers are 68"
b)
Improve the funcJon from the previous exercise and make it take two inputs (integer or
float) so that running higher_than(X,Y) will return one of the following:
"X is not higher than Y"
"X is higher than Y"
"Both numbers are X" (if X and Y are the same)
Sets
Using the set() constructor func1on, convert duck_list to set and observe the difference.
DicRonaries
a)
Create a dic1onary called favorite_foods with your top three favorite foods as keys and their
corresponding deliciousness levels (on a scale of 1-10) as values. Print the dic1onary to see
your favorite foods and how delicious they are.
Choose one food and print its deliciousness level along with an explanatory message by
retrieving the informa1on from the dic1onary.
Example: The deliciousness level of broccoli is 10
Modify the deliciousness level to make it even more delicious and make a new explanatory
message.
Example: Actually, the deliciousness level of broccoli is more like 11
Add your least favorite food to the dic1onary along with its corresponding deliciousness
levels and update the explanatory message.
Example: I don’t care for pizza; I only find its deliciousness level to be 3
b)
Make a dic1onary called my_dict with a key called "number" and a key called "food" that
each have an empty list as value.
Make a for loop that iterates through the burger_list you created in exercise 3a and append
all strings to key: “food” and all int/floats to key: “number”.