Exercise 001¶
Variables and Printing¶
Store the string Hello World
in a variable with name first_variable
and print it.
first_variable = "Hello World"
For-Loops¶
Count from 1
to 10
by using a for loop, store each value in a variable value and print it. Use the range-function to help you.
Tip: Typically programming languages start by counting from zero. Make sure that the range-function starts correctly.
for number in range(0, 11):
print(number)
0 1 2 3 4 5 6 7 8 9 10
While-Loops¶
Repeat the previous task using a while-loop.
Tip: While-Loops are closed by a condition, so make sure that the loop is stopped at the end.
number = 0
while number <= 10:
print(number)
number += 1
0 1 2 3 4 5 6 7 8 9 10
Conditional statements¶
Count from 1
to 10
using a for-loop, store each value in variable value and distinguish whether a number is odd or even. Print each result and modify the print to display whether the number is odd or even as well as the number itself:
Example:
print("odd", 1)
Tip: Use the modulo-function using the %
-operator. It behaves similar to any other operator and returns the remainder of the division.
5 % 5 = 0
for number in range(0, 11):
if number % 2 == 0:
print("even", number)
else:
print("odd", number)
even 0 odd 1 even 2 odd 3 even 4 odd 5 even 6 odd 7 even 8 odd 9 even 10
Lists #1¶
Store the numbers 1
to 5
in a list under the variable my_list
and print it. Finally, add the first and last element of the list and print the result.
my_list = []
for number in range(1, 11):
my_list.append(number)
print("Addition of first and last: ", my_list[0] + my_list[-1])
Addition of first and last: 11
Lists #2¶
Store the number 0
to 49
in a list under variabl my_list
. Now sort the list in two new lists even
and odd
, where each list contains even and odd number from my_list
respectively. Finally, print both lists.
Tip: So called list comprehensions unify the concept of a list and a for-loop into a single line. In addtion, you can add conditions that help only to include those value in a list, that meet the conditions.
# Using list comprehension
numbers = [number for number in range(0, 50)]
even, odd = [], []
for number in numbers:
if number % 2 == 0:
even.append(number)
else:
odd.append(number)
print("Even numbers: ", even)
print("Odd numbers: ", odd)
Even numbers: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48] Odd numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
Dictionaries¶
Store both of the previous lists even
and odd
in a dictionary by mapping each list using the keys even
and odd
. In addition, add a new list which contains all numbers in my_list
that are divisible by 3
using the dictionary key by_three
. Finally, print the dicitonary to inspect its content.
import rich # This just for an overview, you can ignore it
division_dict = {
"odd": odd,
"even": even,
"by_three": [number for number in numbers if number % 3 == 0]
}
rich.print("Resulting dictionary: ", division_dict)
Resulting dictionary: { 'odd': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49], 'even': [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48], 'by_three': [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48] }
Reading and writing of files (Optional)¶
Write out all numbers found in even
to a file called even.txt
. Next, read the same file and save the result in a new list called new_list
. Finally, add the first and third entry of the list. Does it work? If not, can you find an explanation?
Tip: When writing to a file, Python does exactly what its told. So make sure to include newlines \n
after writing each number. Otherwise, reading it correctly will be impossible.
Tip: Newlines \n
will also be read and can be removed upon parsing of a line by using line.strip()
, where line is the string on which you'd like to remove the newline.
Tip: Using the open-function facilitates writing and reading of a file. There are ways to shorten your work. See the following examples:
# Write to a file
with open("example.txt", "w") as file:
for value in my_list:
file.write("Example")
# Read from a file
example = [ line.strip() for line in open("example.txt", "r").readlines() ]
# Writing the file
with open("even.txt", "w") as f:
for number in even:
f.write(f"{number}\n")
# Reading the file (using a list comprehension)
my_list = [line.strip() for line in open("even.txt").readlines()]
print("Addition of first and last: ", my_list[0] + my_list[-1])
print("Entry types: ", type(my_list[0]))
# We need to modify our code!
my_list = [
int(line.strip()) # Type-cast to an integer
for line in open("even.txt").readlines()
]
print("\nType casted - Addition of first and last: ", my_list[0] + my_list[-1])
print("Entry types: ", type(my_list[0]))
Addition of first and last: 048 Entry types: <class 'str'> Type casted - Addition of first and last: 48 Entry types: <class 'int'>