Exercises

Exercises - “Introduction to Python”

For the first assignment you should get familiar with the Python-interpreter. In the second assignment you 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!

 

Python - I

Load the module for Python 3 with the command module load python/3.9.5. Open the Python-interpreter with the command python3. You should then see at the beginning of the line: >>>. In this exercise we use only the Python-interpreter. You can leave the Python-interpreter when you type quit().

 

  1. Type in the Python-interpreter the following command:
print("Assignment7")

what happens?

  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?

  1. Assign to variable 7Assignment the string black magic. Don’t forget to put the string in quotation marks (" ").

What error occurs and why?

  1. 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.
  1. Concatenate A and i and print the result.

What happens and why?

  1. Enter now print(A + str(i)).

What happens now and why?

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().

  1. Print the substring of A from position 2 to 4. The output should be: GCT.
  2. 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.
  1. Write a for-loop with the loop variable i, which runs from 0 to len(A) and prints out i.

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

  1. Add now 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.
  2. Write a while-loop, which produces the same output as the for-loop and if-condition together.
  3. Print the variable A again. What happens?
  4. Leave the interactive mode of Python with quit().

  5. Now return to the interactive mode of Python and print the variable A.
    What happens now and why?

 

First small program :computer:

Open your favorite editor (nano, idle3, gedit, etc.) and write in the file named compare.py your first Python program.

Hint: When you type $ idle3 compare.py & in the terminal, a new line in the terminal should appear (if not press <ctrl C>). Then you can run your program in the same terminal window: $ python3 compare.py The advantage is that you can edit your program and switch easily between the editor and terminal window.

  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.
  2. Within the program assign different numbers to i and j, e.g.:
    a) i = 3 and j = 4 and
    b) i = 10 and j = 10

Does your program work?

 

Bonus Exercises

1. 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).

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

Then extend your program:
1.1 Concatenate both sequences in both ways (AB and BA) and print both options.

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

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

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

1.5 Print out the middle base of each sequence. When a sequence has an even number of bases, print out the base at the right position of the middle. Use the built-in function len() for this task.

For example: For A = "GTCA" the program should print out C.
Hint: There exist built-in functions to convert a number to an integer.

1.6 Count how often each base occurs in the first sequence (How often does G occur in the first sequence, then A, so on.) and print out this number for each base.

1.7 Count how often TA occurs in the second sequence and print out this number.

2. Calculate the product of two numbers

Write in an editor the program product.py as introduced in the lecture, which calculates the product of two numbers 456 and 15. Save the program code as product.py and run the program as described in the previous assignment.

2.1 Now calculate the product 234 and 24 additionally to the first product and print out both products (results) in one single line.

2.2 Change the program so that all numbers 456, 15, 234, and 24 are saved in one list l. Change the print statement so that each number gets printed and also the product of the first two and the last two numbers.

3. More sequences

Write in an editor a program, which has three lists lm, and n. Each list contains several sequences. Save and run the program as described previously.

l: AGGTC, GATC, CTGCA, ATTCGT, ATGGT, GATC
m: CTGCA, GATC
n: CUAGCUA, GTATGG, GUAUC, GTAG

Note: Remember to store all sequences as strings in each of the lists. Extend your program so that it can perform the following tasks.

3.1 Print each sequence in list l.

3.2 Print the first and last sequence in list l.

3.3 For each sequence in list l store the second position of each sequence in a new variable and print this new sequence.

3.4 Add this new sequence to the list l.

3.5 How long is list l now? Print out the length of list l.

3.6 Delete the second sequence of list l. Print list l and its length.

3.7 Divide the new list l into two equal parts and store the first half in a new list l1 and the second half in a new list l2. Print both lists.

3.8 Concatenate list l2 and list l1 (in this order) and store it in a new list l3.

3.9 Remove all sequences in list l, which are also present in list m

3.10 Invert all sequences in new list l and store them in a new list l4.

3.11 In list n a few RNA sequences (U instead of T) are present.

  • Change these sequences back to DNA sequences.
  • Delete the RNA sequences in list n.
  • Add the new DNA sequences at the same position of list n.

When you print list n it should contain the following sequences in this order:
CTAGCTA, GTATGG, GTATC, and GTAG.