Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1574
Kernel: Python 2 (system-wide)

Variables

  • Lets make a variable:

  • Execute the code by pressing ctrl + enter

    • Name: first_name

    • Value: Aridaman

first_name = "Sander" sur_name = "Boymans" print first_name + " " + sur_name
Sander Boymans

Exercise:

  • Print your surname using a new variable.

  • Can you print both your first and surname?

number3=8/3.0 print number3
2.66666666667

Strings - methods

  • All types have build in methods to manipulate them.

  • For example:

    • my_string.upper() - returns the string in uppercase

# Write here your code

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

# Write here your code

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

"ABC" + "DEF"
'ABCDEF'
"ABC" * 2
'ABCABC'
"ABCDEF"[3]
'D'
"ABCDEF"[2:4]
'CD'
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]

# Write here your code

Strings - Special characters

  • Tabs: \t

  • Newlines: \n

  • Quotes: "

  • Backslash: \\

print "Tab\tTab\nSecond Line\nPrint out a \"backslash\" : \\"
Tab Tab Second Line Print out a "backslash" : \

Comments

  • Comments start with #

  • Useful for:

    • Explaining code or your program

    • Disable parts of your program

# This is an example script using comments print "Comment example script" # print "This will not print" print "This will print"
Comment example script This will print

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

your_name = raw_input("Please type your name:") print your_name
# Write here your code

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

x = 5.0 y = 2.0
x + y
x - y
x / y
x * y
x ** y
x % y
5 * (2+1)

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 %

# Write here your code

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

"2" + "2"
int("2") + int("2")
str(2) + str(2)
int("this does not work, why?")
float(5) / float(2)
int(5.0) / int(2.0)

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.

# Write here your code

Boolean: True or False

  • Booleans are the outcome when you compare or test variables

  • Examples:

x = 2 x < 3
x = "yes" x == "no"

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.

5 > 4
3 < 5
4 == 4
4 <= 4
4 >= 3
3 != 3
"yes" != "no"
"yes" == "yes"

Boolean – exercise

  • Just like before, try to guess the result before executing the comparisons

5 > 3 and 5 > 1
5 < 3 or 5 > 1
not 5 > 3
False
not 5 > 3 or 5 > 1
True
not (5 > 3 or 5 > 1)
False

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:

x = 5 if (x < 0): print "x is negative" elif (x > 0): print "x is positive" else: print "x is 0"

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

# Write here your code

Loops – while

  • while [removed]:

  • < tab > do something

number = float(raw_input("Please provide a number: ")) while number < 20: print number number += 1

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

# Write here your code

Loops – for

  • for [removed] in [removed]:

  • < tab > do something

string = "my string" for char in string: print char

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

# Write here your code