Do you like this page?

Views of this page

Views of the site

Please help share my site. Thank you!

Python 3 Basic Grammar for Beginners - Easy Microbial Genomics

Python 3 Basic Grammar for Beginners

Last updated: April 23, 2022

This session will talk about basic grammar of Python 3. You can practice the example codes using iPython. iPython is a wonderful interactive interface to run codes and check results. You can install it by 'conda install ipython'. Then run 'ipython' to open the interface. Type codes and press return key to run it. Press upward arrow to rerun previous codes. Run 'history' to find history codes.

❤ The secret to learn Python well is to use it and ask Google when you have questions.

Data types

Like other programing languages, there are multiple different data types in Python: integer, float, string, byte, bool, list, tuple, set and dictionary. One should pay extra attention to data types because using wrong data type will lead to errors returned by Python. For example, Python will complain when you join a string and an integer. ❤ Personally speaking, type error is the easiest one for me to make.


Integers are whole numbers. For example, 0 and 1.

Input: 1+2
Output:> 3


Floats are fractional numbers. For example, 0.3 and 1.0. Adding an integer and a float up will result in a float.

> 2.0


A string is a series of characters . For example, 'abc' and '1'. You need to use a set of quotes ('' or “”) to define a string.

> '12'

Note that the result is a string. Never join a string and an integer. For example, 1+'1' will get an error.


Bytes contain raw data of a sequence of octets, whereas strings are Unicode sequences. Bytes are defined with the function 'b' or 'encode'.

> b'123'

> b'123'


Bool includes only two values: True or False. They are not strings or bytes. They are just bools to state right or wrong.

1 > 0
> True


A list includes zero, one or more elements. It is define with a '[]'. The elements can be any data type. Elements can be duplicated. For example, this list includes all the data types.

[1, 1.0, '1', b'1', 1>1, [1], (1,1), {1}, {1: 1}]
> [1, 1.0, '1', b'1', False, [1], (1, 1), {1}, {1: 1}]


Tuples are the same as lists, except that the elements cannot be changed (immutable). They are defined with a '()'. For example:

('a', 1)
> ('a', 1)


A set is a set of ordered non-duplicate elements. It is defined with a '{}'. The elements should be hashable, meaning that their elements cannot be changed. For example, the elements of 'abc' and ('a', 'b', 'c') cannot be changed, while those of ['a', 'b', 'c'] and {'a', 'b', 'c'} can. Hashable data types include integer, float, string, byte, bool and tuple, but not list, set and dictionary.

{1, 1.0, '1', b'1', 1>1, (1,1)}
> {(1, 1), 1, '1', False, b'1'}

👏 Set is useful to order elements and remove duplicates. For example:

{2, 2, 1}
> {1, 2}


A dictionary is similar to a hash in Perl. It includes zero, one or more elements, and each element includes a key and a value. Keys cannot be duplicated, but values can. Keys should be hashable. For example:

{'a':1, 'b':2}
> {'a': 1, 'b': 2}

To summarize, there are generally nine basic data types, including integer, float, string, byte, bool, list, tuple, set and dictionary. Among them, set, list and dictionary are non-hashable and the rest are hashable. In any case, you can use the function 'type' to check the data type of a variable. For example:

> bool

🏋 Homework: Try to check other data types.

Data type conversions

Conversions among simple data: integer, float, string, byte and bool

Data types can be converted to each other. All data types can be converted into strings with the function 'str'. Floats and integers can be converted into each other. Strings can be converted into integers. All data types can be converted into a bool. For example:

> 'True'

> 1

> 64

> False

🏋 Homework: Try to explore more of these data type conversions.

Conversions among complex data: list, tuple, set and dictionary

Lists can turn to tuples, and vice versa. There are many conversions among these types. For example:

> (1, 1)

> {1}

> [(1, 1)]

🏋 Homework: Try to explore more of these data type conversions.

Variable assignment

Variables are containers to store values. Variables can be named with numbers, letters and underscores ('_'). They cannot start with numbers. A variable is assigned a value with a '='.

a = 1 + 1
> 2

You can assign a value to multiple variables.

a = b = c = 1

You can conduct multiple assignments in one line.

a, b, c = 1, 2, 3

You can reset a variable.

a = None

You can also delete a variable.


Sequence indexing and slicing

In Python, some data types are sequential, meaning that their elements are ordered in a certain way. For example, a string has some characters in order. Sequential data can contain zero, one or more elements. Use the function 'len' to check their element numbers (e.g. len('abc')).


Each element of a sequential data has its own index. Like many other language, index in Python starts from 0. s[0] is the first element of the variable 's'.

Get the first element of a string

> '0'

Get the first element of an integer

> TypeError: 'int' object is not subscriptable

Oops! Integer is not a sequential data.

Get the last element of a list

> 3

Get the second last element of a tuple

> 2

Delete an element and redefine the index

l1 = [1,2,3]
> [2, 3]
l1[0] = 1
> [1, 3]

🏋 Homework: Try to find out all the sequential ones among the nine basic data types in Python. You can try to call an index to figure it out, just like what I have tried on the integer above.


Sequential data (including string, byte, list and tuple) can be cut into subsets, like slicing a sausage. Indexes are used to define the start and end.

Get the subset from the second to the fourth element of a string

> '123'

Note that slice is upper-bound exclusive, meaning that the element corresponding to the ending index will not be included. In the example above, the element '4' with the index 4 did not appear in the output slice.

