python_intro_2 - from python import lecture A gentle...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
from python import lecture A gentle introduction to the python programming language Muli Ben-Yehuda [email protected] IBM Haifa Research Labs from python import lecture, Haifux July 2003 – p. 1/21
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
what is python? Python is procedural object-oriented functional interactive dynamic Indentation is meaningful. (Almost) everything is an object. Object attributes can be added/removed at run time. There is one way to do it. Interpreted, not compiled (to native machine code). from python import lecture, Haifux July 2003 – p. 2/21
Background image of page 2
history 1990: Python invented by Guido van Rossum. 1991: Python announced on usenet, first public release. Python was really named after Monty Python. Free (as in speech) from the beginning, nowdays released under a GPL compatible license. Latest version: Python 2.3b2 from python import lecture, Haifux July 2003 – p. 3/21
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
variables numbers: 1, 2, 3.0, (3+9j), complex(7,9) strings: "a", ’a’, """aaa""" a = "a", b = "b", c = a + b + a*3 # a,b,c are strings d = c[3] # d == ’a’ e = c[0:1] # e == ’a’, the slice of c from 0 to 1 # strings are immutable (cannot be changed) e[0] = ’g’ # error len(e) # 1 # len is a built-in - don’t name your variables len >>> print len <built-in function len> >>> len = 1 >>> len(3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: ’int’ object is not callable from python import lecture, Haifux July 2003 – p. 4/21
Background image of page 4
lists lists: [1,3], [’a’, 7, 9L, 7j] # L is ’long’, j is for complex # lists are mutable (can be changed) lst = [1,2] lst[1] = 3 # lst == [1,3] #lists can be nested lst = [1, [’a’, ’b’], []] # more on lists: append, extend, insert, remove, pop, index, count, sort, reverse # list comprehensions [x for x in range(4) if x < 2] lst = [x*2 for x in range(1, 100) if x%2] >>> len(lst), lst[-1] (50, 198) from python import lecture, Haifux July 2003 – p. 5/21
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
math fun >>> # Fibonacci series: ... # the sum of two elements defines the next ... a, b = 0, 1 >>> while b < 10: ... print b, ... (a, b) = (b, a+b) 1 1 2 3 5 8 >>> # number of combinations of k items out of n >>> # first, we need a factorial function >>> fac = lambda n:[1,0][n>0] or fac(n-1)*n >>> def comb(n, k): ... return (fac(n) / (fac(n-k) * (fac(k)))) >>> # sanity check >>> comb(4, 2) 6 >>> comb(3, 0) 1 from python import lecture, Haifux July 2003 – p. 6/21
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 452 taught by Professor Cheng during the Fall '05 term at Michigan State University.

Page1 / 21

python_intro_2 - from python import lecture A gentle...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online