Python On The Pi CHAPTER 3
Python On The Pi CHAPTER 3
Interpreted language : can write a program and execute it directly rather than compiling it into machine code Quicker to program : the interpreter figures out itself the data types when program executed. No need to notify variable types. Can be run in 2 ways :
i) as an interactive shell to execute individual command ii) as a command line program to execute standalone scripts
1
At script editing window, type print("Saluton Mondo!"), then save in your home directory as SalutonMondo.py . Select Run -> Run Module . Youll see it execute in the shell. * You can also run Python script from the command line. Open up LXTerminal and type python SalutonMondo.py .
4
Run Module and give your script a name (such as EvenIntegers.py). As it runs, you should see all even integers printed (press Control-C to interrupt the output, because it will go on forever).
5
# sleep function stops the execution of the program for one second. One thing you will notice after running this code is that the time will drift a bit each time. Thats for two reasons: 1. The code doesnt take into account the amount of time it takes to calculate the current time (.9 seconds would be a better choice) 2. Other processes are sharing the CPU and may take cycles away from your programs execution.
10
There is error in the e-book. Change while to for. modify example 3 to open a text file and periodically log some data to it. Everything is a string when handling text files. Use the str() function to convert numbers to strings (and int() to change back to an integer).
11
opens the file, reads each line as a string and prints it. Note that print() acts like println() does in other languages; it adds a newline to the string that is printed. The end argument to print() suppresses the newline.
12