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: Working with the Python interpreter
-
Start by typing the following command into the Python interpreter:
-
Enter now
i = 10
in the Python-interpreter and then (in a new line)print(i)
. After that (in a new line) enterj = i/2
and (in a new line)print(j)
. -
Assign to variable
7assignment
the string"black magic"
. Don’t forget to put the string in quotation marks (" "). -
Assign to variable
A
a sequence AGCTA (don’t forget to put the sequence in quotation marks). Use the built-in functionlen()
to determine the length of the sequenceA
and assign the length ofA
to variablei
. PrintA
andi
. -
Concatenate
A
andi
and print the result. -
Now enter
print(A + str(i))
.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()
. -
Print the substring of A from position 2 to 4. The output should be: GCT.
-
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.
-
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.
Execute the same for-loop a second time and print out the character at each position of string A using A[i] as well.
-
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. -
Write a while-loop, which produces the same output as the for-loop and if-condition together.
-
Print the variable A again.
-
Leave the interactive mode of Python with
quit()
. -
Now return to the interactive mode of Python and print the variable A.
Assignment 2: Write a small program
-
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.
-
Within the program assign different numbers to i and j, e.g.:
- i = 3 and j = 4 and
- 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 Exercises¶
Assignment 3 (Bonus): 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
-
Chose two variables, e.g. A and B and assign the sequences
GATTACA
andTACCATAC
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
-
Now extend the program: Concatenate both sequences in both ways (AB and BA) and print both options.
-
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.pyprint("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:])
-
Print out the second sequence from the last to the first position (last position first, first position last).
-
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:
- More bonus exercises: https://hackmd.io/@pmitev/UPPMAX-intro-Python-assignments1