Dabbling with Python
Dictionaries
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}
. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.
>>> d1={‘name’:’Fedora’,’Company’:’Redhat project’} # Create a dicionary d1
>>> d1 #Display the dictionary
{‘Company’: ‘Redhat project’, ‘name’: ‘Fedora’}
>>> d1[‘name’]; #Get the value of the key name
‘Fedora’
>>> d1.update({‘Current Version’:’FC6′}) #Add a key value pair
>>> d1
{‘Company’: ‘Redhat project’, ‘Current Version’: ‘FC6’, ‘name’: ‘Fedora’}
>>> if d1.has_key(‘name’):d1[‘name’] #Search for a key if present
…
‘Fedora’
Modules
if you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__
. For instance, use your favorite text editor to create a file called arith.py in the current directory with the following contents:
# Arithmetic operations on numbers
def sum(a,b): # sum
return a+bdef diff(a,): # diff
return a-b>>> import arith
>>> arith.sum(1,2)
3>>> sum.__name__
‘sum’
The module we just created was a user defined module, defined by us.Python comes with a library of standard modules, described in a separate document, the Python Library Reference (“Library Reference” hereafter). Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls. The set of such modules is a configuration option which also depends on the underlying platform For example, the amoeba module is only provided on systems that somehow support Amoeba primitives. One particular module deserves some attention: sys, which is built into every Python interpreter.
>>> import sys
>>> sys.ps1=’python >’ # changing the python prompt
python >
python >sys.platform;
‘linux2’
python >sys.byteorder
‘little’
Getting started with Tkinter
Resources