Part 1: Iteration, Summaries of English

Python has several features that make iteration easy. while loops while loop statements are used to repeat blocks of code while some condition is true:.

Typology: Summaries

2022/2023

Uploaded on 02/28/2023

jacksonfive
jacksonfive 🇺🇸

4.4

(35)

280 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Part 1: Iteration
Iteration is a common feature of computer programs. Iteration is the repetition of a specific set of
statements a specified number of times or while some condition is true. Python has several
features that make iteration easy.
while loops
while loop statements are used to repeat blocks of code while some condition is true:
while some condition is True:
do something
For example:
In [ ]:
In plane English the code reads:
"While n is greater than 0, print n, then subtract 1 from n."
So while n is greater than 0, the while statement evaluates to true and the block of code is
executed. After n iterations, n is reduced to 0, the while condition than evaluates to False, and
the code is not executed.
Infinite loops
Within the body of the while loop the value of a variable is often changed such that the
condition tested in the while statement no longer evaluates to True after some number of
iterations and the loop terminates. Otherwise, and infinite loop is born.
Let's write a function, countup() , that contains a while loop that counts up from 0 to n:
def countdown(n):
while n > 0:
print(n)
n -= 1 # subtract 1 from n, equivelent to n = n - 1, this is called decrementing
countdown(10)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Part 1: Iteration and more Summaries English in PDF only on Docsity!

Part 1: Iteration

Iteration is a common feature of computer programs. Iteration is the repetition of a specific set of statements a specified number of times or while some condition is true. Python has several features that make iteration easy.

while loops

while loop statements are used to repeat blocks of code while some condition is true: while some condition is True : do something For example: In [ ]: In plane English the code reads: "While n is greater than 0, print n, then subtract 1 from n." So while n is greater than 0, the while statement evaluates to true and the block of code is executed. After n iterations, n is reduced to 0, the while condition than evaluates to False, and the code is not executed.

Infinite loops

Within the body of the while loop the value of a variable is often changed such that the condition tested in the while statement no longer evaluates to True after some number of iterations and the loop terminates. Otherwise, and infinite loop is born. Let's write a function, countup() , that contains a while loop that counts up from 0 to n : def countdown(n): while n > 0 : print(n) n -= 1 # subtract 1 from n, equivelent to n = n - 1, this is called d countdown( 10 )

In [ ]: To escape an infinite loop, use ctrl-c in the terminal or restart the notebook kernel.

break and continue

Although infinite loops are often unintentional, they can actually be quite useful. Here's a function, seq_len() , that prompts the user for a sequence and prints the length of the sequence: In [ ]: The while statement will always evaluate to True because True is True. We can use the keyword break to exit the entire loop in which it's contained. Thus, you can evaluate whether or not a condition is True at any point within the loop and exit the loop if desired - an affirmative approach (stop when some condition is True) rather than a negative approach (keep going until some condition is False). Let's modify the seq_len() function to exit when the user hits the return key instead of typing a sequence: In [ ]: Let's modify the seq_len() function to ignore input that doesn't contain at least one valid nt: def countup(n): num = 0 while num <= n: print(num) num += 1 countup( 10 ) def seq_len(): while True : nts = len(input("Enter a sequence: ")) print(nts) seq_len() def seq_len(): while True : nts = len(input("Enter a sequence: ")) if nts == 0 : break print(nts) seq_len()

for loops

for loops are for looping over a defined list of objects. for loops are typically used when we want to repeat a block of code a fixed number of times, as opposed to a while looop in which we want to repeat something until some condition is met: for some elements in a sequence: do something For example: In [ ]: In plane English, the code reads: "For each nucleotide in the variable seq, print the nt." Within the print statement, we can specify the ends to be empty strings, as opposed to new lines using end = ''. Let's write a function, comp() , that returns the complement of a sequence: In [ ]: seq = 'ATGTATA' for nt in seq: print(nt, end = '') seq = 'ATGTATA' def comp(seq): c = '' for nt in seq: if nt == 'A': c += 'T' elif nt == 'T': c += 'A' elif nt == 'C': c += 'G' elif nt == 'G': c += 'C' else : print("Unregonized nt") return c comp(seq)

The range function

To iterate over a simple list of number, use the range function. The range() function allows you to specify a range of integers to iterate through using the following syntax: range(start, stop[, step]). Essentially, it generates a list of numbers between start and stop at optional step intervals which are generally iterated over in for loops. Here's a simple example of range() in action: In [ ]: Note that the stop number in the range is not part of the sequence. By default, if only one argument is given, it's treated as the stop value, start defaults to 0 and step defaults to 1: In [ ]: If two arguments are passed to the function, step defaults to 1: In [ ]: Let's print all even numbers between 1-10: In [ ]: Print all odd numbers less than 9: In [ ]: range() can also iterate up from a negative numbers but the number with the lower value must be the start value: In [ ]: To iterate from a higher number to a lower number, use negative values for step. For example, print each number from 10 to 0: for i in range( 0 , 10 , 1 ): print(i) for i in range( 10 ): print(i) for i in range( 0 , 10 ): print(i) for i in range( 2 , 11 , 2 ): print(i) for i in range( 1 , 9 , 2 ): print(i) for i in range( - 10 , 0 ): print(i)

