Python 2: Logic, Loops, iterators
Guest Lecture by Sawyer Masonjones
2.1: Logic
if foo==bar:
doThis(foo)
else:
doThat(bar)
2.1 Syntax of If / Loop statements
Python determines structure of if/else statements looking for the “:” and the code that follows needs to be indented
If counter > 10:� print(counter, “ is a big number”)�else:� print(counter, “ is smaller or equal to 10”)
This can be a tab or 4 spaces. But don’t mix tabs and spaces!
2.1 Logical Expressions
2.1 Logic Operators
���������
foo | Not foo |
T | F |
F | T |
foo | bar | foo or bar |
T | T | T |
T | F | T |
F | T | T |
F | F | F |
foo | bar | foo and bar |
T | T | T |
T | F | F |
F | T | F |
F | F | F |
2.1 Logic in Python
If statements start the conditional statement, executing the indented code. Elif statements extend this with new conditions, doing the same. Else is used to execute everything else ie the false case. You don’t need to finish with an else statement.
if foo == bar:
doThis()
elif foo > bar:
doThat()
else:
doThisAndThat()
2.2 Loops
2.2 For Loops
for i in range(1,11): #count forwards
print i
for i in range(10,-1,-1): #count backwards
print i
for i in range(1,11,2): #count by 2s
print i
2.2 For Loops and lists
for i in range(len(list)):
print list[i]
for item in list:
print item
2.2 While Loops
Iterate until a condition is false
Count = 100
while count > 0:
print count
count -= 1
2.2 Break and Continue
for item in list:
for i in item:
if i == ‘A’:
break #breaks inner most loop
for item in list:
for i in item:
if i == ‘A’:
continue #continues to next iteration
2.4 Iterators
Iterator = iter(list) #turn iterable object to iterator
next(iterator) #get next object
2.4 Iterator example
list = [0,1,2,3]
iterator = iter(list)
next(iterator) #0
next(iterator) #1
next(iterator) #2
next(iterator) #3
next(iterator) #Throws error StopIteration
2.4 Iterators and loops
for item in iterator:
print( item)
while True:
try:
next(iter)
except StopIteration:
break
Putting it together
Problem: You have a DNA sequence and you need to calculate GC content.
Write a script that iterates over a sequence to calculate GC content across the entire sequence.
Write a script that iterates over a sliding window across the sequence.
2.2 Sequences
2.2 List methods
list = [] #Creates empty list
list = [1,3,2,5,21,5] #Creates list with values
list[i] #access ith element of list
list[-j] #access jth element starting from end of list
list[i:j] #slices list from position i to j. Leaving i or j blank slices from begining (i) or end (j)
list.insert(i,value) #inserts value at ith position
list.append(value) #Adds value to end of list
2.2 Lists Methods Continued
list.pop() #removes item from end of list and returns it
list.pop(i) #removes item from the ith position and returns it
list.extend(list2) #adds items from list2 onto list 1
list.remove(value) #removes first item with value
list.count(value) #returns count of items with that value in list
list.index(value) #returns index of first item with value.
list.index(value,start,end) #returns index of first item in range [i:j]
list.copy() #return copy of list
list.clear() #empties list
2.2 Stacks and Queues
stack = [1,2,3,4,5]
stack.append(6) #stack is now [1,2,3,4,5,6]
stack.pop() #stack is now [1,2,3,4,5]
Queue uses pop(0) to take from front of list.