Introduction

The target of this course is to get familiar with the Python programming language, in particular with its practical use for scientific and engineering purposes.

Why Python?

Python is nowadays one the the most pospular languages (ranked 8th on Tiobe index in August 2013). In the scientific and engineering community, Python is becomming more and more significant---deservedly.

Fundamental properties of Python

  • Dynamically typed language
  • Strongly typed language
  • Interpreted language, running in a virtual machine
  • Garbage collector based on reference counting
  • Extensive standard library
  • Free license (Python Software Foundation License -- PSFL).
In [2]:
a = 1  # a dynamically typed variable
print("a = %i, type(a) = %s" % (a, type(a)))
a = eval("a + 1")  # eval evaluates an arbitrary string
print("a = %i" % a)
try:
    a + "1"  # error - Python is strongly typed
except Exception as e:
    print("Error: %s" % e)
a = 1, type(a) = <class 'int'>
a = 2
Error: unsupported operand type(s) for +: 'int' and 'str'

Python philosophy

The philosophy (Zen) of Python is based on

  • clear, simple and readable syntax (source code is more often read than written),
  • multiple programming styles (paradigms): object oriented /structured / functional / aspect oriented programming,
  • extensibility,
  • strong conventions, often more important than the syntax.

More in PEP 20 -- The Zen of Python

This is "Hello world! in Python

In [3]:
print("Hello world!")
Hello world!

History

The history goes back to 1980. The implementation of Python began in 1989 by Guido Van Rossum. He is still the main figure, the so called Benevolent Dictator for Life---BDFL, of the Python community. He used to work in Google and now has a job with Dropbox. More about the history on e.g. wikipedia.

Python in dates:

  • Python 1.0 - 1994
    • Python 1.6 - 2000
  • Python 2.0 - 2000
    • Python 2.7 - 2010
  • Python 3.0 2008
    • Python 3.3 - 2012

Python 2 vs. 3

Pythons 3 tries to fix certain issues, which came out after many years of using Python 2. See http://docs.python.org/3.0/whatsnew/3.0.html. Some of these changes create incompatibilities between Python 2 and 3. The most important changes concern

  • print is a function in Pythonu 3 --> one must use print(...).
  • The / division operator in Python 3 is always floating point --> 1 / 2 = 0.5.
  • raw_input is renamed to input.

Pro co nejplynulejší přechod jsou tyto rozdíly zpětně implementovány v Pythonu 2.7, a to buď přímo nebo prostřednictvím modulu future. Dále je k dispozici program 2to3, který upozorňuje na problémy ve zdrojových kódech.

V tomto kurzu používáme Python 2.7, budeme se ale snažit o kompatibilitu s Pythonem 3, případně na možné rozdíly upozorňovat. Důvodem jsou větší zkušenosti a donedávna nedostupnost důležitých modulů pro Python 3.

Další Pythony

Python jakožto jazyk je jeden, existuje však více implementací jeho interpreteru (t.j. programu, který zpracovává a vykonává příkazy). Referenčním interpreterem je v C napsaný CPython, dostupný z www.python.org. Ostatní implementace se k němu vztahují a snaží se o kompatibilitu (bývají na úrovni 2.6/2.7), nabízejí vždy něco navíc. Namátkou:

  • Jython - implementace Pythonu v Javě, která umožňuje používat objekty (a knihovny) Javy z Pythonu
  • IronPython - implementace Pythonu pro .NET, která umožňuje využívat možnosti prostředí .NET
  • PyPy - implementace Pythonu v... Pythonu

Celkem přehledné vysvětlení toho, co je který "Python" zač, nalezenete v článku Why are there so many Pythons?.

Comments

Comments powered by Disqus