Python
Session 1: introduction to Python
José Dapena Paz <jdapena@igalia.com>
Sessions
● Session 1:
– Presentation of language, brief history and practical
examples of language structures.
● Session 2:
– Important projects
– Standard libraries
– Other modules
First session contents
● ¿What's Python?
– Brief history
– Properties
– Documentation ¿Where?
● Introduction to the language
– Installation and usage
– Practical examples
¿What's Python?
● ¿What's Python?
– Brief history
– Properties
– Documentation ¿Where?
● Introduction to the language
– Installation and usage
– Practical examples
¿What's Python?
● High level language
● Author: Guido Van Rossum (first release in
1991)
● Emphasis on readability.
● Priority: easy for programmer, then, computer.
● Minimalist language. Wide libraries.
History
● Author: Guido Van Rossum. CWI.
● Begins Python at the end of 80's. Inspired in
ABC language.
● First release in 1991.
● Guido Van Rossum: Benevolent Dictator For
Life
Cronology
● 1991: 1st release. Classes, inheritance,
exceptions, modules and basic types list,
dict y str
● 1994: 1.0. Basic functional programming
(lambda methods).
● 2001: 1.6.1 y 2.1. GPL compatible license.
● Python 2.2. All types are unified. All are classes.
● Futurr: Python 3000 (3.0) in 2008.
Python is free software
● From Python 2.2, Python Foundation Software
License, GPL-compatible.
● All code is property of Python Foundation.
● Python community show a very high activity:
– Hundreds of modules/extensions available.
– Lots of software available implemented in Python.
– Python is the language of 2007 (language TIOBE,
Python had biggest increase of usage).
Documentation
● Python is very well documented
http://docs.python.org
● Tutorial: http://docs.python.org/tut/
● Language reference: (for language lawyers):
http://docs.python.org/ref/
● Library reference: http://docs.python.org/lib/
First session contents
● ¿What's Python?
– Brief history
– Properties
– Documentation ¿Where?
● Introduction to the language
– Installation and usage
– Practical examples
Installation and running
In ubuntu, install Python package:
$ sudo apt-get install python
Very useful to install online documentation:
$ sudo apt-get install python-doc
Python shell
● Python is an interpreted language.
● Modes:
– Interactive
– File
– Command line
– Executable
Modo interactivo
Just run Python, and add commands and
statements in the shell
$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu
4.1.2-16ubuntu2)] on linux2
Type quot;helpquot;, quot;copyrightquot;, quot;creditsquot; or quot;licensequot; for more
information.
>>> 1+1
2
>>> a = ”hola”
>>> a
'hola'
>>> print a
hola
Shell mode help
Whith dir () command we get the methods
available.
>>> dir (a) # With instances
>>> dir (list) # With classes
With help () command we get the help of a class
or instance.
>>> help (a) # With instances (no strings)
>>> help (list) # With classes
Python as a calculator
● Basic types:
– Integers
– Floats and complex numbers
– Strings
● Asign
● _ variable
● Lists. Ranges, asigns by range, operators.
Control flow structures
● While. Indentation in Python
● If, If-Elif-Else
● For, range.
● Break, continue, else, pass
● Functions. Definition.
● Classes
More flow control structures
● List methods. The list as a queue and as a
stack.
● Map, reduce, filter
● List comprehensions
● Del
● Other sequences: tuples. Dictionaries
● Looping
● Conditions, conditions in sequences.
Modules
● Modules reference
● Examples: Files api (File objects), http api
(urllib2)