Broadly understand the importance of logical decision making in computation
Become familiar with for loops, if/else, and while statements
Continue to use print statements to check output
Try out list comprehensions
First, some useful tools
using % notation in print statements makes for better output
%d: insert number here
%f: insert float here
%s: insert string here
random module is useful for generating fake data
Decisions and conditions: For, If, While
Remember: we program for Repeatability, Speed, and Automation. Using decisions and conditions we can automate a logical procession of actions.
A for loop is a way to iterate through a list, range, dictionary, string, etc. Use it for whenever you are repeating something.
Ok, let’s test this out with the print statement. Use this statement in for loops often to check for errors and make sure that everything is working as you would expect.
Let’s try something a bit more complicated. Here we can do a repeat of a mathematical operation on items in a list. Note that the x in for x in thing can be whatever you want, you just have to be consistent within the loop.
But, if you see in this code, there is a calculation that is repeated four times, so we should also use a for loop in this case too.
Now we can check what is in our dictionary.
A quick note: you can do for loops in bash too!
And in many many other languages: http://rosettacode.org/wiki/Foreach
If statements are used to test if your variable is true for some logical condition in order to determine what the program should do next. These are super handy and are essentially how you get the computer to make decisions for you.
Adding an else to your statement allows the computer to decide what to do when the conditional is False.
You can do nested if/else statements.
But there is a better way:
The if/elif statement can be used when you would otherwise use multiple nested if/else statements. They are performed in order and will not go through all of the options if one before the end is True.
IF and FOR together!
Using conditionals and for loops together makes your code very powerful.
Important tip: You can make a dictionary without knowing/defining a priori what they keys are
While loops repeat until some condition becomes False. Be careful as they are prone to infinite loops. If you get caught in one, hit Ctrl+C.
Example script with while loop:
While loops can ensure that user input is in the right format. They are also useful when you want to manipulate part of the conditional that is being used within the loop.
Try using this program, which has a test for user input using a while loop:
Here we manipulate part of the conditional (x) within the loop. Could you do this with a for loop? Why is the last number less than 40?
List comprehensions are faster ways to execute loops that result in lists. Your typical loop structure is shown below followed by the comprehension structure.
Python 2 Homework
Create your own python script that creates a genome with equal base frequencies (but randomly shuffled) of a user-requested size. Hint: use input(), random, and some of the text from the lesson.
Now rewrite that code into a for loop that allows you to create 30 sequences of length 100. Save them in a list.
Using a for loop and string indexing, print the position of the first T for each sequence in a readable format (use %d with the print statement).
Now add a variable to save this number within the loop rather than printing it.
Use this variable and the string module to cut the part of the sequence that comes before that index out of each sequence.
Repeat the last step but add an if statement so that it only cuts the sequence if the T is one of the first three nucleotides.
Write a for loop using your sequences that checks whether the sequence ‘AAAA’ is in them, and if it is, adds one to a counter. At the end of the loop, use the print statement with %d to state how many of your sequences had this subsequence.