Commando’s python
1. Go to jupyter notebook log in
a. String = sequence of characters
b. Error: SyntaxError (eg. No “”) & NameError (eg. prin > print)
Print(‘x’) Printing a message to the screen
Print(“x”) x = argument
Print(“he said, ‘x’”)
Print(‘he said, “x”’) The function print() can take either a string or
a number (integer) as argument
#comments A comment will be ingored by the computer
\n New line
\t Tab
my_dna = “ATGC” Assigning a string (“ATGC”) to a variable
(my_dna) = Giving a name to something
print(my_dna) Output: ATGC
print(“my_dna”) Output: my_dna
Variable name are arbitrary Rules:
- Only letters, numbers and underscore
- Can not start with a number
- Con not use a word that is built in to
the Python language, eg. print
- Case sensitive
+ Concatenating string
My_dna = “AATT” + “GGCC”
Print (My_dna) Output: AATTGGCC
Upstream = “AAAA”
My_dna = upstream + “ATGC”
Print(My_dna) Output: AAAAATGC
The result of concatenating strings is itself a
string
len(“x”) Length of a sequence
dna_length = len(“AGTC”) (of
len(“varable”) Output: 4
print(dna_length) The return value of len() is a number (an
integer)
You can not use strings & integers mixed up in the same command: functions str() and
int()
str() String (argument)
Vb. print(“AT content is “ + Output: AT content is 4
str(at_content))
at_content is a number Integer to string: when you need to
concatenate or format numbers as part of a
string
int() Integer (number)
Vb. int(‘4’) Int(expression) als expression een getal is
Vb. number = 3 + int(dna_length)
Print(number) Output: 9 (if dna_length = len(“AATCGA’) = 6)
, String to integer: when you receive numerical
input as a string and need to perform
numerical operations
Only usable with variables that are strings – methods that belong to the string type
x.lower() Lowercase
print(my_dna.lower()) Doesn’t change the variable, they
return an copy
x.upper() Uppercase
print(my_dna.upper()) Doesn’t change the variable, they
return an copy
x.replace(“x”,”y”) Replacing x by y
print(protein.replace(“v”,”y”) It returns a copy of the variable where
all occurrences of the first string are
replaced by the second string
Extracting part of a string (taking a substring)
x[1:6] Number 1 until 5 (incl. 1, excl. 6)
- Position starts at zero
- Inclusive start
- Exclusive stop
x[0:] From begin to end
Counting substring
x.count(‘y’) In take 1 argument (the substring), it returns
the number of occurrences of the substring in
the string
Vb. my_dna.count(‘X’) We will count how many times X is in the
variable ‘my_dna’
x.find(‘y’) Finding substrings
It takes 1 argument (the substring), it returns
the position of the first occurrence of the
substring in the string
vb. protein.find(‘p’) We will find the position where ‘p’ stands the
first time in the variable ‘protein’
- Of the output is -1: ‘p’ is not in protein
- The positions start counting at zero
Order of count() and find()
My_dna.count(“A”) ≠ A.count(my_dna)
Working with files
open(“x.txt”) Opening a file
- Argument = string
Vb. data_file = open(“data_2024.csv”)
NOT: my_file_name = “dna.txt”
My_contancts = #fout: read() werkt niet op strings
my_file_name.read()
NOT: print(data_file) #fout: print het bestandobject, niet de inhoud
x.read() Read the contents of the file
- x = file objects (not name of variable)
dna_file = open(“dna.txt”)
my_dna = dna_file.read()
x.rstrip(“y”) To remove a character
- my_file_contents = my_file.read()
my_dna = my_file_contents.rstrip(“\n”)
1. Go to jupyter notebook log in
a. String = sequence of characters
b. Error: SyntaxError (eg. No “”) & NameError (eg. prin > print)
Print(‘x’) Printing a message to the screen
Print(“x”) x = argument
Print(“he said, ‘x’”)
Print(‘he said, “x”’) The function print() can take either a string or
a number (integer) as argument
#comments A comment will be ingored by the computer
\n New line
\t Tab
my_dna = “ATGC” Assigning a string (“ATGC”) to a variable
(my_dna) = Giving a name to something
print(my_dna) Output: ATGC
print(“my_dna”) Output: my_dna
Variable name are arbitrary Rules:
- Only letters, numbers and underscore
- Can not start with a number
- Con not use a word that is built in to
the Python language, eg. print
- Case sensitive
+ Concatenating string
My_dna = “AATT” + “GGCC”
Print (My_dna) Output: AATTGGCC
Upstream = “AAAA”
My_dna = upstream + “ATGC”
Print(My_dna) Output: AAAAATGC
The result of concatenating strings is itself a
string
len(“x”) Length of a sequence
dna_length = len(“AGTC”) (of
len(“varable”) Output: 4
print(dna_length) The return value of len() is a number (an
integer)
You can not use strings & integers mixed up in the same command: functions str() and
int()
str() String (argument)
Vb. print(“AT content is “ + Output: AT content is 4
str(at_content))
at_content is a number Integer to string: when you need to
concatenate or format numbers as part of a
string
int() Integer (number)
Vb. int(‘4’) Int(expression) als expression een getal is
Vb. number = 3 + int(dna_length)
Print(number) Output: 9 (if dna_length = len(“AATCGA’) = 6)
, String to integer: when you receive numerical
input as a string and need to perform
numerical operations
Only usable with variables that are strings – methods that belong to the string type
x.lower() Lowercase
print(my_dna.lower()) Doesn’t change the variable, they
return an copy
x.upper() Uppercase
print(my_dna.upper()) Doesn’t change the variable, they
return an copy
x.replace(“x”,”y”) Replacing x by y
print(protein.replace(“v”,”y”) It returns a copy of the variable where
all occurrences of the first string are
replaced by the second string
Extracting part of a string (taking a substring)
x[1:6] Number 1 until 5 (incl. 1, excl. 6)
- Position starts at zero
- Inclusive start
- Exclusive stop
x[0:] From begin to end
Counting substring
x.count(‘y’) In take 1 argument (the substring), it returns
the number of occurrences of the substring in
the string
Vb. my_dna.count(‘X’) We will count how many times X is in the
variable ‘my_dna’
x.find(‘y’) Finding substrings
It takes 1 argument (the substring), it returns
the position of the first occurrence of the
substring in the string
vb. protein.find(‘p’) We will find the position where ‘p’ stands the
first time in the variable ‘protein’
- Of the output is -1: ‘p’ is not in protein
- The positions start counting at zero
Order of count() and find()
My_dna.count(“A”) ≠ A.count(my_dna)
Working with files
open(“x.txt”) Opening a file
- Argument = string
Vb. data_file = open(“data_2024.csv”)
NOT: my_file_name = “dna.txt”
My_contancts = #fout: read() werkt niet op strings
my_file_name.read()
NOT: print(data_file) #fout: print het bestandobject, niet de inhoud
x.read() Read the contents of the file
- x = file objects (not name of variable)
dna_file = open(“dna.txt”)
my_dna = dna_file.read()
x.rstrip(“y”) To remove a character
- my_file_contents = my_file.read()
my_dna = my_file_contents.rstrip(“\n”)