Programming Under Linux In Python Marwan Osman [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++.
usable as an extension language for applications that need a programmable interface.  </li></ul>
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><li>CPython - implemented in C, the primary implementa...
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
Paradigm of your choice
Free and open source </li><ul><li>Implemented under an open source license. Freely usable and distributable, even for comm...
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
Upcoming SlideShare
Loading in...5
×

Programming Under Linux In Python

8,418

Published on

Python language and some usage for it under Linux.

Published in: Technology, Education
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,418
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
522
Comments
0
Likes
13
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
  • Transcript of "Programming Under Linux In Python"

    1. 1. Programming Under Linux In Python Marwan Osman [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++.
    20. 20. usable as an extension language for applications that need a programmable interface. </li></ul>
    21. 21. What is Python ??? <ul><li>supports multiple programming paradigms (primarily object oriented, imperative, and functional)
    22. 22. 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>
    23. 23. What is Python ??? <ul><li>Developed and supported by a large team of volunteers - Python Software Foundation
    24. 24. Major implementations: CPython, Jython, Iron Python, PyPy </li><ul><li>CPython - implemented in C, the primary implementation
    25. 25. Jython - implemented for the JVM
    26. 26. Pypy - implemented in Python
    27. 27. IronPython - implemented in C#, allows python to use the .NET libraries </li></ul></ul>
    28. 28. Why Python ??? <ul><li>What is Python ???
    29. 29. Why Python ???
    30. 30. Syntax Walkthroughs
    31. 31. Linux and Python </li></ul>
    32. 32. Why Python ??? <ul><li>Readability, maintainability, very clear readable syntax
    33. 33. Fast development and all just works the first time...
    34. 34. very high level dynamic data types
    35. 35. Dynamic typing and automatic memory management
    36. 36. Paradigm of your choice
    37. 37. Free and open source </li><ul><li>Implemented under an open source license. Freely usable and distributable, even for commercial use. </li></ul><li>Simplicity , Great first language
    38. 38. Availability (cross-platform)
    39. 39. Interactivity (interpreted language) </li></ul>
    40. 40. Why Python ??? <ul><li>GUI support – GUIs typically developed with Tk
    41. 41. Strong introspection capabilities
    42. 42. Intuitive object orientation
    43. 43. Natural expression of procedural code
    44. 44. Full modularity, supporting hierarchical packages
    45. 45. Exception­based error handling
    46. 46. The ability to be embedded within applications as a scripting interface
    47. 47. Scalable – can play nicely with other languages </li></ul>
    48. 48. <ul>Batteries Included </ul><ul><li>The Python standard library is very extensive </li><ul><li>regular expressions, codecs
    49. 49. date and time, collections, theads and mutexs
    50. 50. OS and shell level functions (mv, rm, ls)
    51. 51. Support for SQLite and Berkley databases
    52. 52. zlib, gzip, bz2, tarfile, csv, xml, md5, sha
    53. 53. logging, subprocess, email, json
    54. 54. httplib, imaplib, nntplib, smtplib
    55. 55. and much, much more ... </li></ul></ul>
    56. 56. Python Libraries <ul><li>Biopython - Bioinformatics
    57. 57. SciPy - Linear algebra, signal processing
    58. 58. NumPy - Fast compact multidimensional arrays
    59. 59. PyGame - Game Development
    60. 60. Visual Python - real-time 3D output
    61. 61. Django - High-level python Web framework
    62. 62. and much more ... </li></ul>
    63. 63. <ul>E.g. Projects with Python </ul><ul><li>Websites: Google, YouTube, Yahoo Groups & Maps, CIA.gov </li><ul><li>Appengine: http://code.google.com/appengine/
    64. 64. ” Google: Python has been an important part of Google since the beginning.”, Peter Norvig.
    65. 65. Python application servers and Python scripting to create the web UI for BigTable (their database project) </li></ul><li>Systems: NASA, LALN, CERN, Rackspace </li><ul><li>Nasa Nebula http://nebula.nasa.gov/about </li></ul><li>Games: Civilization 4, Quark (Quake Army Knife)
    66. 66. Mobile phones: Nokia S60 (Symbian), PythonCE
    67. 67. P2P: BitTorrent </li></ul>
    68. 68. Python Problems <ul><li>Scripting-like language and compiled and runtime - hence slower than C/C+ + and slightly slower than Java
    69. 69. Memory economy hard to achieve (high level data-structures)
    70. 70. Relies on a locking mechanism called the Global Interpreter Lock (GIL) in multi-threading </li></ul>
    71. 71. Turn Arounds <ul><li>Write extension libraries in C or C++
    72. 72. Use multiple processes instead of multiple threads
    73. 73. Use a different language
    74. 74. Use Stackless Python </li></ul>
    75. 75. Syntax Walkthroughs <ul><li>What is Python ???
    76. 76. Why Python ???
    77. 77. Syntax Walkthroughs
    78. 78. Linux and Python </li></ul>
    79. 79. Hello World <ul><li>Python 2.6 </li></ul><ul><li>Python 3.0 </li></ul>print (“Hello World”) print “Hello World”
    80. 80. Basics <ul><li>Numbers: </li><ul><li>Integers: 4 , 8 , 15, 16 , 23 , 42 ,108
    81. 81. Floating point: 4.23 , 42.8E-4
    82. 82. Complex Numbers: -5+4j , 2.3 – 4.6j </li></ul><li>Strings </li><ul><li>Immutable , Unicode by default
    83. 83. Single quotes, double quotes ,triple quotes (multiline)
    84. 84. 'Hello Word' , ”Hello World” , '''Hello World''' , ”””Hello World”””
    85. 85. Concatenation : 'What's ' 'your name?'
    86. 86. automatically converted in to &quot;What's your name?” </li></ul></ul>
    87. 87. Strings: format() >>>age = 25 >>>name = 'Swaroop' >>> print ( '{0} is {1} years old' .format(name, age)) Swaroop is 25 years old >>> '{0:.3}' .format( 1 / 3 ) '0.333' >>> '{0:_^11}' .format( 'hello' ) '___hello___' >>> '{name} wrote {book}' .format(name= 'Swaroop' , book= 'A Byte of Python' ) 'Swaroop wrote A Byte of Python'
    88. 88. Variables <ul><li>Naming identifiers: </li><ul><li>The first character be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ('_').
    89. 89. The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ('_') or digits (0-9).
    90. 90. Identifier names are case-sensitive. For example, myname and myName are not the same.
    91. 91. Valid identifier e.g.: i, __my_name, name_23, a1b2_c3, </li></ul></ul>resumÃÆâ€TMƢ€™Ãƒ¢â‚¬Å¡Ãƒâ€šÃ‚©_count. <ul><ul><li>invalid identifier e.g.: 2things, my-name </li></ul></ul>
    92. 92. Indentation <ul><li>Python uses whitespace to determine blocks of code </li></ul>def greet (person): if person == “Tim”: print (“Hello Master”) else : print (“Hello {name}”.format(name=person))
    93. 93. 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
    94. 94. Data Structures <ul><li>List </li><ul><li>Mutable data type, array-like
    95. 95. [1, 2, 4, “Hello”, False]
    96. 96. list.sort() ,list.append() ,len(list), list[i] </li></ul><li>Tuple </li><ul><li>Immutable data type, faster than lists
    97. 97. (1, 2, 3, “Hello”, False) </li></ul><li>Dictionary </li><ul><li>{42: “The answer”, “key”: “value”} </li></ul><li>Set </li><ul><li>([“list”, “of”, “values”]) </li></ul></ul>
    98. 98. Functions <ul><li>Order is important unless using the name
    99. 99. 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' )
    100. 100. 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 ))
    101. 101. 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 )
    102. 102. Modules <ul><li>Any python file is considered a module
    103. 103. 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' )
    104. 104. Modules #!/usr/bin/python # Filename: mymodule_demo.py import mymodule mymodule.sayhi() print ( 'Version' , mymodule.__version__) #!/usr/bin/python # Filename: mymodule.py def sayhi (): print ( 'Hi, this is mymodule speaking.' ) __version__ = '0.1' # End of mymodule.py
    105. 105. OOP class MyClass : &quot;&quot;&quot;This is a docstring.&quot;&quot;&quot; name = &quot;Eric&quot; def say (self): return ( 'My name is {0}' .format(name)) instance = MyClass() print instance.say()
    106. 106. OOP class Person : def __init__ (self, name): self.name = name def __del__ (self): print ( 'deleting this person' ,self.name) def sayHi (self): print ( 'Hello, my name is' , self.name) p = Person( 'Swaroop' ) p.sayHi() del p
    107. 107. OOP <ul><li>All class members (including the data members) are public and all the methods are virtual in Python.
    108. 108. Double undersocre prefix turns them private </li><ul><li>e.g.: __privatevar, </li></ul><li>Inheritance
    109. 109. MetaClasses </li></ul>class SchoolMember (metaclass=ABCMeta): @abstractmethod def tell (self): pass <ul><ul><li>class Teacher (SchoolMember): </li></ul></ul>
    110. 110. Input & Output #input something = input( 'Enter text: ' ) #output print (something)
    111. 111. Files myString = ”This is a test string” f = open( 'test.txt' , 'w' ) # open for 'w'riting f.write(myString) # write text to file f.close() # close the file f = open( 'test.txt' ) #read mode while True : line = f.readline() if len(line) == 0 : # Zero length indicates EOF break print (line, end= '' ) f.close() # close the file
    112. 112. Pickle import pickle shoplistfile = 'shoplist.data' shoplist = [ 'apple' , 'mango' , 'carrot' ] f = open(shoplistfile, 'wb' ) pickle.dump(shoplist, f) # dump the object to a file f.close() del shoplist # destroy the shoplist variable f = open(shoplistfile, 'rb' ) storedlist = pickle.load(f) # load the object from the file print (storedlist)
    113. 113. More … <ul><li>Exception Handling
    114. 114. Standard Library
    115. 115. Passing Tuples
    116. 116. Logging Module </li></ul>
    117. 117. Linux and Python <ul><li>What is Python ???
    118. 118. Why Python ???
    119. 119. Syntax Walkthroughs
    120. 120. Linux and Python </li></ul>
    121. 121. Linux and Python <ul><li>Installed by default in most distros
    122. 122. Various editors </li><ul><li>Text editors, IDLE , plugins for eclipse & Netbeans </li></ul><li>Embeddable in many applications as scripting interface </li><ul><li>Rhythmbox, Blender, OpenOffice, BitTorrent, ... </li></ul></ul>
    123. 123. Linux and Python ” Talk is cheap. Show me the code.” Linus Torvalds
    124. 124. Demos <ul><li>Backup Script
    125. 125. Simple XML Processing
    126. 126. Simple Spell Checker
    127. 127. Python Virus
    128. 128. Open Office Script
    129. 129. Rhythmbox python console
    130. 130. Basic Twitter client </li></ul>
    131. 131. Open Office macro script for python syntax Highlighing Demo #!usr/bin/python #Send a new Tweet from getpass import getpass import tweepy username = raw_input( 'Twitter username: ' ) password = getpass( 'Twitter password: ' ) basic_auth = tweepy.BasicAuthHandler(username, password) api = tweepy.API(basic_auth) api.update_status( &quot;Hello Twitter !!! I'm a Pythoneer&quot; )
    132. 132. More Resources <ul><li>http://wiki.python.org/moin/BeginnersGuide
    133. 133. http://www.python.org/doc/faq/
    134. 134. Learn Python in 10 minutes: http://www.poromenos.org/tutorials/python
    135. 135. Byte of Python: http://www.swaroopch.com/notes/Python
    136. 136. Dive Into Python: http://diveintopython.org/
    137. 137. Google </li></ul>
    138. 138. Any Questions ???
    139. 139. Thank You
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×