Sei sulla pagina 1di 12

Chapter 3 : Python On The Pi

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

i) as an interactive shell to execute individual command


Open IDLE 3 application (>>>) Python interactive prompt Examples : >>> print("Saluton Mondo!") # And hit Enter or Return. Python executes that statement and youll see the result in the shell window. >>> 3+4+5 12 # As a kind of calculator to test out statements or calculations. >>> import math >>> (1 + math.sqrt(5)) / 2 1.618033988749895 # Can set up variables or import modules
2

i) as an interactive shell to execute individual command


>>> import math >>> radius = 200 >>> radius * 2 * math.pi 125.66370614359173 # import command makes all of Pythons math functions available to your program. To set up a variable, use the assignment operator (=). >>> help("print") >>> help("topics") >>> help("keywords") >>> help("modules") # get information about how to use a particular statement, module, or other Python topics with the help() command
3

ii) as a command line program to execute standalone scripts


Open IDLE application To start new Python program (open script editing window) , select File -> New Window Examples :

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

Setup and Loop functions


setup() is is a function run once loop() is a function that executes over and over Example 1 : Select New Window from the shell in IDLE 3 and type the following
n = 1, 2 , 3, ..
Print n if remainder of n/2 is 0 Examples : N=1, 1/2 remainder not 0, not print N=2, 2/2 remainder 0, print 2

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

Setup and Loop functions


Examples 2 :
It is important to watch your whitespace; Python is a highly structured language where the whitespace determines the structure. In the next example, everything indented one level below the loop() function is considered part of that function. The end of a loop is determined by where the indentation moves up a level (or end of the file). *In this example, the output will be every even number from 102 on. Heres how it works: -First the variable n is defined as a global variable that can be used in any block in the script. -Here, the setup() function is defined (but not yet executed). -Similarly, heres the definition of the loop() function. -In the main code block, setup() is called once, then loop(). *global keyword : tells the interpreter to use the global variable n rather than create a second (local, or private to that function) n variable usable only in the function. 6

Objects and Modules


Core part of the language : Only 34 reserved keywords Everything isnt keywords can be considered as an Object Object : a combination of data and behaviors that has a name. You can change an objects data, retrieve information from it, and even manipulate other objects.

Objects and Modules


A Python object can be thought of as an encapsulated collection of attributes and methods. You get access to these attributes and methods using a simple dot syntax [ . ] Example 1 : >>> myString = "quux" >>> myString.capitalize() 'Quux # set up a string object and call the method that tells it to capitalize itself Example 2 : >>> myList = ['a', 'man', 'a', 'plan', 'a', 'canal'] >>> myList.reverse() >>> print(myList) ['canal', 'a', 'plan', 'a', man', 'a'] # use reverse() to rearrange a list in reverse order

Objects and Modules


Both String and List are built-in modules of the standard library Some of the standard library modules are not built-in and you need to explicitly say youre going to use them with the import command. To use the time module from the standard library to gain access to helpful functions for dealing with timing and timestamps, use : import time You may also see the use of import as to rename the module in your program: import time as myTime Youll also see the use of from import to load select functions from a module: from time import clock
9

Objects and Modules


Example 3 :

# 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

Objects and Modules


Example 4 :

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

Objects and Modules


Example 5 :

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

Potrebbero piacerti anche