Indexing and slicing

To capture a single character in a string, we can specify an index (position in the string) using the bracket operator: string[index] This is called indexing. To capture a longer substring within a string, we also use the bracket operator but include a : : string[start:end:step] Or equivelently: string[slice(start, end, step)] This is called slicing. In most programming languages indexing of strings and other objects starts at 0. Thus, the largest value an index can be is len(string) - 1. In [ ]: In [ ]: If we want to count from the end of a string, we can use negative values as our indexes: In [ ]: Let's write a function, kmer(seq, k) , that prints every possible sequence of length k (k-mer) from a given DNA sequence: num = '123456789' # indexing print(num[ 1 ]) # second position = index 1 print(num[ 0 ]) # first position = index 0 print(num[ - 1 ]) # last position = index - num = '123456789' # slicing print(num[ 0 : 9 ]) # indexes 0- print(num[ 1 : 5 ]) # indexes 1- print(num[ 1 :]) # indexes 1-last print(num[: 9 ]) # indexes first- print(num[:]) # entire string print(num[slice( 0 , 9 )]) num = '123456789' num[ - 3 ]

In [ ]: Python doesn't have a built-in function for reversing a string, however, we can do so using slices: In [ ]: Let's write a function, every_other_codon() , that concatenates every other codon in an RNA sequence: In [ ]:

Stings are immutable

Existing strings cannot be changed. So while, it may be tempting to reassign a character in a string to a different value, it is not permissable: In [ ]: We can use some of the comparison operators with strings, for example to test if a single character is lowercase, we can use <= : In [ ]: Or to test the alphabetical order of some strings: dna = 'ATAGCTAGCTTTAC' def kmer(seq, k): for i in range(len(seq) - k + 1 ): print(seq[i:i + k]) kmer(dna, 6 ) name = 'LaSieg' name[:: - 1 ] rna = 'AAAGGGUUUCCCUUU' _# ___ ___ ___

1 2 3 4 5_

def every_other_codon(seq): new_seq = '' for i in range( 0 , len(seq), 6 ): new_seq += seq[i:i + 3 ] return new_seq every_other_codon(rna) seq = 'ATG' seq[ 1 ] = 'U' print('a' <= 'A') # all uppercase characters print('A' <= 'a') # come before lowercase characters

A return value of -1 indicates that the string was not found: In [7]: Note that str is the name of the class that represents string objects in Python. If you would like to know what other methods a string has, you can type str. followed by tab for autocomplete to see all the possible options: In [ ]: In case you're unsure what a given method does, you can use Python itself to get some help on it: In [8]: But more likely you'll do a google search and find a list of methods like the one here (https://docs.python.org/3/library/stdtypes.html#string-methods). Another very useful string method is str.replace(old, new[, count) for replacing one string with another. For example, let's replace all Ts in a DNA sequence with Us: In [11]: If we want to replace only the first instance, we can use the optional count parameter: Out[7]: (^) - Help on method_descriptor: count(...) S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. Out[11]: 'UAAUGAGAUGCCCCGCGACGACAACGAC' dna = 'TAATGAGATGCCCCGCGACGACAACGAC' dna.find('ATG', 10 , 20 ) str. help(str.count) dna = 'TAATGAGATGCCCCGCGACGACAACGAC' rna = dna.replace('T', 'U') rna

In [15]: We can use the string method str.translate to do more complex substitutions. Let's replace A, C, G, and T with 1, 2, 3, and 4, respectively, within a string. We first have to have a translation table: In [16]:

String parsing

In many programming applications we need to process strings in various ways. This is called parsing. For example, let's extract the domain name associated with an email address such as [email protected] (mailto:[email protected]), using a slice and the str.find method: In [18]: To return an uppercase string you can use str.upper() , where str is your string or a variable containing your string: In [19]: Notice, however, that the variable itself is unchanged. Alternatively, you can use the syntax str.upper('string') : Out[15]: 'UAAUGAGATGCCCCGCGACGACAACGAC' 12423142313 Out[18]: 3 Out[19]: 'WOCKET' dna = 'TAATGAGATGCCCCGCGACGACAACGAC' dna.replace('T', 'U', 2 ) input_chars = 'ATCG' output_chars = '1234' translation_table = str.maketrans(input_chars, output_chars) nts = 'ATGTCAGTCAC' nums = nts.translate(translation_table) print(nums) email = '[email protected]' i = email.find('@') email[i + 1 :] i = '[email protected]'.find('@') i word = 'wocket' word.upper()