Tags:
create new tag
view all tags

# Exercises for session1: Introduction to IoT and Python

## Exercise 1:

• Write a program that calculates and prints the Fibonacci numbers up to 20 iterations.
The Fibonacci numbers are calculated as follows:
F[0] = 0
F[1] = 1
F[n] = F[n-1] + F[n-2]

• Modify your program to get the number of iterations from a command line parameter:

fibonacci.py 20 will calculate and print the same numbers as the exercise above

## Exercise 2:

Write a program that checks if a number is a prime number. It will either print that it found a prime number or give a valid divisor. Get the number to be tested again from the command line argument as in exercise 1

## Exercise 3:

Modify the above programs into functions that can be called from a main program and write this main program into the same file (Test if name is “__main__” to be able to run the program).

Then write a main program in a separate file and import the module into your main program. Call the function in the module

Do the same thing for the prime example

## Exercise 4:

Add a module with a function calculating the factorial of a number:

factorial(n) = 1*2*3*4… *n

## Exercise 5:

Finally put the fibonacci, the factorial and the prime module into a packet. How do you have to import and call the functions now? If the number if iterations is smaller than 1, raise a ValueError

## Exercise 6:

Convert the packet with the 3 math functions into a class with the methods:

• prime

• fibonacci

• factorial

How do you call the functions now?

## Exercise 7:

Write a callback function printing “Hello” and a program with an endless main loop which calls the callback function once every 2 s.

--