Python check questions and some riddles

This is a list of small riddles to repeat the content of the course + some methods and modules we did not cover, so you might have to look up some documentation.

Try to forcast the output of the following snippets:

print(((7//2) ** 2) % 5, end=" ")
print("""abcDE""".upper()[1:-1].startswith("BCD"))
def something(x):
    if x > 3:
        return x
    elif x % 2 == 1:
        return 2 * x
    
print(something(5))
print(something(3))
print(something(0))
def funny(a, b=7, c=5):
    return a // b, c - a

x, y = funny(3)
print(x, y)
x, y = funny(c=3, a=2)
print(x, y)
def test(n):
    d = 2
    while d*d <= n:
        if n % d == 0:
            return True
        d += 1
    return False

print(test(9))
print(test(7))
import math
print(math.log(math.e))
print(float("123") * 3)
print(str(123) * 3)
text = """first line {2}
second line {1}
third line {0}""".format(0, 1, 2)
print(repr(text))
start = 1
for number in range(1, 6):
    if number % 2 == 0:
        start += number * number
print(start)
n = int(input("start value for collatz iteration: "))
while True:
    print(n, end=" ")
    if n == 1:
        break
    if n % 2 == 0:
        n = n // 2
        continue   # is this realy needed here ?
    else:
        n = 3 * n + 1
print()
ll = 0
for word in ["hi\n", "how ", "do ", "you", "do?"]:
    ll += len(word.rstrip())
print(ll)
result = ""
for c in "abcde":
    result = c + result
print(result)
print(list(range(1, 4)))
a = []
a.extend([1, 2, 3])
print(a[-1])
a.append([1, 2, 3])
print(a[-1])
print("124" in "1234")
print("de" not in "abcdef")
d = {1: 2, 3: {4: 5}, 4:[1, 2]}
print(d[4][-1])
print(d[3].get(2))
print(5 in d)
print(1 in d.keys())
print(len(d.values()))
for a, b in d.items():
    print(a + 1, b)
with open("table.txt", "w") as fh:
    for row in range(1, 5):
        for col in range(1, 5):
            print("{}".format(row * col), end=" ", file=fh)
        print(file=fh)
with open("table.txt", "r") as fh:
    sum_ = 0
    count = 0
    for line in fh:
        line = line.strip()
        cells = line.split(" ")
        for cell in cells:
            sum_ += int(cell)
            count += 1
print(sum_/count)
numbers = list(range(2, 5))
text = "abcdeABCDEFT"
result = ""
for (n, c) in zip(numbers, text):
    result += n * c
print(result)
# same as last one but faster for longer strings:
numbers = list(range(2, 5))
text = "abcdeABCDEFT"
result = []
for (n, c) in zip(numbers, text):
    result.append(n * c)
print("".join(result))
a, b, c = 1, 2, 3
b, c, a = a, b, c
print(a, b, c)
for i, c in enumerate("HELLLO!"):
    if i % 2 == 0:
        print(c, end="")
print()
words = "this is a python check question collection"
result = [word[0] for word in words.upper().split(" ") if len(word) <= 6]
print("-".join(result))
from collections import namedtuple

Address = namedtuple("Address", ["name", "street", "zip", "city"])

sis = Address("Scientific IT Services", "Weinbergstrasse 11", 8001, "Zurich")
print(sis.city)
import os
print(os.listdir("."))
import glob
print(glob.glob("*.txt"))
from statistics import mean, median
data = list(range(10))
data.extend([0, 0, 0, 0, 0])
print(mean(data), median(data))
from urllib import request
response = request.urlopen('https://www.python.org/static/img/python-logo@2x.png')
with open("python_logo.png", "wb") as fh:
    fh.write(response.read())