= ["TP53", "BRCA1", "MDM2"]
gene_names print(gene_names[0])
print(gene_names[1])
print(gene_names[2])
TP53
BRCA1
MDM2
= ["TP53", "BRCA1", "MDM2"]
gene_names print(gene_names[0])
print(gene_names[1])
print(gene_names[2])
TP53
BRCA1
MDM2
= ["A", "T", "G", "C", "G", "C"]
bases
= bases[:3]
first_three = bases[-2:]
last_two
print(first_three)
print(last_two)
['A', 'T', 'G']
['G', 'C']
= ["TP53", "BRCA1"]
genes
"ATM")
genes.append("BRCA1")
genes.remove(
print(genes)
['TP53', 'ATM']
= (2_345_678, 2_346_987)
gene_location print(gene_location)
(2345678, 2346987)
= {"TP53": "tumor suppression", "BRCA1": "DNA repair"}
gene_functions print(gene_functions["TP53"])
tumor suppression
= {"TP53": "tumor suppression", "BRCA1": "DNA repair"}
gene_functions
= gene_functions.get("MDM2", "Unknown")
result
print(result)
Unknown
= [2.3, 3.1, 1.9]
expression
for value in expression:
print(value)
2.3
3.1
1.9
= "AAGCTTAA"
sequence = 0
count
for base in sequence:
if base == "A":
+= 1
count
print(count)
4
= ["TP53", "BRCA1", "MDM2", "ATM"]
genes = []
genes_with_A
for gene in genes:
if "A" in gene:
genes_with_A.append(gene)
print(genes_with_A)
['BRCA1', 'ATM']
= ["TP53", "BRCA1", "MDM2"]
genes
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_data
sample_name, observations 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
= ["nrdA", "nrdJ", "nrdD"]
gene_ids = ["Class I RNR", "Class II RNR", "Class III RNR"]
products
= {}
gene_products
for gene_id, product in zip(gene_ids, products):
= product
gene_products[gene_id]
print(gene_products)
{'nrdA': 'Class I RNR', 'nrdJ': 'Class II RNR', 'nrdD': 'Class III RNR'}
= "ATGCTTAC"
dna
= ["U" if base == "T" else base for base in dna]
rna
print(rna)
['A', 'U', 'G', 'C', 'U', 'U', 'A', 'C']
from collections import defaultdict
= "AAGCGAAGCT"
sequence = defaultdict(int)
counts
for base in sequence:
+= 1
counts[base]
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}}
}
= gene_info["nrdA"]["location"]["start"]
start = gene_info["nrdA"]["location"]["end"]
end
print(f"{start=}")
print(f"{end=}")
start=1023
end=3268
Let’s add another gene to the dictionary and loop through it.
"new_gene"] = {
gene_info["product": "Some Protein",
"location": {"start": 5000, "end": 8000},
}
for gene_id, gene_data in gene_info.items():
= gene_data["location"]["start"]
start = gene_data["location"]["end"]
end
print(f"Gene={gene_id}; {start=}; {end=}")
Gene=nrdA; start=1023; end=3268
Gene=new_gene; start=5000; end=8000
= {"A": "T", "T": "A", "G": "C", "C": "G"}
complement
= "AAAACCCGGT"
sequence = ""
rev_comp
for base in reversed(sequence):
+= complement[base]
rev_comp
print(rev_comp)
ACCGGGTTTT
= 10
BACTERIA_COUNT = [5] * BACTERIA_COUNT
bacteria
for turn in range(10):
print(f"Turn {turn}: {bacteria}")
# Check if this is an even turn
= turn % 2 == 0
is_even_turn
for i in range(BACTERIA_COUNT):
# Check if this bacteria is at an even index
= i % 2 == 0
is_even_index
# Even turns: even-indexed bacteria grow
if is_even_turn and is_even_index:
+= 1
bacteria[i] # Odd turns: odd-indexed bacteria shrink
elif not is_even_turn and not is_even_index:
-= 1
bacteria[i]
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]