---+ Exercises for session1: Introduction to !IoT and Python ---++ Exercise 1: * <p>Write a program that calculates and prints the Fibonacci numbers up to 20 iterations. <br /> The Fibonacci numbers are calculated as follows:<br /> F[0] = 0<br /> F[1] = 1<br /> F[n] = F[n-1] + F[n-2]</p> * <p>Modify your program to get the number of iterations from a command line parameter:</p> <p> _fibonacci.py 20_ will calculate and print the same numbers as the exercise above</p> ---++ 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 <i>module </i>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: * <p>prime</p> * <p>fibonacci</p> * <p>factorial</p> 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. -- %USERSIG{UliRaich - 2019-05-27}% ---++ Comments %COMMENT%
This topic: AFNOG
>
WebHome
>
AFNOGWorkshop2019
>
AFNOG-2019Slides
>
WorkshopExercises
>
Session1
Topic revision: r1 - 2019-05-27 - UliRaich
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback