On Python language How to use Python Syntax of Python Types in Python...
Transcript of On Python language How to use Python Syntax of Python Types in Python...
On Python languageHow to use Python
Syntax of PythonTypes in Python
Outline
1 On Python language
2 How to use Python
3 Syntax of Python
4 Types in PythonSimple types in PythonCollectionsProcessing collectionsStringsTips
Marcin Młotkowski Object–oriented programming 1 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
The beginnings of Pythons
90 — CWI Amsterdam, Guido van Rossum
Marcin Młotkowski Object–oriented programming 2 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Current state
Python Software Foundation (PSF)
Marcin Młotkowski Object–oriented programming 3 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Current version
2.* (2.4, 2.6, 2.7.2)
3.* (3.2.2)
Marcin Młotkowski Object–oriented programming 4 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Why I like Python
Simple and clean syntax:
few keywords and operators;
indentation is obligatory
factorial
def fact(x):if x == 0:
return 1else :
return x * fact(x-1)
Marcin Młotkowski Object–oriented programming 5 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Why Python is so cool
Styles of programming
structural programming
object–oriented programming
functional programming
Marcin Młotkowski Object–oriented programming 6 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Why Python is so cool
Built-in types
Lists
vec = [1, 2, 3]doubled_vec = [ 2*e for e in vec]
Dictionaries
phone = { ’chris’ : 235711, ’Susie’ : 246810 }print tel[’susie’]
Marcin Młotkowski Object–oriented programming 7 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Why Python is so cool
Batteries included
I/O libraries
regular expressions
HTTP, HTML, XML
GUIs: pyGTK, wxPython, Tkinter, PythonWin
...
...
Marcin Młotkowski Object–oriented programming 8 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Other features of Python
Dynamic type system
>>> 2 + "two"Traceback (most recent call last):File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: ’int’and ’str’>>>
Marcin Młotkowski Object–oriented programming 9 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Applications of Python
Projects
System tools (RedHat)
NASA
ZOPE/PLONE
http://www.python.org/about/success/
Marcin Młotkowski Object–oriented programming 10 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Outline
1 On Python language
2 How to use Python
3 Syntax of Python
4 Types in PythonSimple types in PythonCollectionsProcessing collectionsStringsTips
Marcin Młotkowski Object–oriented programming 11 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Interactive mode
$ python>>> 2+24>>> [1,2,3][-1:][3]Ctrl-d$
Marcin Młotkowski Object–oriented programming 12 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Execute a file
$ python a_file.py
What’s happened
1 program compilation (inc. syntax checking)
2 sometimes a file *.pyc is created
3 execute a program
Marcin Młotkowski Object–oriented programming 13 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Execute a file
$ python a_file.py
What’s happened
1 program compilation (inc. syntax checking)
2 sometimes a file *.pyc is created
3 execute a program
Marcin Młotkowski Object–oriented programming 13 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Execute a file
$ python a_file.py
What’s happened
1 program compilation (inc. syntax checking)
2 sometimes a file *.pyc is created
3 execute a program
Marcin Młotkowski Object–oriented programming 13 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Outline
1 On Python language
2 How to use Python
3 Syntax of Python
4 Types in PythonSimple types in PythonCollectionsProcessing collectionsStringsTips
Marcin Młotkowski Object–oriented programming 14 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Syntax
Assignment
four = 2 + 2x = y = z = 0name = ’Pyt’ + ’hon’primes = [2, 3, 5, 7]a, b = b, a+b
Marcin Młotkowski Object–oriented programming 15 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Syntax
Conditional statement
x = 4if x % 2 == 0:print "even"print "x = ", 2
x = 4if x % 2 == 0:print "even"
print "x = ", 2
Marcin Młotkowski Object–oriented programming 16 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Syntax
Conditional statement
x = 400if x % 2 == 0:if x > 100: print "large even"elif 10 < x <= 100:print "small even"
else:print "tiny even"
Marcin Młotkowski Object–oriented programming 17 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Logic expressions
false: 0, False, None, [],
true: True, nonempty values
conjunctions: and, or, not
operators: ==, !=, 1 < x < 2
Marcin Młotkowski Object–oriented programming 18 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Logic expressions
false: 0, False, None, [],
true: True, nonempty values
conjunctions: and, or, not
operators: ==, !=, 1 < x < 2
Marcin Młotkowski Object–oriented programming 18 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Logic expressions
false: 0, False, None, [],
true: True, nonempty values
conjunctions: and, or, not
operators: ==, !=, 1 < x < 2
Marcin Młotkowski Object–oriented programming 18 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Logic expressions
false: 0, False, None, [],
true: True, nonempty values
conjunctions: and, or, not
operators: ==, !=, 1 < x < 2
Marcin Młotkowski Object–oriented programming 18 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Loop statement
A statement while
a, b = 0, 1while b < 10:print ba, b = b, a + b
Marcin Młotkowski Object–oriented programming 19 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Loop statement
for in
a = [1,2,3,4]for e in a:print e
print "end"
Marcin Młotkowski Object–oriented programming 20 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Loop statements
for in
sum = 0for i in range(100):sum = sum + i
print "sum=", sum
Marcin Młotkowski Object–oriented programming 21 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Other statements
break and continue
empty statement: pass
while (True): pass
Marcin Młotkowski Object–oriented programming 22 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Functions
def foo(arg1, arg2=1, arg3=[3]):print arg1, arg2, arg3return 4
foo("one", 2)print foo(1, 2, 3)
Marcin Młotkowski Object–oriented programming 23 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Comments
def fun (arg):""" This is very important functionUse with high esteem"""
# end if empty argumentif arg == None: returnreturn arg
Marcin Młotkowski Object–oriented programming 24 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Input/output
Python 2.*
print "Hello world"x = input("Type x: ")y = input("Type y: ")print "x =", x, " y =", y
Python 3.0
print("Hello world")x = input("Type x: ")y = input("Type y: ")print("x =", x, " y =", y)
Marcin Młotkowski Object–oriented programming 25 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Input/output
Python 2.*
print "Hello world"x = input("Type x: ")y = input("Type y: ")print "x =", x, " y =", y
Python 3.0
print("Hello world")x = input("Type x: ")y = input("Type y: ")print("x =", x, " y =", y)
Marcin Młotkowski Object–oriented programming 25 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Tips
Editors with syntax highlighting: vim, gedit, emacs
Tools
idlePythonCard/codeEditor
"Executable"files and national characters:
file.py
#!/usr/bin/python# -*- coding: iso-8859-2 -*-
Marcin Młotkowski Object–oriented programming 26 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Tips
Editors with syntax highlighting: vim, gedit, emacs
Tools
idlePythonCard/codeEditor
"Executable"files and national characters:
file.py
#!/usr/bin/python# -*- coding: iso-8859-2 -*-
Marcin Młotkowski Object–oriented programming 26 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Tips
Editors with syntax highlighting: vim, gedit, emacs
Tools
idlePythonCard/codeEditor
"Executable"files and national characters:
file.py
#!/usr/bin/python# -*- coding: iso-8859-2 -*-
Marcin Młotkowski Object–oriented programming 26 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Why "Python"?
Marcin Młotkowski Object–oriented programming 27 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Further reading
www.python.org
"Dive into Python", Mark Pilgrim
...
Marcin Młotkowski Object–oriented programming 28 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Outline
1 On Python language
2 How to use Python
3 Syntax of Python
4 Types in PythonSimple types in PythonCollectionsProcessing collectionsStringsTips
Marcin Młotkowski Object–oriented programming 29 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Simple types
simple types: integers, floats, booleans
sequential types: strings, lists, tuples
Marcin Młotkowski Object–oriented programming 30 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
”Simple” integers
Range: [−sys.maxint − 1, sys.maxint]
Operators: +, - , /, //, %, **
Standard functions: abs(x), divmod(x, y) = (x // y, x % y)
Warning
Dividing integer by integer gives integer
4//3 = 1
Marcin Młotkowski Object–oriented programming 31 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Integers and conversions
(1.0 ∗ 4)//3 = 1.0
(1.0 ∗ 4)/3 = 1.33333333333333
float(4)/3 = 4/float(3)
Marcin Młotkowski Object–oriented programming 32 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Large integers
Example
>>> 1000 ** 101000000000000000000000000000000L>>> long(100)100L
Marcin Młotkowski Object–oriented programming 33 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Constants
Integers0x123, 0x123456789L, 0123, 06789L
Floats3.14 10. .002 .271e1
Marcin Młotkowski Object–oriented programming 34 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Complex numbers
Literals and expressions1 + 3jx + 12jcomplex(x,0)
Operatorsx.real ∗ ∗2 + y.imag ∗ ∗2
Marcin Młotkowski Object–oriented programming 35 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Standard numerical libraries
math
import mathprint math.cos(3.14)
random
from random import *print random()
Marcin Młotkowski Object–oriented programming 36 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Examples
Lists: [12,3]
Strings: "abc", ’def’, u’Zazółc zółta jazn’
tuples: (1, ”one”, (1, 2+3j, 0x4))
dictionaries
Sets
Marcin Młotkowski Object–oriented programming 37 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Tuple
brown = 165, 42, 42NavyBlue = (0,0,128)htmlColor = { ’turquoise’ : (64,224,208), ’NavyBlue’ :NavyBlue }r, g, b = htmlCol[’NavyBlue’]
Marcin Młotkowski Object–oriented programming 38 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Reminder
assignment
(
a, b
)
=
(
1, 2
)
Marcin Młotkowski Object–oriented programming 39 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Reminder
assignment
(a, b)= (1, 2)
Marcin Młotkowski Object–oriented programming 39 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Inclusion operator
in
’bc’ in ’abcd’4 not in [2, 3, 5, 7, 11]’pi’ in { ’pi’ : 3.1415, ’e’ : 2.7182 }
Marcin Młotkowski Object–oriented programming 40 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Collection operators
+
>>> [ ’one’, 2, 3.0 ] + [ 0x4, 05 ][’one’, 2, 3.0, 4, 5]>>> (’one’, 2, 3.0) + (0x4, 05)(’one’, 2, 3.0, 4, 5)
Marcin Młotkowski Object–oriented programming 41 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Size of collection
len
len( [’one’, 2, 3.0] )len( {’one’ : 1, ’two’ : 2 } )len( (1, 2, 3) )
Marcin Młotkowski Object–oriented programming 42 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3
’abcd’[1:3] = ’bc’(1, 2, 3)[1:] = (2, 3)(1,2,3)[:1] = (1, )’Python’[:-1] = ’Pytho’’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3’abcd’[1:3] = ’bc’
(1, 2, 3)[1:] = (2, 3)(1,2,3)[:1] = (1, )’Python’[:-1] = ’Pytho’’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3’abcd’[1:3] = ’bc’(1, 2, 3)[1:] = (2, 3)
(1,2,3)[:1] = (1, )’Python’[:-1] = ’Pytho’’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3’abcd’[1:3] = ’bc’(1, 2, 3)[1:] = (2, 3)(1,2,3)[:1] = (1, )
’Python’[:-1] = ’Pytho’’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3’abcd’[1:3] = ’bc’(1, 2, 3)[1:] = (2, 3)(1,2,3)[:1] = (1, )’Python’[:-1] = ’Pytho’
’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Elements of collections
[1, 2, 3][2] = 3’abcd’[1:3] = ’bc’(1, 2, 3)[1:] = (2, 3)(1,2,3)[:1] = (1, )’Python’[:-1] = ’Pytho’’Python’[-1:] = ’Python’[-1] = ’n’
Marcin Młotkowski Object–oriented programming 43 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Slicing
>>> ’informatics’[::3]’ioac’
Marcin Młotkowski Object–oriented programming 44 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Iterations over collections
x = [1,2,3]y = [4,5,6]prod = 0for i in range(len(x)):
prod += x[i] * y[i]
Marcin Młotkowski Object–oriented programming 45 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
List processing
x = [1,2,3]y = [4,5,6]prod = 0for i, v in enumerate(x):
prod += v * y[i]print prod
Marcin Młotkowski Object–oriented programming 46 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
List processing, other implementation
x = [1,2,3]y = [4,5,6]prod = 0for a, b in zip(x, y):
prod += a * bprint prod
Marcin Młotkowski Object–oriented programming 47 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Dictionary processing
iteration
dict = { ’uno’ : 1, ’duo’ : 2, ’tre’: 3 }for key, val in dict.iteritems():
print key, "=", val
Marcin Młotkowski Object–oriented programming 48 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Constants
’Alice has a cat’“Alice has a cat”
Unicode
uZazółc zółta jazn""Zazółc zółta jazn"
Unicode
len(u"zółty") == 5len("zółty") == 8
Long strings
"""This is amultiline string""""
Marcin Młotkowski Object–oriented programming 49 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Constants
’Alice has a cat’“Alice has a cat”
Unicode
uZazółc zółta jazn""Zazółc zółta jazn"
Unicode
len(u"zółty") == 5len("zółty") == 8
Long strings
"""This is amultiline string""""
Marcin Młotkowski Object–oriented programming 49 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Constants
’Alice has a cat’“Alice has a cat”
Unicode
uZazółc zółta jazn""Zazółc zółta jazn"
Unicode
len(u"zółty") == 5len("zółty") == 8
Long strings
"""This is amultiline string""""
Marcin Młotkowski Object–oriented programming 49 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Constants
’Alice has a cat’“Alice has a cat”
Unicode
uZazółc zółta jazn""Zazółc zółta jazn"
Unicode
len(u"zółty") == 5len("zółty") == 8
Long strings
"""This is amultiline string""""
Marcin Młotkowski Object–oriented programming 49 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
Strings
Strings are collections
’raw’ strings: r’abcd\n’
String continuation:"This is a \n\very long string\n"
A lot of library functions
Strings are immutable, ie. ’abc’[1] = ’d’
Marcin Młotkowski Object–oriented programming 50 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
String formatting
Operator %
print "%i + %i = %i\n" % (2, 2, 2+2)
dict = { ’two’ : 2, ’four’ : 4 }print "%(two)s + %(two)s = %(four)s\n" % dict
Marcin Młotkowski Object–oriented programming 51 / 52
On Python languageHow to use Python
Syntax of PythonTypes in Python
Simple types in PythonCollectionsProcessing collectionsStringsTips
First aid
Interactive mode
>>> type(3.1415)<type ’float’>>>> dir(float)....>>> dir(3.1415)....>>> float.__doc__
Marcin Młotkowski Object–oriented programming 52 / 52