Coding for Codons: Python Notes 2

(Variables, Data Structure, Control Flow, Function, Error Handling, User Input) Continuing with my Python notes, this post has information on Python variables, control flow commands, data structures, and other items.

  1. Variables
  2. Data Structures
    1. Lists
    2. Tuples
    3. Sets
    4. Dictionary
  3. Control Flow Statements
    1. If/Elif/Else
    2. For
    3. While

Variables

Unlike other programming languages, variables in Python don’t have to be declared. The data type of the variable can be changed even after a value is set using a cast statement! Here are some examples of variables. The values of the variables can be output to the screen using the print statement.

# Variables in Python
sStartCode = "ATG" # variable sStartCode is a str data type variable
iStartPos = 0 # variable iStartPos is an int data type variable
sRank = str(3) # variable sRank stores 3 as str "3"

# Output the value of the variable using print statement
print(sRank)
# Concatenate text and output the value
print(sStartCode + " AAA")

The output of the above code snippet is shown below.

3
ATG AAA

Data Structures

Lists

Lists are a type of data structure that contains multiple values in a variable. Each value in a list can be accessed using the index. You can add new items to an existing list using the append statement. You can update the value in an existing list by specifying the index and assigning a new value. You can use the len statement to return the no. of values in a list. More statements related to lists can be found here.

Tuples

Tuples are another data structure in Python that is similar to lists.

Sets

Sets are data structures that only permit unique values. It can be used to remove duplicates or check if the value exists in the collection of values in a set.

Dictionary

Dictionary is a type of data structure called a mapping type that stores multiple key-value pairs where the key is used to retrieve the associated value.

# This is an example of a list
sStopCodes = ["TAA","TAG","TGA"]
print (sStopCodes)

# Each value in the list can be accessed by an index
sStopCodes[1] # returns "TAG"

# Update a value by specifying the index and assigning a new value
sStopCodes[0] = "UAA"
print (sStopCodes)

# Add a new value to the list
sStopCodes.append("UAG")
print (sStopCodes)

# Display the length of the list
print (len(sStopCodes))

# This is an example of a set
setNos = {3,6,1,2,2,6,5,8,9,0,3}
# printing the setNos only returns unique values
print(setNos)

# This is an example of a dictionary
dictPairs = {"A": "T", "C": "G", "G": "C", "T": "A"}
# Returns the value from a dictionary based on a key
print (pairs["C"])
['TAA', 'TAG', 'TGA']
['UAA', 'TAG', 'TGA']
['UAA', 'TAG', 'TGA', 'UAG']
4
{0, 1, 2, 3, 5, 6, 8, 9}
G

Control Flow Statements

Control flow statements specifies whether statements in a program is executed or not based on some condition.

If/Elif/Else

The if/elif/else statement tests a condition and based on the result of the test, executes the code. If the code under the if statement tests false, it goes to the following elif statement, if any, and executes the code till one test is successful. If none of the tests are successful, it finally executes code in the else statement. To identify a code block to be executed, indent the lines.

For

The for statement iterates over items in a list or other sequences. In other languages, this statement is used to iterate a fixed sequence of numbers. For more uses of the for statement, e.g. with range() statement or else statement, see this page on w3schools.com.

While

The while statement executes code in the indented code block till a condition is not satisfied. If the condition is satisfied, it will keep executing the code, forever!

# Example of if/elif/else control flow statement
ildl = 147

if ildl < 100:
   # indent the lines to execute them as a code block
   print("LDL Cholesterol is optimal")
elif 100 <= ildl <= 129:
   print("LDL Cholesterol is near or above optimal")
elif 130 <= ildl <=159:
   print("LDL Cholesterol is borderline high")
elif 160 <= ildl <=189:
   print("LDL Cholesterol is high")
else:
   print("LDL Cholesterol is very high - LDL > 190 mg/dL")

# Example of a for control flow statement
fglucosereads = {132, 123, 89, 122, 126, 91, 139, 134, 129, 136}
fglutot = 0
fgluavg = 0
fa1c = 0

# loop through each item in the list and process the item fgluread
for fgluread in fglucosereads:
   # indent the lines to execute them as a code blockprint
   fglutot = fglutot + fgluread
# the following lines are executed after the for statement completes executing the code block
fgluavg = fglutot / len(fglucosereads)
fa1c = (46.7 + fgluavg) / 28.7
print("This is your A1C after 10 days of glucose reading " + str(fa1c) + "%. Normal A1C is below 5.7%")

# Example of a while control flow statement
sDNAString = "AGACTGATTGCATCAGGTGG"
bEOL = False
iIndex = 0
sCodon = ""

#while end of the line is not reached or if the last set of necleotides is less than 3
while not bEOL:
   sCodon = sDNAString[iIndex : iIndex + 3]
   if iIndex <= len(sDNAString) and len(sCodon) == 3:
      print(sCodon)
      iIndex = iIndex + 3
   else:
      bEOL = True
LDL Cholesterol is borderline high
This is your A1C after 10 days of glucose reading 5.881533101045297%. Normal A1C is below 5.7%
AGA
CTG
ATT
GCA
TCA
GGT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s