Get the subset from the second to the fourth element of a string with a step of 2

> '13'

A missing index means the start or the end

> '0123'

> '01234'

👏 A trick: reverse the string

> '43210'

Delete a subset of a list

l1 = list('01234')
> ['3', '4']

Assign elements of a list with slicing

l1 = list('012')
l1[2:4] = ['a', 'b']
> ['0', '1', 'a', 'b']

🏋 Homework: Try to convert a string into a list and then reverse it with slicing

Read and write files

You can read and write a plain text file using the function open, read and write.

infile = open('1.txt', 'r')

This will read a file and show the first line of the file.

Put the content of a file in a string

st = open('1.txt', 'r').read()

Put the content of a file in a list

li = open('1.txt', 'r').readlines()

Open a file to write

outfile = open('1.txt', 'w')

This will overwrite the file if it is pre-existing.

Add content to a pre-existing file rather than overwrite it

outfile = open('1.txt', 'a')

Write something in the file and then close the file handle

outfile.write('Hello World!')

Module import

In Python, some basic functions are loaded by default, like 'print', 'len' and 'max', but other customed functions and classes need to be imported before using them. Use 'import' to load the corresponding modules and then call them.

Get the current working directory using 'os'

import os

You can import a module as an abbreviated name if you feel the full name is too long or there is a name conflict

import pandas as pd

You can import a class from a module. For example, parse a FASTA sequence file using SeqIO

from Bio import SeqIO
parse = SeqIO.parse('1.fas', 'fasta')

Install the package 'biopython' first by 'pip install biopython' or 'conda install -c conda-forge biopython'.

Errors and exceptions

If you run an incorrect code, Python will return an error report because it cannot interpret the code. There are a bunch of bult-in error types. Here are some very common ones.

You can also build your own errors and exceptions for debug purpose.

    [some code]
    [error report]

If the code(s) in the second line ran incorrectly, Python will run the code(s) in the fourth line, else Python will ignore it. This is useful to find out which line(s) of your program went wrong.

Boolean logic and conditional statement: if

As we have mentioned, bools are logic statements including True an False. Multiple bools can be combined with 'and' or 'or'.

a, b = True, False
a and b
> False
a or b
> True

To reverse a bool, use 'not'

not a
> False

A conditional statement determines a logical condition and then execute a statement accordingly. It uses 'if', 'elif' (else if) and 'else' to determine the conditions.

if 2 < 1:
    print('2 < 1')
elif 2 > 3:
    print('2 > 3')
    print('1 < 2 < 3')
> '1 < 2 < 3'

Note that the logical conditions should end with a ':' and the statement blocks should be indented (e.g. four spaces). A statement block will be executed only if the condition is true. When a statement is executed, the rest statements will be ignored.

Conditional loop statement: while

Conditional loop statement is different from a typical 'if' conditional statement. A loop means repeating doing something, until the condition is False, or the statement asks to stop. A conditional loop statement typically uses 'while' to lead the condition.

Print the integers from 1 to 9

n = 1
while n < 10:
    n += 1

Python has three statements to control a loop: 'break', 'continue' and 'pass'. 'break' terminates the loop where it is located and the codes behind the loop will keep running. 'continue' will not terminate the loop, but will ignore the codes behind itself in the same loop. 'pass' does nothing to the loop.

n = 1
while True:
    if n > 9:
    n += 1

Here, the 'break' terminated the loop, and ran the code after the loop if any.

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:

Here the 'continue' ignored the 'print(n)' when it was an even number but did not terminate the loop.

Here is a useful application of the 'while' loop. Check the time every five seconds and send a reminder by email at 13:00 every day. Keep doing it for 10 days.

import time
from datetime import datetime
n = 0
while True:
    now = str(
    if '13:00:05' > now > '13:00':
        send_email() # define it somewhere
        n += 1
    if n < 10:

It should be checking the time. Press Ctrl + C to exit the loop if you don't want to wait hours to check.

Iterative loop statement: for

Iterative loop statement is similar to the conditional loop statement, but it iterates all the elements of an iterable container, like range, string, byte, list, tuple, set and dictionary. It also has the control of 'break', 'continue' and 'pass'.

for i in {1, 2, 3}:

Time for fun!

❤ Now let's have some fun.

Here is a small game to play. It will set a random number between 1 and 20, and you have 10 times to guess what it is. It will remind you if your guess is too big or small each time.

import random
r = random.randint(1,20)
print('A random number between 1 and 20 has been set. Can you guess what it is?')
n = 0
while True:
   print('Please guess a number: ')
   i = int(input())
   n += 1
   if i > r:
      print('Oops, too big. You have '+str(10 - n)+' tries left.')
   elif i < r:
      print('Aha, too small. '+str(10 - n)+' tries left.')
      print('You are genius! Yes the number is '+str(i)+'. You tried '+str(n)+' times.')
   if n > 9:
      print('You are out. The number is '+str(r))

This is my output.

A random number between 1 and 20 has been set. Can you guess what it is?
Please guess a number:
Oops, too big. You have 9 tries left.
Please guess a number:
Oops, too big. You have 8 tries left.
Please guess a number:
Oops, too big. You have 7 tries left.
Please guess a number:
Oops, too big. You have 6 tries left.
Please guess a number:
You are genius! Yes the number is 1. You tried 5 times.

👏 What is your best try?