Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Added: | ||||||||
> > |
Exercises on PythonBefore being able to start programming sensors and actuators we need to know a programming language giving us access to the sensor and actuator interfaces. Python is an interpreted language, rather easy to learn. Nevertheless, learning a new computer language in just 2 hours is of course a challenge. Here are a few simple exercises to get a feeling for the language:Exercise Session 1 Introduction to Python1. Connection to the Raspberry Pi and first steps in PythonConnect to the Raspberry Pi using ssh with X forwarding. Create a directory afnog in your home directory and a subdirectory session_1pwd tells you which is your current working directory and this should be the same as defined in the environment variable HOME. Check that echo $HOME and pwd gives you the same result. Then create the directories with mkdir -p afnog/session_1 and go to the session_1 directory: cd afnog/session_1 Now start idle and type in the commands we have seen during the lectures 2. A first programConvert the above into a script and run it within idle. Save it with file name into.py Then
3. AssignmentsCreate 2 variables: a=5 and b=3 and apply the 4 basics arithmetic operations on them: a+b, a-b, a*b, a/b and print the results4.ConditionalsUse again the 2 values a and b in the example above and write a program that finds out, which of the two values is bigger and prints the result. Exchange the values of a and b.5. LoopsWrite a program that calculates 20 sin(x) values for 0 < x < 2*п You must import the math library: import math Then you can call the math function sin with result=math.sin(x). This calculates the sin value of x, where x is given in radians.You get the value of pi: piValue = math.pi 6. Command line argumentsWrite a program that accepts command line arguments. The program is supposed to find out how many arguments have been given and prints their values. Please notice that the arguments are passed as strings. You must import the sys library: import sys Now you have access to the parameters given by the user in the string array called sys.argv. The length of this array: len(sys.argv) is the number of parameters.7. Function definitionsWrite a function calculating the first n Fibonacci numbers where n is a parameter to the function. Write a main program calling this function and print the first 20 Fibonacci numbers. You may improve this program by giving the user the choice of how many numbers are to be calculated through a command line parameter.8. ModulesPut the function definitions of exercise 7 into a module called fibo. Write a new Python program importing the module and calling the functions defined in it.9. FilesCollect the sine values from exercise 5 into a list and write this list (converted into a string) onto a file called sineFile.txt. Have a look at the file with an editor or (in Unix) with the cat or more command.10. ClassesWrite a calculator class with methods implementing the 4 basic arithmetic operations plus, minus, mult and div.HelpIf you need help, small example code etc. please have a look at the Python tutorial![]() ![]() Comments |