Skip to content

Exercises

For the first assignment of this exercise session you should get familiar with the Python-interpreter. In the second assignment you will write your first Python program using an editor. More instructions are given in the following how to use both, the Python-interpreter and an editor. Good luck!

Assignment 1

Assignment 1.1

Start by typing the following command into the Python interpreter:

print("Assignment 7")
What happens?
>>> print("Assignment7")
Assignment7

Assignment 1.2

Enter now i = 10 in the Python-interpreter and then (in a new line) print(i). After that (in a new line) enter j = i/2 and (in a new line) print(j).

What values are displayed and why?
>>> i = 10
>>> print(i)
10
>>> j = i/2
>>> print(j)
5.0

Assignment 1.3

Assign to variable 7assignment the string "black magic". Don't forget to put the string in quotation marks (" ").

What error occurs and why?
>>> 7Assignment = "black magic"
  File "<stdin>", line 1
    7Assignment = "black magic"
              ^
SyntaxError: invalid syntax

Assignment 1.4

Assign to variable A a sequence AGCTA (don't forget to put the sequence in quotation marks). Use the built-in function len() to determine the length of the sequence A and assign the length of A to variable i. Print A and i.

Output
>>> A = "AGCTA"
>>> i = len(A)
>>> print(A, i)
AGCTA 5

Assignment 1.5

Concatenate A and i and print the result.

What happens and why?
>>> print(A + i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

Assignment 1.6

Now enter print(A + str(i)).

What happens now?
>>> print(A + str(i))
AGCTA5

Hint: What might the built-in function str() do? There are also other built-in functions, e.g., to convert a string or number to an integer: int(), or to convert a string or number to a floating point: float().

Assignment 1.7

Print the substring of A from position 2 to 4. The output should be GCT.

Solution
>>> print(A[1:4])
GCT

Assignment 1.8

Print the prefix (beginning of a string) of length 2 and the suffix (end of a string) of length 2 of the sequence stored in A. The output should be AG and TA.

Solution
>>> print(A[:2])
AG
>>> print(A[-2:])
TA

Assignment 1.9

Write a for-loop with the loop variable i, which runs from 0 to len(A) and prints out i.

Hint: Don’t forget to indent the body of the for-loop.

??? tip "Solution"

    ```python
    >>> for i in range(len(A)):
    >>>     print(i)
    0
    1
    2
    3
    4
    ```

Execute the same for-loop a second time and print out the character at
each position of string A using A[i] as well.

Assignment 1.10

Now add an if-condition inside the for-loop, which checks if i < len(A)/2. Only print i and A[i] if this condition is true.

Solution
>>> for i in range(len(A)):
>>>     if (i < len(A)/2):
>>>         print(i, A[i])
0 A
1 G
2 C

Assignment 1.11

Write a while-loop, which produces the same output as the for-loop and if-condition together.

Solution
>>> i = 0
>>> while (i < len(A)/2):
>>>     print(i, A[i])
>>>     i=i+1
0 A
1 G
2 C

Assignment 1.12

Print the variable A again.

What happens?
>>> print(A)
AGCTA

Assignment 1.13

Leave the interactive mode of Python with quit().

Assignment 1.14

Now return to the interactive mode of Python and print the variable A.

What happens now and why?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined

Assignment 2: Write a small program

Assignment 2.1

Write a short program which compares two variables i and j. It should print the value 1, if i and j are equal, and otherwise the value 0.

Solution

Write the script compare.py using your favorite editor.

compare.py
i = 3
j = 4
if i == j:
    print(1)
else:
    print(0)

Then run it from the command line using:

$ python compare.py
0

Assignment 2.2

Within the program assign different numbers to i and j, e.g.:

- `i = 3` and `j = 4`
- `i = 10` and `j = 10`

Congratulations, you have now completed the basic python exercises for this session. If you were too quick or just want to try a bit harder exercises, please continue with the bonus exercises below.

(Bonus) Assignment 3: Sequences

In this exercise we write a short Python program (named `<program_name>.py`,
think of a reasonable program name and name your file accordingly. Replace
<program_name> with your new program name).

Assignment 3.1

Chose two variables, e.g. A and B and assign the sequences GATTACA and TACCATAC to these variables. Make sure that the two sequences are assigned as strings to their variables A and B. Then print these sequences. Save everything you wrote and close the editor. Then you can run your program: python3 <program_name>.py

Solution
sequence_operations.py
A = "GATTACA"
B = "TACCATAC"
print("sequence A: ", A)
print("sequence B: ", B)

Assignment 3.2

Now extend the program: Concatenate both sequences in both ways (AB and BA) and print both options.

Solution
sequence_operations.py
A = "GATTACA"
B = "TACCATAC"
print("sequence A + B: ", A + B)
print("sequence B + A: ", B + A)

Screen output:

sequence A + B:  GATTACATACCATAC
sequence B + A:  TACCATACGATTACA

Assignment 3.3

Print prefixes and suffixes of length 3 of both sequences A and B. Use the built-in function len() for determining the suffixes.

Solution
sequence_operations.py
print("prefix A: ", A[:3])
print("prefix B: ", B[:3])
suffix_A = len(A) - 3
suffix_B = len(B) - 3
print("suffix A: ", A[suffix_A:])
print("suffix B: ", B[suffix_B:])

# It is also possible to use a negative index
# to count from the end:
print("suffix A: ", A[-3:])
print("suffix B: ", B[-3:])

Screen output:

prefix A: GAT
prefix B: TAC
suffix A: ACA
suffix B: TAC

Assignment 3.4

Print out the second sequence from the last to the first position (last position first, first position last).

Solution
sequence_operations.py
# Alternative 1
for i in range(len(B)):
    print(B[len(B) - i - 1])

# Alternative 2 (reversed range)
for i in range(len(B), 0, -1)
    print(B[i-1])

# Alternative 3 (reversed string)
for c in reversed(B):
    print(c)

# Alternative 4 (reverse slicing)
for c in B[::-1]
    print(c)

Screen output:

C
A
T
A
C
C
A
T

Assignment 3.5

Assign this inverted sequence to a third variable, you could use the variable name C, and print the value of this variable.

More bonus exercises

Some extra bonus exercises have not been integrated into the webpage yet. You can find them here: