In [ ]:
# print the squares of the numbers 1 to 10
i = 1
while i <= 10:
print(i**2)
i = i + 1
print("The loop has finished")
condition must be a boolean expression! The loop is executed while the condition evaluates to True.while-loop execution block. The block is merely indicated by identation!
This is the case for all Python control structures! Blocks are always indicated
by code-identation. All lines belonging to a block must be idented by the same
amount of spaces. The usual ident is four spaces (never use tabs).
In [ ]:
# your solution here
The square root $y=\sqrt{x}$ of a positive number $x$ can be estimated iteratively with $y_0>0$ (an arbitrary positive number) and $y_{n+1}=\frac 12\left(y_n+\frac{x}{y_n}\right)$.
Write a python program to estimate the square root with that recipe!
Hint: Construct a while-loop with the condition $|y_{n+1}-y_n|>\epsilon$ with $\epsilon=10^{-6}$ and update $y_n$ and $y_{n+1}$ within the loop. Consider the final $y_{n+1}$ as estimate for $\sqrt{x}$.
In [ ]:
# your solution here
In [ ]:
x = 6
if x < 10:
print("x is smaller than 10!")
if x % 2 == 0:
print("x is even!")
else:
print("x is odd!")
In [ ]:
# your solution here
Write a program to test whether a given positive integer $x$ is a prime number!
Hints:
while-loop and an if-statement.
In [ ]:
# your solution here