Python in A Nutshell: Python's Whys & Hows
Python in A Nutshell: Python's Whys & Hows
Python in A Nutshell: Python's Whys & Hows
What is Python
Python in a Python in a
Nutshell Nutshell
Python’s Basics Python’s Basics – it best fits the role of scripting language.
functions
Overview of the Basic Concepts functions
Readable Code Readable Code Python supports multiple programming paradigms
Objects Objects
Indenting Code Indenting Code – imperative (functions, state, . . . );
Exceptions Exceptions
Running Scripts
Walter Cazzola Running Scripts – object-oriented/based (objects, methods, inheritance, . . . );
References References – functional (lambda abstractions, generators, dynamic typing, . . . ).
Dipartimento di Informatica e Comunicazione
Università degli Studi di Milano Python is
e-mail: cazzola@dico.unimi.it – interpreted, dynamic typed and object-based;
– open-source.
Slide 1 of 14 Slide 2 of 14
Python in a
Nutshell
We are considering Python 3+ Python in a
Nutshell
The python shell can be used to get interactive help.
Walter Cazzola – versions >3 is incompatible with previous versions; Walter Cazzola [23:35]cazzola@ulik:~/esercizi-pa>python3
– version 2.6 is the current version. Python 3.1.1 (r311:74480, Aug 17 2009, 21:52:33)
[GCC 4.4.1] on linux2
Python Python
Type "help", "copyright", "credits" or "license" for more information.
Whys
A python program can be: Whys
>>> help()
Hows Hows
Python’s Basics
– edited and run through the interpreter Python’s Basics Welcome to Python 3.1! This is the online help utility.
functions functions
Readable Code
[23:31]cazzola@ulik:~/esercizi-pa>vim hello.py Readable Code If this is your first time using Python, you should definitely check out
Objects
[23:32]cazzola@ulik:~/esercizi-pa>cat hello.py Objects the tutorial on the Internet at http://docs.python.org/tutorial/.
Indenting Code
print("Hello World!!!") Indenting Code
Exceptions
[23:32]cazzola@ulik:~/esercizi-pa>python3 hello.py Exceptions Enter the name of any module, keyword, or topic to get help on writing
Running Scripts
Hello World!!! Running Scripts Python programs and using Python modules. To quit this help utility and
[23:32]cazzola@ulik:~/esercizi-pa> return to the interpreter, just type "quit".
References References
To get a list of available modules, keywords, or topics, type "modules",
– edited in the python shell and executed step-by-step by the shell. "keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
[23:34]cazzola@ulik:~/esercizi-pa>python3 such as "spam", type "modules spam".
Python 3.1.1 (r311:74480, Aug 17 2009, 21:52:33)
[GCC 4.4.1] on linux2 help> ^D
Type help, copyright, credits or license for more information. You are now leaving help and returning to the Python interpreter.
>>> print("Hello World!!!") If you want to ask for help on a particular object directly from the
Hello World!!! interpreter, you can type "help(object)". Executing "help(’string’)"
>>> has the same effect as typing a particular string at the help> prompt.
[23:35]cazzola@ulik:~/esercizi-pa> >>> ^D
[23:40]cazzola@ulik:~/esercizi-pa>
Slide 3 of 14 Slide 4 of 14
Overview of the Basic Concepts Overview of the Basic Concepts
Our First Python Program Declaring Functions
Python in a Python in a
Nutshell humanize.py Nutshell
Slide 5 of 14 Slide 6 of 14
Python in a Python in a
Nutshell Nutshell
Slide 7 of 14 Slide 8 of 14
Overview of the Basic Concepts Overview of the Basic Concepts
Everything is an Object Everything is an Object (Cont’d)
Python in a Python in a
Nutshell Everything in Python is an Object, functions included. Nutshell
Python in a Python in a
Nutshell Nutshell Exceptions are Anomaly Situations
Walter Cazzola Walter Cazzola – C encourages the use of return codes which you check;
No explicit block delimiters
– the only delimiter is a column (’:’) and the code indentation. – Python encourages the use of exceptions which you handles.
Python Python
Whys Whys
Hows
def approximate_size(size, a_kilobyte_is_1024_bytes=True): Hows Raising Exceptions
Python’s Basics if size < 0: Python’s Basics – the raise statement is used to raise an exception as in
functions raise ValueError(’number must be non-negative’) functions
Readable Code multiple = 1024 if a_kilobyte_is_1024_bytes else 1000 Readable Code raise ValueError(’number must be non-negative’)
Objects for suffix in SUFFIXES[multiple]: Objects
Indenting Code size /= multiple Indenting Code – syntax recalls function calls: raise statement followed by an excep-
Exceptions if size < multiple: Exceptions
Running Scripts return ’{0:.1f} {1}’.format(size, suffix) Running Scripts
tion name with an optional argument;
raise ValueError(’number too large’)
References References – exceptions are realized by classes.
Note No need to list the exceptions in the function declaration.
– code blocks (i.e., functions, if statements, loops, . . . ) are defined by Handling Exceptions
their indentation;
– an exception is handled by a try . . . except block.
– white spaces and tabs are relevant: use them consistently;
try:
– indentation is checked by the compiler. from lxml import etree
except ImportError:
import xml.etree.ElementTree as etree
Slide 11 of 14 Slide 12 of 14
Overview of the Basic Concepts References
Running Scripts
Python in a Python in a
Nutshell Nutshell
Look, again, at the bottom of the humanize.py program:
Walter Cazzola Walter Cazzola
1 if __name__ == ’__main__’:
2 print(approximate_size(1000000000000, False))
Python
Whys
3 print(approximate_size(1000000000000))
Python
Whys
I Jennifer Campbell, Paul Gries, Jason Montojo, and Greg Wilson.
Hows Hows
Practical Programming: An Introduction to Computer Science Using
Modules are Objects
Python’s Basics Python’s Basics Python.
functions – they have a built-in attribute __name__ functions
Readable Code Readable Code The Pragmatic Bookshelf, second edition, 2009.
I
Objects Objects
[18:29]cazzola@ulik:~/esercizi-pa>python3
Indenting Code Indenting Code
Mark Lutz.
Exceptions >>> import humanize Exceptions
Running Scripts >>> humanize.__name__ Running Scripts Learning Python.
’humanize’
References
>>> ^D
References O’Reilly, third edition, November 2007.
I
[18:30]cazzola@ulik:~/esercizi-pa>python3 humanize.py
1.0 TB Mark Pilgrim.
931.3 GiB
[18:30]cazzola@ulik:~/esercizi-pa> Dive into Python 3.
Apress*, 2009.
The value of __name__ depends on how you call it
– if imported it contains the name of the file without path and
extension;
– if run as a stand-alone program it contains the "main" string.
Slide 13 of 14 Slide 14 of 14