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:
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)
.
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?
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
.
Assignment 1.5¶
Concatenate A
and i
and print the result.
What happens and why?
Assignment 1.6¶
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()
.
Assignment 1.7¶
Print the substring of A from position 2 to 4. The output should be 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.
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.
Assignment 1.11¶
Write a while-loop, which produces the same output as the for-loop and if-condition together.
Assignment 1.12¶
Print the variable A
again.
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?
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.
Then run it from the command line using:
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
Assignment 3.2¶
Now extend the program: Concatenate both sequences in both ways (AB
and
BA
) and print both options.
Solution
A = "GATTACA"
B = "TACCATAC"
print("sequence A + B: ", A + B)
print("sequence B + A: ", B + A)
Screen output:
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
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:
Assignment 3.4¶
Print out the second sequence from the last to the first position (last position first, first position last).
Solution
# 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:
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:
- More bonus exercises: https://hackmd.io/@pmitev/UPPMAX-intro-Python-assignments1