Welcome to the:  "Oh, no!  Yet another  programming language!" Python scripting Kickoff Andrea Gangemi December ...
What's cool in Python (IMHO) <ul><ul><li>A Free, easy to learn High Level OO Language  </li></ul></ul><ul><ul><li>JIT Inte...
History <ul><li>Guido Van Rossum  </li></ul><ul><li>Dutch Programmer and Monty Python fan </li></ul><ul><ul><li>0.9.0 (199...
Installing Python <ul><ul><li>GNU/Linux Systems: Usually already installed, just type &quot;python&quot; at the shell prom...
Python Shell <ul><li>To Exit: </li></ul><ul><li>Ctrl-D   In Unix: &quot;End of File&quot; </li></ul><ul><li>  </li></ul><u...
Variables <ul><ul><li>Variables are NOT declared i.e. type is defined when you assign the value for the first time. This a...
Compound data types (1/2) <ul><ul><li>List:  A collection of items whatever they are >>> list_example = [ 1, &quot;hellowo...
Compound data types (2/2) <ul><ul><li>Set:  an unordered collection with no duplicate elements. >>> basket = ['apple', 'or...
List Comprehension and operators <ul><ul><li>Operators:  </li></ul></ul><ul><ul><ul><li>Boolean comparison, Arithmetic ope...
Flow Control <ul><ul><li>if ,  for  and  while    Python does not uses parentheses to identify code blocks, indentation is...
SPAM Break
Functions <ul><li>def  function_name[(parameters)] : </li></ul><ul><ul><li>&quot;&quot;&quot;Optional document string&quot...
More about functions <ul><ul><li>Functions supports default operators. </li></ul></ul><ul><ul><li>Functions can also be ca...
Exception handling <ul><li>In order to nicely manage unexpected exception Python provides  try ,  except ,  finally  state...
Modules <ul><ul><li>Modules are files containing functions definitions and statements. </li></ul></ul><ul><ul><li>File nam...
Script Sample <ul><li>#!/usr/bin/env python def function_divide(numerator, denominator):     try:         result = numerat...
Some Interesting base modules <ul><ul><li>optparse powerful command line option parser </li></ul></ul><ul><ul><li>os OS ro...
Modules modules modules  Scripting scripting scripting... <ul><li>Most of the time spent to code in Python is to search fo...
Useful links <ul><ul><li>Python official documentation   </li></ul></ul><ul><ul><li>Learn to Program using Python, develop...
Thank you   Andrea Gangemi   email: andrea.gangemi@gmail.com tel: +39 340 5987091  skype: andreagangemi linkedIn: www.link...
Upcoming SlideShare
Loading in …5
×

Python scripting kick off

1,401 views

Published on

An introduction of Python language.
The presentation is intended to give a very basic overview of Python feature using an informal approach

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,401
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
45
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python scripting kick off

  1. 1. Welcome to the: &quot;Oh, no!  Yet another  programming language!&quot; Python scripting Kickoff Andrea Gangemi December 2008
  2. 2. What's cool in Python (IMHO) <ul><ul><li>A Free, easy to learn High Level OO Language </li></ul></ul><ul><ul><li>JIT Interpreter </li></ul></ul><ul><ul><li>Powerful standard library, string management... </li></ul></ul><ul><ul><li>Huge collection of external libraries and docs </li></ul></ul><ul><ul><li>Multiplatform </li></ul></ul><ul><ul><li>List Comprehension </li></ul></ul><ul><ul><li>Native Exception handling </li></ul></ul>
  3. 3. History <ul><li>Guido Van Rossum  </li></ul><ul><li>Dutch Programmer and Monty Python fan </li></ul><ul><ul><li>0.9.0 (1991) </li></ul></ul><ul><ul><ul><li>exception handling, list, dict  ... </li></ul></ul></ul><ul><ul><ul><li>running on Amoeba distributed OS </li></ul></ul></ul><ul><ul><li>1.x (1994) </li></ul></ul><ul><ul><ul><li>Lambda Calculus, Complex numbers ... </li></ul></ul></ul><ul><ul><ul><li>running on PC  </li></ul></ul></ul><ul><ul><ul><li>GPL license </li></ul></ul></ul><ul><ul><li>2.x (2000) </li></ul></ul><ul><ul><ul><li>List Comprehension ... </li></ul></ul></ul><ul><ul><ul><li>Running ... almost everywhere. </li></ul></ul></ul><ul><ul><li>3.x AKA Python 3000 or Py3k (3 Dec 2008) </li></ul></ul>
  4. 4. Installing Python <ul><ul><li>GNU/Linux Systems: Usually already installed, just type &quot;python&quot; at the shell prompt </li></ul></ul><ul><ul><li>Windows: </li></ul></ul><ul><ul><ul><li>http://www.python.org/download/  Official Site </li></ul></ul></ul><ul><ul><ul><li>http://www.cygwin.org/cygwin/  Cygwin </li></ul></ul></ul><ul><ul><ul><li>http://www.activestate.com  ActivePython </li></ul></ul></ul><ul><ul><li>For other OS or details: </li></ul></ul><ul><ul><ul><li>http://diveintopython.org  Dive into Python free book </li></ul></ul></ul><ul><ul><ul><li>http://www.google.com/search...  Google :)  </li></ul></ul></ul><ul><ul><li>You can also try Python on-line at: http://try-python.mired.org/ </li></ul></ul>
  5. 5. Python Shell <ul><li>To Exit: </li></ul><ul><li>Ctrl-D   In Unix: &quot;End of File&quot; </li></ul><ul><li>  </li></ul><ul><li>And Now for Something Completely Different ... </li></ul><ul><li>some practical examples </li></ul>
  6. 6. Variables <ul><ul><li>Variables are NOT declared i.e. type is defined when you assign the value for the first time. This also mean you have to assign a value before using it and some potential pitfalls. </li></ul></ul><ul><ul><li>Multiple Assignment >>> x = y = z = 42 </li></ul></ul><ul><ul><li>Python implements powerful string manipulation methods </li></ul></ul><ul><ul><ul><li>negative index </li></ul></ul></ul><ul><ul><ul><li>concatenation </li></ul></ul></ul><ul><ul><ul><li>slicing </li></ul></ul></ul><ul><ul><ul><li>strip, find </li></ul></ul></ul><ul><li>  </li></ul><ul><li>click here for further References about this and next slides </li></ul>
  7. 7. Compound data types (1/2) <ul><ul><li>List: A collection of items whatever they are >>> list_example = [ 1, &quot;helloworld&quot; , 3.14 , 42+12j ] >>> list_example [1, 'helloworld', 3.1400000000000001, (42+12j)] </li></ul></ul><ul><ul><ul><li>Can be sliced, nested, items can be inserted/removed </li></ul></ul></ul><ul><ul><ul><li>Lists have methods </li></ul></ul></ul><ul><ul><li>Tuples: a collection of items separated by comma >>> tuple_example =  1, &quot;helloworld&quot; , 3.14 , 42+12j >>> tuple_example (1, 'helloworld', 3.1400000000000001, (42+12j)) </li></ul></ul><ul><ul><ul><li>Faster than Lists but cannot be modified </li></ul></ul></ul><ul><ul><ul><li>Can be sliced (obtaining a new tuple) </li></ul></ul></ul><ul><ul><ul><li>Used for print formatting (Python <= 2.x) </li></ul></ul></ul><ul><ul><li>Tuples can be converted into list and viceversa </li></ul></ul>
  8. 8. Compound data types (2/2) <ul><ul><li>Set: an unordered collection with no duplicate elements. >>> basket = ['apple', 'orange', 'apple', 'pear', 'banana'] >>> fruit = set(basket) set(['orange', 'pear', 'apple', 'banana']) </li></ul></ul><ul><ul><ul><li>Useful for eliminate duplicates or membership testing </li></ul></ul></ul><ul><ul><li>Dictionaries: an unordered set of keys , which can be any immutable type. >>> tel = {'jack': 4098, 'sape': 4139} >>> tel['guido'] = 4127 >>> tel {'sape': 4139, 'guido': 4127, 'jack': 4098} >>> tel['jack'] 4098 </li></ul></ul><ul><ul><ul><li>  dict() can be used to build dictionary from key-value tuples </li></ul></ul></ul>
  9. 9. List Comprehension and operators <ul><ul><li>Operators: </li></ul></ul><ul><ul><ul><li>Boolean comparison, Arithmetic operation and Bit manipulations are similar to C language </li></ul></ul></ul><ul><ul><ul><li>Logical operations are: &quot;and&quot;, &quot;not&quot;, &quot;or&quot; </li></ul></ul></ul><ul><ul><li>List Comprehension: </li></ul></ul><ul><ul><li>Wikipedia : &quot;A list comprehension is a syntactic construct [...] for creating a list based on existing lists.  </li></ul></ul><ul><ul><li>  >>> vec = [2, 4, 6] >>> result = [(x, x**2) for x in vec] What results looks like?  </li></ul></ul><ul><ul><li>Try it! </li></ul></ul><ul><ul><li>Let's see how I used list comprehension in Barblery </li></ul></ul>
  10. 10. Flow Control <ul><ul><li>if , for and while  Python does not uses parentheses to identify code blocks, indentation is Python’s way of grouping statements. </li></ul></ul><ul><ul><ul><li>Coder is forced to write readable code (more or less) </li></ul></ul></ul><ul><ul><li>>>> for x in range (2,5): ...     print 'x is ' , x ... x is  2 x is  3 x is  4 </li></ul></ul><ul><li>Note the use of keyword &quot;in&quot; and builtin function range() </li></ul>
  11. 11. SPAM Break
  12. 12. Functions <ul><li>def function_name[(parameters)] : </li></ul><ul><ul><li>&quot;&quot;&quot;Optional document string&quot;&quot;&quot; </li></ul></ul><ul><ul><li># Code here </li></ul></ul><ul><ul><li># ... </li></ul></ul><ul><ul><li>[return value]   </li></ul></ul><ul><ul><li>  </li></ul></ul><ul><li>Example: </li></ul><ul><li>def makedir(stringdate):     dirname = stringdate[:4]+stringdate[5:7]+stringdate[8:10]     if os.path.exists(dirname) and os.path.isdir(dirname):         created = False     else:         os.mkdir(dirname)         created = True     return dirname,created </li></ul><ul><li>Let's comment together this function </li></ul>
  13. 13. More about functions <ul><ul><li>Functions supports default operators. </li></ul></ul><ul><ul><li>Functions can also be called using keyword arguments </li></ul></ul><ul><ul><li>End of function is detected by indentation. </li></ul></ul><ul><ul><li>return value can be a single value or a tuple. </li></ul></ul><ul><ul><li>To pass a tuple to a function requiring separate arguments use the &quot;*&quot; operator: >>> args = [3, 6] >>> range(*args) [3, 4, 5] </li></ul></ul><ul><li>  </li></ul>
  14. 14. Exception handling <ul><li>In order to nicely manage unexpected exception Python provides try , except , finally statements </li></ul><ul><li>import sys try:     s = raw_input('Enter something --> ') except EOFError:     print ' Why did you do an EOF on me?'     sys.exit() # exit the program except:     print ' Some error/exception occurred.'     # here, we are not exiting the program print 'Done' </li></ul><ul><li>If finally is present, it specifies a cleanup handler </li></ul><ul><li>  </li></ul>
  15. 15. Modules <ul><ul><li>Modules are files containing functions definitions and statements. </li></ul></ul><ul><ul><li>File name is module name and .py as extension. </li></ul></ul><ul><ul><li>Use &quot;import&quot; to import module  </li></ul></ul><ul><ul><li>You can import only some functions from the modules: from time import gmtime, strftime  </li></ul></ul><ul><ul><li>Modules always have a variable __name__  containing module name.  </li></ul></ul><ul><li>  </li></ul><ul><li>Let's see some real scripts... </li></ul><ul><li>  </li></ul><ul><li>Click here for a scripting tutorial </li></ul>
  16. 16. Script Sample <ul><li>#!/usr/bin/env python def function_divide(numerator, denominator):     try:         result = numerator / denominator         print 'x/y=', result     except ZeroDivisionError:         print 'Divide by zero'     except:         print 'Invalid operation' if __name__ == '__main__':     while True:         input_data = input('type 2 numbers separated by comma: ')         if len(input_data) == 2:             function_divide(*input_data)         else:             break         </li></ul>
  17. 17. Some Interesting base modules <ul><ul><li>optparse powerful command line option parser </li></ul></ul><ul><ul><li>os OS routines for Mac, NT or Posix </li></ul></ul><ul><ul><li>shutil Utility functions for copying files and directory trees. </li></ul></ul><ul><ul><li>time Time access and conversion </li></ul></ul><ul><ul><li>urllib2 URL access library </li></ul></ul><ul><ul><li>commands take a system command as a string and return output </li></ul></ul>
  18. 18. Modules modules modules Scripting scripting scripting... <ul><li>Most of the time spent to code in Python is to search for something that already does what you need... </li></ul>Python Serial Port Extension
  19. 19. Useful links <ul><ul><li>Python official documentation </li></ul></ul><ul><ul><li>Learn to Program using Python, developer.com  </li></ul></ul><ul><ul><li>Dive into Python   </li></ul></ul><ul><ul><li>Thinking in Python  </li></ul></ul><ul><ul><li>Python Istantaneo  (Italian) </li></ul></ul><ul><ul><li>IBM Discover Python </li></ul></ul><ul><ul><li>A byte of Python  </li></ul></ul><ul><ul><li>urllib2 - The Missing Manual  </li></ul></ul><ul><ul><li>Python: 50 modules for all needs  </li></ul></ul><ul><ul><li>AND ... </li></ul></ul><ul><ul><li>Monty Python on wikipedia </li></ul></ul>
  20. 20. Thank you   Andrea Gangemi   email: andrea.gangemi@gmail.com tel: +39 340 5987091  skype: andreagangemi linkedIn: www.linkedin.com/in/andreagangemi

×