gene_names = ["TP53", "BRCA1", "MDM2"]
print(gene_names[0])
print(gene_names[1])
print(gene_names[2])TP53
BRCA1
MDM2
gene_names = ["TP53", "BRCA1", "MDM2"]
print(gene_names[0])
print(gene_names[1])
print(gene_names[2])TP53
BRCA1
MDM2
bases = ["A", "T", "G", "C", "G", "C"]
first_three = bases[:3]
last_two = bases[-2:]
print(first_three)
print(last_two)['A', 'T', 'G']
['G', 'C']
genes = ["TP53", "BRCA1"]
genes.append("ATM")
genes.remove("BRCA1")
print(genes)['TP53', 'ATM']
gene_location = (2_345_678, 2_346_987)
print(gene_location)(2345678, 2346987)
gene_functions = {"TP53": "tumor suppression", "BRCA1": "DNA repair"}
print(gene_functions["TP53"])tumor suppression
gene_functions = {"TP53": "tumor suppression", "BRCA1": "DNA repair"}
result = gene_functions.get("MDM2", "Unknown")
print(result)Unknown
expression = [2.3, 3.1, 1.9]
for value in expression:
print(value)2.3
3.1
1.9
sequence = "AAGCTTAA"
count = 0
for base in sequence:
if base == "A":
count += 1
print(count)4
genes = ["TP53", "BRCA1", "MDM2", "ATM"]
genes_with_A = []
for gene in genes:
if "A" in gene:
genes_with_A.append(gene)
print(genes_with_A)['BRCA1', 'ATM']
genes = ["TP53", "BRCA1", "MDM2"]
if "BRCA1" in genes:
print("Found")
else:
print("Not found")Found
samples = [
["Sample1", [1.1, 1.2, 1.3]],
["Sample2", [2.1, 2.2, 2.3]],
["Sample3", [3.1, 3.2, 3.3]],
]
print(samples[2][1][0])3.1
Here is one way that you could nested for loops with enumerate to print the values and their indices:
for sample_index, sample_data in enumerate(samples):
sample_name, observations = sample_data
for observation_index, observation in enumerate(observations):
print(f"{sample_index}: {sample_name}; {observation_index}: {observation}")0: Sample1; 0: 1.1
0: Sample1; 1: 1.2
0: Sample1; 2: 1.3
1: Sample2; 0: 2.1
1: Sample2; 1: 2.2
1: Sample2; 2: 2.3
2: Sample3; 0: 3.1
2: Sample3; 1: 3.2
2: Sample3; 2: 3.3
gene_ids = ["nrdA", "nrdJ", "nrdD"]
products = ["Class I RNR", "Class II RNR", "Class III RNR"]
gene_products = {}
for gene_id, product in zip(gene_ids, products):
gene_products[gene_id] = product
print(gene_products){'nrdA': 'Class I RNR', 'nrdJ': 'Class II RNR', 'nrdD': 'Class III RNR'}
dna = "ATGCTTAC"
rna = ["U" if base == "T" else base for base in dna]
print(rna)['A', 'U', 'G', 'C', 'U', 'U', 'A', 'C']
from collections import defaultdict
sequence = "AAGCGAAGCT"
counts = defaultdict(int)
for base in sequence:
counts[base] += 1
for base, count in counts.items():
print(f"{base} => {count}")A => 4
G => 3
C => 2
T => 1
gene_info = {
"nrdA": {"product": "Class I RNR", "location": {"start": 1023, "end": 3268}}
}
start = gene_info["nrdA"]["location"]["start"]
end = gene_info["nrdA"]["location"]["end"]
print(f"{start=}")
print(f"{end=}")start=1023
end=3268
Let’s add another gene to the dictionary and loop through it.
gene_info["new_gene"] = {
"product": "Some Protein",
"location": {"start": 5000, "end": 8000},
}
for gene_id, gene_data in gene_info.items():
start = gene_data["location"]["start"]
end = gene_data["location"]["end"]
print(f"Gene={gene_id}; {start=}; {end=}")Gene=nrdA; start=1023; end=3268
Gene=new_gene; start=5000; end=8000
complement = {"A": "T", "T": "A", "G": "C", "C": "G"}
sequence = "AAAACCCGGT"
rev_comp = ""
for base in reversed(sequence):
rev_comp += complement[base]
print(rev_comp)ACCGGGTTTT
BACTERIA_COUNT = 10
bacteria = [5] * BACTERIA_COUNT
for turn in range(10):
print(f"Turn {turn}: {bacteria}")
# Check if this is an even turn
is_even_turn = turn % 2 == 0
for i in range(BACTERIA_COUNT):
# Check if this bacteria is at an even index
is_even_index = i % 2 == 0
# Even turns: even-indexed bacteria grow
if is_even_turn and is_even_index:
bacteria[i] += 1
# Odd turns: odd-indexed bacteria shrink
elif not is_even_turn and not is_even_index:
bacteria[i] -= 1
print(f"At the end: {bacteria}")Turn 0: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
Turn 1: [6, 5, 6, 5, 6, 5, 6, 5, 6, 5]
Turn 2: [6, 4, 6, 4, 6, 4, 6, 4, 6, 4]
Turn 3: [7, 4, 7, 4, 7, 4, 7, 4, 7, 4]
Turn 4: [7, 3, 7, 3, 7, 3, 7, 3, 7, 3]
Turn 5: [8, 3, 8, 3, 8, 3, 8, 3, 8, 3]
Turn 6: [8, 2, 8, 2, 8, 2, 8, 2, 8, 2]
Turn 7: [9, 2, 9, 2, 9, 2, 9, 2, 9, 2]
Turn 8: [9, 1, 9, 1, 9, 1, 9, 1, 9, 1]
Turn 9: [10, 1, 10, 1, 10, 1, 10, 1, 10, 1]
At the end: [10, 0, 10, 0, 10, 0, 10, 0, 10, 0]