Variables
Lets make a variable:
Execute the code by pressing ctrl + enter
Name: first_name
Value: Aridaman
Exercise:
Print your surname using a new variable.
Can you print both your first and surname?
Strings - methods
All types have build in methods to manipulate them.
For example:
my_string.upper() - returns the string in uppercase
Exercise: Strings – Methods
Assign a random number of DNA nucleotides to a variable.
Print the DNA sequence in uppercase
Count the number of A, C, G and T’s within the sequence
Store these numbers in a new variable and print them
Replace all A’s with T’s
Time remaining?
Try other string methods
Strings – Operators
+ Concatenate a string
* Repetition of a string
[n] Slice, returns nth value (Python counts from 0)
[x:y] Range slice, returns the xth to yth value
Not an operator but very useful:
len("ABCDEF") = 6
Exercise: Strings – Operators
Create two variables and assign a random number of DNA nucleotides to both.
Combine both variables and print
Add 10 copies of the second dna sequence to the combined sequence.
Print the total length of the combined sequence
Print the 5th nucleotide
Print the 7th to the 14th nucleotide
Bonus: print the last character of "gaattc" but not by using string[5]
Strings - Special characters
Tabs: \t
Newlines: \n
Quotes: "
Backslash: \\
Comments
Comments start with #
Useful for:
Explaining code or your program
Disable parts of your program
Last exercise on strings
You can let Python ask you to input a variable:
your_name = raw_input("Please type your name:")
print your_name
raw_input always returns a string!
Let Python request your first and last name
Print them using \t, \n, " and/or \\
Play around with this for a couple minutes
Numbers
Integers: 1, 5, etc.
Floats: 1.5, 5.2, etc.
Simple math using numbers (x = 5.0 and y = 2.0) :
Addition: x + y = 7.0
Subtraction: x – y = 3.0
Division: x / y = 2.5
Multiplication: x * y = 10.0
Power: x ** y = 25.0
Modulo (remainder): x % y = 1.0 (2+2=4 5-4 = 1)
Python uses the standard order of operations
5 * (2+1) = 15
Numbers – Exercise
Assign the value 8 to number1
Assign the value 3 to number2
Make number3 equal to the division of number1 by number2
Print the number3
Does the result make sense?
Once it does, input a DNA sequence and calculate the GC %
Converting types
Sometimes we need to convert types
str(variable) converts variable to a string
int(variable) converts variable to a integer
float(variable) converts variable to a float
Last exercise on numbers
Write a script that asks the user for
his/her name
length (m)
weight (kg)
Calculate the bmi of the user
user_bmi = weight / length2
Print out with one print command:
The used formula to calculate bmi and on the next line the name and bmi separated by a tab.
Boolean: True or False
Booleans are the outcome when you compare or test variables
Examples:
Operators for comparison
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal
!= not equal
Boolean – exercise
Below are a couple comparisons. Try to guess the result before executing the comparisons in python.
Boolean – exercise
Just like before, try to guess the result before executing the comparisons
Control flow – if / else / elif
With if, else and elif we can control the execution of certain parts of a program.
if (condition):
< tab > do something
Example:
Control flow – if – exercise
Write a script that asks the user for a nucleotide
Check whether the nucleotide is a adenine, thymine, cytosine or guanine
Print out the nucleotide type or an error message if the input is not a nucleotide
Loops – while
while [removed]:
< tab > do something
Loops – while – exercise
Ask the user to enter some DNA sequence
As long as the length of the DNA sequence is smaller than 100 keep the user asking for more DNA sequence and add it to the piece you already have
Once you have more than 100 nucleotides print the result
Loops – for
for [removed] in [removed]:
< tab > do something
Loops – for – exercise
Ask the user for a DNA code of exactly 10 nucleotides, and check this
Using “for” start at nucleotide 1 and print one by one until and including nucleotide 5
Time remaining?
Ask the user for a random DNA code with a minimum length of 3 nucleotides and print the last 3 nucleotides going backwards