9 The system requirements: |
9 The system requirements: |
10 * Python - version 2.5.x or newer. |
10 * Python - version 2.5.x or newer. |
11 * IPython |
11 * IPython |
12 * Text editor - scite, vim, emacs or whatever you are comfortable with. |
12 * Text editor - scite, vim, emacs or whatever you are comfortable with. |
13 |
13 |
14 |
14 1. Introduction |
15 |
15 =============== |
16 |
16 |
17 Introduction |
17 The Python programming language was created by a dutch named Guido van Rossum. |
18 ============ |
18 The idea of Python was conceived in December 1989. The name Python has nothing |
19 |
19 to do with the reptilian, but its been named after the 70s comedy series |
20 The Python programming language was created by a dutch named Guido van Rossum. |
20 "Monty Python's Flying Circus", since it happens to be Guido's favourite |
21 The idea of Python was conceived in December 1989. The name Python has nothing |
21 TV series. |
22 to do with the reptilian, but its been named after the 70s comedy series |
22 |
23 "Monty Python's Flying Circus", since it happens to be Guido's favourite |
23 Current stable version of Python is 2.6.x, although Python 3.0 is also the stable |
24 TV series. |
24 version, it is not backwards compatible with the previous versions and is hence |
25 |
25 not entirely popular at the moment. This material will focus on the 2.6.x series. |
26 Current stable version of Python is 2.6.x, although Python 3.0 is also the stable |
26 |
27 version, it is not backwards compatible with the previous versions and is hence |
27 Python is licensed under the Python Software Foundation License (PSF License) |
28 not entirely popular at the moment. This material contains material pertaining |
28 which is GPL compatible Free Software license (excepting license version 1.6 and 2.0) |
29 to the 2.6.x series. |
29 It is a no strings attached license, which means the source code is free to modify |
30 |
30 and redistribute. |
31 The Python Interpreter |
31 |
32 ====================== |
32 The Python docs define Python as "Python is an interpreted, object-oriented, |
33 |
33 high-level programming language with dynamic semantics." A more detailed summary |
34 |
34 can be found at http://www.python.org/doc/essays/blurb.html. Python is a language that |
|
35 has been designed to help the programmer concentrate on solving the problem at hand |
|
36 and not worry about the programming language idiosyncrasies. |
|
37 |
|
38 Python is a highly cross platform compatible language on account of it being an |
|
39 interpreted language. It is highly scalable and hence has been adapted to run on |
|
40 the Nokia 60 series phones. Python has been designed to be readable and easy to use |
|
41 |
|
42 **Resources available for reference** |
|
43 |
|
44 * Web: http://www.python.org |
|
45 * Doc: http://www.python.org/doc |
|
46 * Free Tutorials: |
|
47 * Official Python Tutorial: http://docs.python.org/tut/tut.html |
|
48 * Byte of Python: http://www.byteofpython.info/ |
|
49 * Dive into Python: http://diveintopython.org/ |
|
50 |
|
51 **Advantages of Python - Why Python??** |
|
52 |
|
53 * Python has been designed for readability and ease of use. Its been designed in |
|
54 such a fashion that it imposes readability on the programmer. Python does away |
|
55 with the braces and the semicolons and instead implements code blocks based on |
|
56 indentation, thus enhancing readability. |
|
57 |
|
58 * Python is a high level, interpreted, modular and object oriented language. |
|
59 Python performs memory management on its own, thus the programmer need not bother |
|
60 about allocating and deallocating memory to variables. Python provides extensibility |
|
61 by providing modules which can be easily imported similar to headers in C and |
|
62 packages in Java. Python is object oriented and hence provides all the object oriented |
|
63 characteristics such as inheritance, encapsulation and polymorphism. |
|
64 |
|
65 * Python offers a highly powerful interactive programming interface in the form |
|
66 of the 'Interactive Interpreter' which will be discussed in more detail in the |
|
67 following sections. |
|
68 |
|
69 * Python provides a rich standard library and an extensive set of modules. The |
|
70 power of Python modules can be seen in this slightly exaggerated cartoon |
|
71 http://xkcd.com/353/ |
|
72 |
|
73 * Python interfaces well with most other programming languages such as C, C++ |
|
74 and FORTRAN. |
|
75 |
|
76 Although, Python has one setback. Python is not fast as some of the compiled |
|
77 languages like C or C++. Yet, the amount of flexibility and power more than make |
|
78 up for this setback. |
|
79 |
|
80 |
|
81 1.1 The Python Interpreter |
|
82 -------------------------- |
|
83 |
|
84 1.1.1 The Interactive Interpreter |
|
85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
86 |
|
87 Typing *python* at the shell prompt on any standard Unix/Gnu-Linux system and |
|
88 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python |
|
89 interpreter is one of the most integral features of Python. The prompt obtained |
|
90 when the interactive interpreter is similar to what is shown below. The exact |
|
91 appearance might differ based on the version of Python being used. The ``>>>`` |
|
92 thing shown is the python prompt. When something is typed at the prompt and the |
|
93 enter key is hit, the python interpreter interprets the command entered and |
|
94 performs the appropriate action. |
|
95 |
|
96 :: |
|
97 |
|
98 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
|
99 [GCC 4.3.2] on linux2 |
|
100 Type "help", "copyright", "credits" or "license" for more information. |
|
101 >>> |
|
102 |
|
103 Lets try with an example, type ``print 'Hello, World!'`` at the prompt and hit |
|
104 the enter key. |
|
105 |
|
106 :: |
|
107 |
|
108 >>> print 'Hello, World!' |
|
109 Hello, World! |
|
110 |
|
111 This example was quite straight forward, and thus we have written our first |
|
112 line of Python code. Now let us try typing something arbitrary at the prompt. |
|
113 For example: |
|
114 |
|
115 :: |
|
116 |
|
117 >>> arbit word |
|
118 File "<stdin>", line 1 |
|
119 arbit word |
|
120 ^ |
|
121 SyntaxError: invalid syntax |
|
122 >>> |
|
123 |
|
124 The interpreter gave an error message saying that 'arbit word' was invalid |
|
125 syntax which is valid. The interpreter is an amazing tool when learning to |
|
126 program in Python. The interpreter provides a help function that provides the |
|
127 necessary documentation regarding all Python syntax, constructs, modules and |
|
128 objects. Typing *help()* at the prompt gives the following output: |
|
129 |
|
130 :: |
|
131 |
|
132 >>> help() |
|
133 |
|
134 Welcome to Python 2.5! This is the online help utility. |
|
135 |
|
136 If this is your first time using Python, you should definitely check out |
|
137 the tutorial on the Internet at http://www.python.org/doc/tut/. |
|
138 |
|
139 Enter the name of any module, keyword, or topic to get help on writing |
|
140 Python programs and using Python modules. To quit this help utility and |
|
141 return to the interpreter, just type "quit". |
|
142 |
|
143 To get a list of available modules, keywords, or topics, type "modules", |
|
144 "keywords", or "topics". Each module also comes with a one-line summary |
|
145 of what it does; to list the modules whose summaries contain a given word |
|
146 such as "spam", type "modules spam". |
|
147 |
|
148 help> |
|
149 |
|
150 |
|
151 As mentioned in the output, entering the name of any module, keyword or topic |
|
152 will provide the documentation and help regarding the same through the online |
|
153 help utility. Pressing *Ctrl+d* exits the help prompt and returns to the |
|
154 python prompt. |
|
155 |
|
156 Let us now try a few examples at the python interpreter. |
|
157 |
|
158 Eg 1: |
|
159 :: |
|
160 |
|
161 >>> print 'Hello, python!' |
|
162 Hello, python! |
|
163 >>> |
|
164 |
|
165 Eg 2: |
|
166 :: |
|
167 |
|
168 >>> print 4321*567890 |
|
169 2453852690 |
|
170 >>> |
|
171 |
|
172 Eg 3: |
|
173 :: |
|
174 |
|
175 >>> 4321*567890 |
|
176 2453852690L |
|
177 >>> |
|
178 |
|
179 :: |
|
180 |
|
181 Note: Notice the 'L' at the end of the output. The 'L' signifies that the |
|
182 output of the operation is of type *long*. It was absent in the previous |
|
183 example because we used the print statement. This is because *print* formats |
|
184 the output before displaying. |
|
185 |
|
186 Eg 4: |
|
187 :: |
|
188 |
|
189 >>> big = 12345678901234567890 ** 3 |
|
190 >>> print big |
|
191 1881676372353657772490265749424677022198701224860897069000 |
|
192 >>> |
|
193 |
|
194 :: |
|
195 |
|
196 This example is to show that unlike in C or C++ there is no limit on the |
|
197 value of an integer. |
|
198 |
|
199 1.1.2 *ipython* - An enhanced interactive Python interpreter |
|
200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
201 |
|
202 The power and the importance of the interactive interpreter was the highlight |
|
203 of the previous section. This section provides insight into the enhanced |
|
204 interpreter with more advanced set of features called **ipython**. Entering |
|
205 *ipython* at the shell prompt fires up the interactive interpreter. |
|
206 |
|
207 :: |
|
208 |
|
209 $ ipython |
|
210 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
|
211 Type "copyright", "credits" or "license" for more information. |
|
212 |
|
213 IPython 0.8.4 -- An enhanced Interactive Python. |
|
214 ? -> Introduction and overview of IPython's features. |
|
215 %quickref -> Quick reference. |
|
216 help -> Python's own help system. |
|
217 object? -> Details about 'object'. ?object also works, ?? prints more. |
|
218 |
|
219 In [1]: |
|
220 |
|
221 This is the output obtained upon firing ipython. The exact appearance may |
|
222 change based on the Python version installed. The following are some of the |
|
223 various features provided by **ipython**: |
|
224 |
|
225 * Suggestions - ipython provides suggestions of the possible methods and |
|
226 operations available for the given python object. |
|
227 |
|
228 Eg: |
|
229 |
|
230 :: |
|
231 |
|
232 In [4]: a = 6 |
|
233 |
|
234 In [5]: a. |
|
235 a.__abs__ a.__divmod__ a.__index__ a.__neg__ |
|
236 a.__rand__ a.__rmod__ a.__rxor__ |
|
237 a.__add__ a.__doc__ a.__init__ a.__new__ |
|
238 a.__rdiv__ a.__rmul__ a.__setattr__ |
|
239 a.__and__ a.__float__ a.__int__ a.__nonzero__ |
|
240 a.__rdivmod__ a.__ror__ a.__str__ |
|
241 a.__class__ a.__floordiv__ a.__invert__ a.__oct__ |
|
242 a.__reduce__ a.__rpow__ a.__sub__ |
|
243 a.__cmp__ a.__getattribute__ a.__long__ a.__or__ |
|
244 a.__reduce_ex__ a.__rrshift__ a.__truediv__ |
|
245 a.__coerce__ a.__getnewargs__ a.__lshift__ a.__pos__ |
|
246 a.__repr__ a.__rshift__ a.__xor__ |
|
247 a.__delattr__ a.__hash__ a.__mod__ a.__pow__ |
|
248 a.__rfloordiv__ a.__rsub__ |
|
249 a.__div__ a.__hex__ a.__mul__ a.__radd__ |
|
250 a.__rlshift__ a.__rtruediv__ |
|
251 |
|
252 In this example, we initialized 'a' (a variable - a concept that will be |
|
253 discussed in the subsequent sections.) to 6. In the next line when the *tab* key |
|
254 is pressed after typing '*a.*' ipython displays the set of all possible methods |
|
255 that are applicable on the object 'a' (an integer in this context). Ipython |
|
256 provides many such datatype specific features which will be presented in the |
|
257 further sections as and when the datatypes are introduced. |
|
258 |
|
259 1.2 Editing and running a python file |
|
260 ------------------------------------- |
|
261 |
|
262 The |