Programming at the speed of light Pythonism Kracekumar.R [email_address]
Agenda <ul><li>What is Python ???
Why Python ???
Syntax Walkthroughs
Linux and Python </li></ul>
What is Python ??? <ul><li>What is Python ???
Why Python ???
Syntax Walkthroughs
Linux and Python </li></ul>
History <ul><li>Created by Guido von Rossum in 1990 (BDFL)
named after Monty Python's Flying Circus
http://www.python.org/~guido/
Blog http://neopythonic.blogspot.com/
Now works for Google  </li></ul>
What is Python ??? <ul><li>general-purpose high-level programming language, often used as a scripting language.
interpreted, interactive, object-oriented.
incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes, automatic memory manage...
remarkable power with very clear syntax.
has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++.  <...
What is Python ??? <ul><li>supports multiple programming paradigms (primarily object oriented, imperative, and functional)
portable: runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, OS/2, FreeBSD Solaris, OS/...
What is Python ??? <ul><li>Developed and supported by a large team of  volunteers - Python Software Foundation
Major implementations: CPython, Jython, Iron  Python, PyPy </li></ul><ul><ul><li>CPython - implemented in C, the primary i...
Jython - implemented for the JVM
Pypy - implemented in Python
IronPython - implemented in C#, allows python to use the .NET libraries </li></ul></ul>
Why Python ??? <ul><li>What is Python ???
Why Python ???
Syntax Walkthroughs
Linux and Python </li></ul>
Why Python ??? <ul><li>Readability, maintainability, very clear readable syntax
Fast development and all just works  the first time...
very high level dynamic data types
Dynamic typing and automatic memory management
Free and open source </li></ul><ul><ul><li>Implemented under an open source license. Freely usable and distributable, even...
Availability (cross-platform)
Interactivity (interpreted language) </li></ul>
Why Python ??? <ul><li>GUI support – GUIs typically developed with Tk
Strong introspection capabilities
Intuitive object orientation
Natural expression of procedural code
Full modularity, supporting hierarchical  packages
Exception­based error handling
The ability to be embedded within applications as a scripting interface
Scalable – can play nicely with other languages </li></ul>
Batteries Included <ul><li>The Python standard library is very extensive  </li></ul><ul><ul><li>regular expressions, codecs
date and time, collections, threads and mutexs
OS and shell level functions (mv, rm, ls)
Support for SQLite and Berkley databases
zlib, gzip, bz2, tarfile, csv, xml, md5, sha
logging, subprocess, email, json
httplib, imaplib, nntplib, smtplib
and much, much more ... </li></ul></ul>
Python Libraries <ul><li>Biopython - Bioinformatics
SciPy  - Linear algebra, signal processing
NumPy - Fast compact multidimensional arrays
PyGame - Game Development
Upcoming SlideShare
Loading in …5
×

Learn python

4,235 views
4,007 views

Published on

