Python: Level 1 (Basics)

For complete beginners with no coding background. We start from what Python even is, then build every core idea (data types, decisions, loops, and functions) one small step at a time, with pictures.

🟢 Beginner ⏱️ ~1.5 hr 🐍 Python

Start here, even if you have never written a line of code

This lesson assumes nothing. We will explain what Python even is, then build up every core idea (data, decisions, repetition, and reusable recipes) one small step at a time, with pictures. By the end you will understand the heart of the language. Go slowly. You cannot break anything.

What is Python, and what is "code"?

Python is a programming language: a set of words and rules for writing instructions that a computer can follow. Code is just those instructions written as plain text. You write the steps, Python reads them top to bottom, and the computer carries them out and shows you the result.

You write code instructions, as text Python reads it line by line, in order You see a result on the screen
Programming in one picture: code in, result out.

The simplest instruction is print, which means "show this on the screen." Anything after a # is a comment: a note for humans that Python ignores.

print("Hello, bioinformatics!")   # this line prints a message
# this whole line is a comment, Python skips it
Try it: run this, then change the text inside the quotes and run it again. That loop (change something, run, see what happens) is how you will learn everything here.

1. Variables: named boxes that hold data

A variable is a name you give to a piece of data so you can use it later. Think of it as a labelled box. The equals sign = means "put the value on the right into the box named on the left."

dna = "ATGC" "ATGC" the box is named dna
A variable is a labelled box. Here the box dna holds the text "ATGC".
dna = "ATGC"        # put the text "ATGC" into a box called dna
print(dna)            # show what is in the box  ->  ATGC

Naming rules, kept simple

A name can use letters, numbers, and underscores, but cannot start with a number or contain spaces. Pick names that describe the data: dna, gene_count, gc_percent. Good names make code read almost like English.

2. The core data types (the heart of the language)

Every piece of data in Python has a type: what kind of thing it is. There are six you must know. Here they are in plain English. Do not memorize this table; just know it is here to come back to.

TypeWhat it isExample
str (string)Text: letters, words, a DNA sequence. Always written in quotes."ATGC"
int (integer)A whole number, with no decimal point.42
floatA number with a decimal point.66.7
bool (boolean)A yes/no value: only True or False.True
listAn ordered collection of several values in one box.["TP53", "EGFR"]
dict (dictionary)A set of labelled pairs: each label (key) points to a value.{"A": 10, "G": 7}

You can ask Python the type of anything with type(). Run this:

2a. Text (strings)

A string is text in quotes. A DNA sequence is just a string. Python gives strings handy abilities: count letters, measure length, and pick out single characters by position.

dna = "ATGCGT"
len(dna)            # length: how many letters  ->  6
dna.count("G")      # how many G's  ->  2
dna[0]              # the FIRST letter  ->  A

That last line is the tricky one for everyone at first: Python counts positions starting from 0, not 1. Here is the picture:

A T G C G T 0 1 2 3 4 5
Each letter has a position (in purple). Counting starts at 0, so dna[0] is "A" and dna[2] is "G".

2b. Numbers (int and float) and the maths operators

Whole numbers are int; numbers with a decimal point are float. Python does maths with familiar symbols, called operators.

OperatorMeaningExample
+ - *add, subtract, multiply3 * 4 → 12
/divide (gives a float)9 / 2 → 4.5
**power (to the exponent)2 ** 3 → 8
%remainder after dividing7 % 3 → 1

A real example: GC content is the fraction of a sequence that is G or C, as a percentage.

2c. Booleans (True/False) and comparisons

A boolean is a yes/no value: True or False. You usually get one by comparing two things. These comparisons are the questions your code asks.

ComparisonAsks
==are they equal? (note: two equals signs)
!=are they different?
>   <greater than, less than
>=   <=greater-or-equal, less-or-equal

One equals or two?

= (one) stores a value in a variable. == (two) asks "are these equal?" and answers True or False. Mixing them up is the most common early mistake, and now you know the difference.

3. Making decisions: if, elif, else

Comparisons become useful when code acts on the answer. An if statement runs a block only when its question is True. elif ("else if") and else handle the other cases. The indentation (the spaces at the start of a line) is how Python knows which lines belong inside.

gc = 66.7

if gc > 60:
    print("GC-rich")
elif gc < 40:
    print("AT-rich")
else:
    print("balanced")

4. Lists: many values in one box

A list holds several values in order, inside square brackets. Like strings, list items have positions starting at 0.

"TP53" "BRCA1" "EGFR" "MYC" 0 1 2 3
A list of gene names. genes[1] is "BRCA1".
genes = ["TP53", "BRCA1", "EGFR", "MYC"]
genes[1]            # "BRCA1"
len(genes)          # 4
genes.append("KRAS")  # add one to the end

5. Dictionaries: labelled lookups

A dictionary stores pairs: each key (a label) points to a value. It is perfect for "how many of each?" Instead of a position, you look things up by their label.

"A" 10 "C" 7 "G" 9 key → value
Each key (a base) points to its value (a count). You look up counts["G"] to get 9.
counts = {"A": 10, "C": 7, "G": 9, "T": 12}
counts["G"]          # look up G's count  ->  9
counts["N"] = 2        # add a new pair

6. Loops: do something to every item

A loop repeats an instruction for each item in a collection, so you write it once and Python applies it to everything. This is the moment programming starts saving you real time.

for gene in genes:
    print(gene, "has", len(gene), "letters")

You can loop over the letters of a sequence too. Here we count each base by hand:

Why loops matter

A genome has millions of bases. You will never process them by hand. You write the instruction once, and the loop applies it to everything, whether that is 8 letters or 8 million.

7. Functions: your own reusable recipes

A function is a named recipe: you define the steps once, then reuse it with different inputs. def starts the recipe, and return hands back the answer.

This is the shape of real code

Reading data into variables, deciding with if, repeating with a loop, and wrapping reusable steps in a function: that is the core of Python, and of almost every bioinformatics script you will ever read.

8. Reading a file (a first look)

Real data lives in files. The pattern for reading one, line by line, is short. You will use proper tools (like Biopython) for real sequence files later, but this is the shape of it.

with open("sequence.txt") as f:
    for line in f:
        line = line.strip()      # remove the invisible newline
        print(line)

Every key word, in plain English

variable
a named box that holds a piece of data.
string (str)
text, written in quotes.
integer (int)
a whole number.
float
a number with a decimal point.
boolean (bool)
a True or False value.
list
an ordered collection of values in square brackets.
dictionary (dict)
labelled key-to-value pairs in curly braces.
index
the position of an item, counting from 0.
operator
a symbol that does something, like + or ==.
comparison
a question like "is this bigger?" that answers True or False.
if / elif / else
run different code depending on a True/False question.
loop
repeat an instruction for every item in a collection.
function
a named, reusable recipe defined with def.
comment
a note after # that Python ignores.

Check your understanding

Right. A dictionary maps each key (a base) to a value (its count), which is exactly the labelled-lookup job dictionaries are for.
Right. += needs the key to already hold a number. counts.get(base, 0) returns 0 when the base has not been seen yet, so the very first count works.
Right. A variable is a name attached to a value, like the box dna holding "ATGC".
Correct. Text, including a DNA sequence, is a string: characters inside quotes.
Exactly. Indexing starts at 0, so position 0 is the first item, position 1 the second, and so on.
Correct. One equals sign assigns; two equals signs compare and give True or False.
Right. A function lets you write the logic once and reuse it, which keeps code short and clear.
Next in the Python series, the payoff

Project: analyze a real DNA sequence →

Ready for more? Python Level 2 (Intermediate) builds straight on this.