Iteration

1 / 5

For loops are used to execute a piece of code repetitively. The code that is executed is called the body of the for loop. The for loop uses an index that runs over a certain specified range. For example:

for i in range(0,10):
  print(i)

This code prints the numbers 0 up to and including 9 (and not 10). The reason that 10 is not included is subtle, but essentially because indexing starts at 0. Indeed, we can also do:

for i in range(10):
  print(i)

This gives the same result. The for loop iterates 10 times. Starting from 0, the ten indices are 0 up to and including 9.

Integer ranges

An number $p > 2$ is prime if it is not divisible by any number $i$ such that $1 < i < p$. A number that is not prime is called composite.

Input a number n and print prime or composite based on whether the value of n is prime.

Hint: In Python, the operator % gives remainder after division. So n being divisible by i is equivalent to n%i == 0.

Congratulations!

You passed the !

Welcome!

Would you like a guided tour on how to use Vibewise?

Your email has been verified!