Learn python in linux and kick start your career

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
  • super....!!!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,235
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
255
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide
  • - needs Software Freedom Day@Alexandria University
  • Write most useful links for beginners starting
  • Write something more interactive
  • Learn python

    1. 1. Programming at the speed of light Pythonism Kracekumar.R [email_address]
    2. 2. Agenda <ul><li>What is Python ???
    3. 3. Why Python ???
    4. 4. Syntax Walkthroughs
    5. 5. Linux and Python </li></ul>
    6. 6. What is Python ??? <ul><li>What is Python ???
    7. 7. Why Python ???
    8. 8. Syntax Walkthroughs
    9. 9. Linux and Python </li></ul>
    10. 10. History <ul><li>Created by Guido von Rossum in 1990 (BDFL)
    11. 11. named after Monty Python's Flying Circus
    12. 12. http://www.python.org/~guido/
    13. 13. Blog http://neopythonic.blogspot.com/
    14. 14. Now works for Google </li></ul>
    15. 15. What is Python ??? <ul><li>general-purpose high-level programming language, often used as a scripting language.
    16. 16. interpreted, interactive, object-oriented.
    17. 17. incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes, automatic memory management.
    18. 18. remarkable power with very clear syntax.
    19. 19. has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. </li></ul>
    20. 20. What is Python ??? <ul><li>supports multiple programming paradigms (primarily object oriented, imperative, and functional)
    21. 21. portable: runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, OS/2, FreeBSD Solaris, OS/2, Amiga,AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS, VxWorks, PlayStation, Sharp Zaurus, Windows CE and even PocketPC ! </li></ul>
    22. 22. What is Python ??? <ul><li>Developed and supported by a large team of volunteers - Python Software Foundation
    23. 23. Major implementations: CPython, Jython, Iron Python, PyPy </li></ul><ul><ul><li>CPython - implemented in C, the primary implementation
    24. 24. Jython - implemented for the JVM
    25. 25. Pypy - implemented in Python
    26. 26. IronPython - implemented in C#, allows python to use the .NET libraries </li></ul></ul>
    27. 27. Why Python ??? <ul><li>What is Python ???
    28. 28. Why Python ???
    29. 29. Syntax Walkthroughs
    30. 30. Linux and Python </li></ul>
    31. 31. Why Python ??? <ul><li>Readability, maintainability, very clear readable syntax
    32. 32. Fast development and all just works the first time...
    33. 33. very high level dynamic data types
    34. 34. Dynamic typing and automatic memory management
    35. 35. Free and open source </li></ul><ul><ul><li>Implemented under an open source license. Freely usable and distributable, even for commercial use. </li></ul></ul><ul><li>Simplicity , Great first language
    36. 36. Availability (cross-platform)
    37. 37. Interactivity (interpreted language) </li></ul>
    38. 38. Why Python ??? <ul><li>GUI support – GUIs typically developed with Tk
    39. 39. Strong introspection capabilities
    40. 40. Intuitive object orientation
    41. 41. Natural expression of procedural code
    42. 42. Full modularity, supporting hierarchical packages
    43. 43. Exception­based error handling
    44. 44. The ability to be embedded within applications as a scripting interface
    45. 45. Scalable – can play nicely with other languages </li></ul>
    46. 46. Batteries Included <ul><li>The Python standard library is very extensive </li></ul><ul><ul><li>regular expressions, codecs
    47. 47. date and time, collections, threads and mutexs
    48. 48. OS and shell level functions (mv, rm, ls)
    49. 49. Support for SQLite and Berkley databases
    50. 50. zlib, gzip, bz2, tarfile, csv, xml, md5, sha
    51. 51. logging, subprocess, email, json
    52. 52. httplib, imaplib, nntplib, smtplib
    53. 53. and much, much more ... </li></ul></ul>
    54. 54. Python Libraries <ul><li>Biopython - Bioinformatics
    55. 55. SciPy - Linear algebra, signal processing
    56. 56. NumPy - Fast compact multidimensional arrays
    57. 57. PyGame - Game Development
    58. 58. Visual Python - real-time 3D output
    59. 59. Django - High-level python Web framework
    60. 60. and much more ... </li></ul>
    61. 61. E.g. Projects with Python <ul><li>Websites: Google, YouTube, Yahoo Groups & Maps, CIA.gov </li></ul><ul><ul><li>Appengine: http://code.google.com/appengine/
    62. 62. ” Google: Python has been an important part of Google since the beginning.”, Peter Norvig.
    63. 63. Python application servers and Python scripting to create the web UI for BigTable (their database project) </li></ul></ul><ul><li>Systems: NASA, LALN, CERN, Rackspace </li></ul><ul><ul><li>Nasa Nebula http://nebula.nasa.gov/about </li></ul></ul><ul><li>Games: Civilization 4, Quark (Quake Army Knife)
    64. 64. Mobile phones: Nokia S60 (Symbian), PythonCE
    65. 65. P2P: BitTorrent </li></ul>
    66. 66. Cont . . . <ul><li>Maya, a powerful integrated 3D modeling and animation system, provides a Python scripting API.
    67. 67. EVE Online, a Massively Multi player Online Game (MMOG), makes extensive use of Python.
    68. 68. iRobot uses Python to develop commercial robotic devices. </li></ul>
    69. 69. What can you do with Python <ul><li>Systems Programming
    70. 70. GUI
    71. 71. Internet Scripting
    72. 72. Database Programming
    73. 73. Numeric and Scientific Programming
    74. 74. Natural language analysis </li></ul>
    75. 75. What people say about Python ? <ul><li>I can remember many Python idioms because </li></ul>they're simpler. That's one more reason I program faster [in Python]. I still have to look up how to open a file every time I do it in Java. In fact, most things in Java require me to look something up. --Bruce Eckel
    76. 76. Cont . . . <ul><li>Python ... is compact -- you can hold its entire </li></ul>feature set (and at least a concept index of its libraries) in your head. -- Eric S. Raymond
    77. 77. Syntax Walkthroughs <ul><li>What is Python ???
    78. 78. Why Python ???
    79. 79. Syntax Walkthroughs
    80. 80. Linux and Python </li></ul>
    81. 81. Talk is cheap. Show me the code. Linus Torvaldus
    82. 82. Hello World <ul><li>Python 2.6 </li></ul><ul><li>Python 3.0 </li></ul>print (“Hello World”) print “Hello World”
    83. 83. Starting python <ul><li>Open terminal and Enter python and hit enter </li></ul>Something like this kracekumar@kracekumar-laptop:~$ python Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information. >>>
    84. 84. Interactive shell <ul><li>>>> 2+3
    85. 85. 5
    86. 86. >>> 2*3
    87. 87. 6
    88. 88. >>> 2/3
    89. 89. 0
    90. 90. >>> 2//3
    91. 91. 0 </li></ul>
    92. 92. Interactive Shell cont . . . <ul><li>>>> 2.0/3.0
    93. 93. 0.66666666666666663
    94. 94. >>> 2.0/3
    95. 95. 0.66666666666666663
    96. 96. >>> 2/3.0
    97. 97. 0.66666666666666663
    98. 98. >>> 2-3
    99. 99. -1 </li></ul>
    100. 100. Interactive shell Cont . . . <ul><li>>>> complex(3,4)
    101. 101. (3+4j)
    102. 102. >>> 5+4j+5+6j
    103. 103. (10+10j)
    104. 104. >>> pow(4,5)
    105. 105. 1024
    106. 106. >>> 4**5
    107. 107. 1024 </li></ul>
    108. 108. Math More <ul><li>>>> 3%4
    109. 109. 3
    110. 110. >>> long(34.999999)
    111. 111. 34L
    112. 112. >>> int(34.9999)
    113. 113. 34
    114. 114. >>> str(5)
    115. 115. '5' </li></ul>
    116. 116. Still more Interactive shell <ul><li>>>> chr(45)
    117. 117. '-'
    118. 118. >>> ord('a')
    119. 119. 97 </li></ul>You can do more in interactive shell ,But you might be bored,so Lets start writing small scripts
    120. 120. First program <ul><li>Open first.py
    121. 121. Run python first.py ,Your program will be interpreted and python will produce .pyc in memory .
    122. 122. .pyc =>python compiled
    123. 123. Duck typed programming language </li></ul>
    124. 124. Interactive Shell (Revisited) <ul><li>>>> b='0x123'
    125. 125. >>> type(b)
    126. 126. <type 'str'>
    127. 127. >>> b=23
    128. 128. >>> type(b)
    129. 129. <type 'int'>
    130. 130. >>> c=32L
    131. 131. >>> type(c)
    132. 132. <type 'long'> </li></ul>
    133. 133. Cont . . . <ul><li>>>> c=2**890
    134. 134. >>> c
    135. 135. 8254602048994769474255309139320571976856989469314398783249386078541779727448825929287769623244643560854287421769642635607536680617229461519539671538483430889193541937484454440136429963694163141453503639190799818814812942074243152169349951543234944945149040326527156224L
    136. 136. >>> len(str(c))
    137. 137. 268
    138. 138. Str() =>Find string length </li></ul>
    139. 139. String <ul><li>>>> name='krace'
    140. 140. >>> name1=&quot;krace&quot;
    141. 141. >>> name2=&quot;&quot;&quot;krace&quot;&quot;&quot;
    142. 142. >>> name3='''krace'''
    143. 143. >>> name
    144. 144. 'krace'
    145. 145. >>> name1
    146. 146. 'krace'
    147. 147. >>> name2
    148. 148. 'krace'
    149. 149. >>> name3
    150. 150. 'krace' </li></ul>
    151. 151. Importing in built modules <ul><li>Open try_import .py
    152. 152. Strings
    153. 153. >>> name=&quot;kracekumar&quot;
    154. 154. >>> name[1:]
    155. 155. 'racekumar'
    156. 156. >>> name[:1]
    157. 157. 'k'
    158. 158. >>> name[:5]
    159. 159. 'krace'
    160. 160. >>> name[:-5]
    161. 161. 'krace' </li></ul>
    162. 162. Strings are immutable <ul><li>>>> name[0]=&quot;a&quot;
    163. 163. Traceback (most recent call last):
    164. 164. File &quot;<stdin>&quot;, line 1, in <module>
    165. 165. TypeError: 'str' object does not support item assignment
    166. 166. >>> name
    167. 167. 'kracekumar'
    168. 168. >>> name=&quot;K&quot;+name[1:]
    169. 169. >>> name
    170. 170. 'Kracekumar'
    171. 171. Above is a small hack to change the first character value in the name </li></ul>
    172. 172. Slicing <ul><li>>>> name[1:5]
    173. 173. 'race'
    174. 174. >>> name + &quot; likes linux &quot; #concatenation
    175. 175. 'Kracekumar likes linux '
    176. 176. >>> name.startswith('k')
    177. 177. False
    178. 178. >>> name.upper()
    179. 179. 'KRACEKUMAR' </li></ul>
    180. 180. Python introspection <ul><li>Python introspection lets you to find all builtin functions,classes etc. . .
    181. 181. >>> dir()
    182. 182. ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'name', 'name1', 'name2', 'name3', 'sys']
    183. 183. dir(anyname) =>will yield all the supported functions. </li></ul>
    184. 184. In Built help <ul><li>help(anyname)
    185. 185. >>> help(a) </li></ul>no Python documentation found for '123' #here no documentation is available so you won't find <ul><li>help(sys) </li></ul>Find the magic here :)
    186. 186. Data structure <ul><li>3 in built data structure
    187. 187. List => a=[1,”linux”,2,”Free Bsd”]
    188. 188. Tuple => b=(1,”apple”,mango)#mango is not quoted because here it refers variable
    189. 189. Dictionary =>dict={”name”:”kracekumar”,”email”:” [email_address] ”,”coll”:”Amrita”}
    190. 190. Don't worry guys you have array but i will introduce later </li></ul>
    191. 191. Data structure <ul><li>>>> a
    192. 192. [1, 'linux', 2, 'Free Bsd']
    193. 193. >>> b
    194. 194. (1, 'apple', 'mango')
    195. 195. >>> dict
    196. 196. {'coll': 'Amrita', 'name': 'kracekumar', 'email': 'kracethekingmaker@gmail.com'}
    197. 197. List is not linked list here
    198. 198. You can have string ,int,float in list,tuple,dictionary </li></ul>
    199. 199. Data structure in python <ul><li>>>> type(a)
    200. 200. <type 'list'>
    201. 201. >>> type(b)
    202. 202. <type 'tuple'>
    203. 203. >>> type(dict)
    204. 204. <type 'dict'>
    205. 205. Tuple is immutable </li></ul>
    206. 206. Cont . . . <ul><li>>>> a[1]
    207. 207. 'linux'
    208. 208. >>> a[1]=&quot;UBuntu&quot;
    209. 209. >>> a[1]
    210. 210. 'UBuntu'
    211. 211. >>> a
    212. 212. [1, 'UBuntu', 2, 'Free Bsd'] </li></ul>
    213. 213. Cont . . . <ul><li>>>> b[1]
    214. 214. 'apple'
    215. 215. >>> b[1]=&quot;pine apple&quot;
    216. 216. Traceback (most recent call last):
    217. 217. File &quot;<stdin>&quot;, line 1, in <module>
    218. 218. TypeError: 'tuple' object does not support item assignment
    219. 219. >>> b
    220. 220. (1, 'apple', 'mango') </li></ul>
    221. 221. Dictionary <ul><li>>>> dict
    222. 222. {'coll': 'Amrita', 'name': 'kracekumar', 'email': 'kracethekingmaker@gmail.com '}
    223. 223. coll =>is key and 'Amrita' =>value
    224. 224. All key should be unique
    225. 225. >>> dict['coll']=&quot;ASE&quot;
    226. 226. >>> print dict
    227. 227. {'coll': 'ASE', 'name': 'kracekumar', 'email': 'kracethekingmaker@gmail.com'}
    228. 228. >>> dir(dict) </li></ul>
    229. 229. Dictionary functions <ul><li>dir() and help() will help you to know more about list tuple
    230. 230. dir(dict) </li></ul>Try <ul><li>has_key()
    231. 231. Pop()
    232. 232. Items()
    233. 233. Slicing holds for list and tuples </li></ul>
    234. 234. Control Flow if guess == number: #do something elif guess < number: #do something else else : #do something else while True : #do something #break when done break else : #do something when the loop ends for i in range( 1 , 5 ): print (i) else : print ( 'The for loop is over' ) #1,2,3,4 for i in range( 1 , 5 , 2 ): print (i) else : print ( 'The for loop is over' ) #1,3
    235. 235. Program <ul><li>Wap to find a no is prime or not
    236. 236. List comprehension
    237. 237. E.g >>> x=[y for y in range(0,20) if y%2 == 0]
    238. 238. >>> x
    239. 239. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    240. 240. Alternate way to do
    241. 241. Open list_comp.py
    242. 242. Dictionaries,list,tuple can be nested </li></ul>
    243. 243. Functions <ul><li>Order is important unless using the name </li></ul><ul><li>Default arguments are supported </li></ul>def sayHello (): print ( 'Hello World!' ) def foo (name, age, address) : pass foo( 'Tim' , address= 'Home' , age= 36 ) def greet (name= 'World' )
    244. 244. Functions <ul><li>Variable length args acceptable as a list or dict </li></ul>def total (initial= 5 , *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: count += keywords[key] return count print (total( 10 , 1 , 2 , 3 , vegetables= 50 , fruits= 100 ))
    245. 245. Functions def printMax (x, y): '''Prints the maximum of two numbers. The two values must be integers.''' x = int(x) # convert to integers, if possible y = int(y) if x > y: r eturn x else : r eturn y printMax( 3 , 5 )
    246. 246. Last important thing about variable <ul><li>>>> a=[1,2,3]
    247. 247. >>> b=a
    248. 248. >>> a[0]=4
    249. 249. >>> b
    250. 250. [4, 2, 3]
    251. 251. >>> a
    252. 252. [4, 2, 3]
    253. 253. If you want to have copy of a list use slice or import copy(use dir and find out) </li></ul>
    254. 254. File Handling <ul><li>Wap to read input from user and write back to file . =>fav_movie.py </li></ul><ul><li>Wap to read set of numbers and write to file and read the file and add all the nos.=>pickling.py </li></ul><ul><li>Swap 3 nos </li></ul>
    255. 255. Practice problem <ul><li>Get a particular line from a file = >get_particular_line.py
    256. 256. Get a list of zip file content and its size =>zip_length.py
    257. 257. Get current system name and ip=>details.py
    258. 258. Generate a random password using python </li></ul>
    259. 259. Modules <ul><li>Any python file is considered a module
    260. 260. Modules can be imported or run by themselves </li></ul>if __name__ == '__main__' : print ( 'This program is being run by itself' ) else : print ( 'I am being imported from another module' )
    261. 261. Recursion <ul><li>>>> def mysum(L):
    262. 262. ... if not L:
    263. 263. ... return 0
    264. 264. ... else:
    265. 265. # Call myself
    266. 266. ... return L[0] + mysum(L[1:])
    267. 267. >>> mysum([1, 2, 3, 4, 5])
    268. 268. 15 </li></ul>
    269. 269. OOPs <ul><li>Python OOP is easy and simple
    270. 270. Class classname: </li></ul><ul><ul><li>Def name(): </li></ul></ul>Objects <ul><ul><li>instance=classname()
    271. 271. Note no new keyword
    272. 272. first_class.py
    273. 273. Isinstance()
    274. 274. issubclass() </li></ul></ul>
    275. 275. Inbuilt functions <ul><li>hasattr(obj,attr)
    276. 276. getattr(obj,attr)=>retrieves attr value
    277. 277. setattr(obj,attr,val)=>set attr value
    278. 278. delattr(obj,attr)
    279. 279. first_class.py
    280. 280. add_book.py </li></ul>
    281. 281. More Resources <ul><li>http://wiki.python.org/moin/BeginnersGuide
    282. 282. http://www.python.org/doc/faq/
    283. 283. Learn Python in 10 minutes: http://www.poromenos.org/tutorials/python
    284. 284. Byte of Python: http://www.swaroopch.com/notes/Python
    285. 285. Dive Into Python: http://diveintopython.org/
    286. 286. Google </li></ul>
    287. 287. Any Questions ???
    288. 288. Thank You

    ×