Personal tools
You are here: Home Learning Python

Learning Python

An introduction to learning Python

man python

"Python is an interpreted, interactive, object-oriented programming language  that  combines  remarkable power with very clear syntax."

 

Wikipedia

"Python is a multi-paradigm programming language (functional, object oriented and imperative) which has a fully dynamic type system and uses automatic memory management; it is thus similar to Perl, Ruby, Scheme, and Tcl."

 

History

  • Written by Guido van Rossum
  • "Benevolent Dictator for Life" (BDFL)
  • Named after Monty Python

 

Versions

  • 1991 - 0.9.0 on alt.sources
  • 1994 - 1.0
  • 2000 - 2.0
  • Current - 2.5.1

 

Getting Started - Installation

 

If it's OO and functional, learn

  • Variable declaration and assignment
  • Math and boolean statements
  • String manipulations
  • Input and Output
  • Functions and flow control
  • Classes and Data Structures

 

Interactive Interpreter

  •  Just run 'python'
  • Can declare and interact with objects
  • Replaces "blah.sh" "foo.c" and "test.php"
  • Enhanced Python shells like iPython

 

Keywords

and del
from
not
while
as
elif
global
or
with
assert
else
if
pass
yield
break
except
import
print
class
exec
in
raise
continue
finally
is
return
def
for
lambda
try





 

 

Operators

  • Math: **, *, /, %, +, -
  • Comparisons: <, <=, >, >=, !=, ==
  • Logical: is, in, not, and, or
  • Bitwise: <<, >>, & , ^, |
  • More on operators

 

Output

  • print string
  • print string,
  • print base_type
  • Most C formats work
>>> print 'PI is approximately %5.3f.' % math.pi
PI is approximately 3.142.

String Literals

>>> s1 = '"spam and eggs", he said\n'
>>> s2 = "'spam and eggs', he said\n"
>>> s3 = """spam
...  and
...  eggs"""
>>> print s1, s2, s3
"spam and eggs", he said
'spam and eggs', he said
spam
 and
 eggs

Some String Methods

  • upper(), lower(), swapcase(), title(), capitalize()
  • split(), strip(), find(substr), replace(old, new)
  • concatenation: s1 + s2
  • More

 

Input

  • name = raw_input("Enter your name: ")
  • py_exp = input("Enter a python expression: ")
  • file = open('my_file.txt', 'w')
    • file.read(), file.readline(), file.seek(2), file.write(''), file.close()

 

Data Structures

  • my_list = ['a', 'b', 2]
  • my_tuple = a, b, 2
  • my_set = set(my_list)
  • my_dictionary = {'key1': value}
  • Sequences
    • strings, Unicode strings, lists, tuples, buffers, and xrange objects

 

Flow Control and Functions

  • for i in range(5): print i
  • while count < 50: print "count is " + repr(count)
  • if a > b: print "a is greater than b"
  • def get_42(): return 42

 

Syntax and Structure

  • Scoping is done with whitespace
  • Forces a good, readable style on structure
a = 5
b = 2
if a > b:
    print "a is greater than b"
else:
    print "a is less than b"

More Syntax

for i in range(5):
    print i

count = 0
while count < 50:
    print count
    count = count + 1

def get_42():
    """A function that returns 42"""
    return 42

get_42()

 

Classes and Modules

The way to be object oriented

class MyClass:
    MyVar = 50
    def MyFunc():
       return MyVar

Go out to Terminal and look at MyModule.py, MySecondModule.py, then talk about importing, docstrings, and one line comments.

Scientific Community - SciPy

SciPy contains modules for optimization, integration, special functions, signal and image processing, genetic algorithms, ODE solvers, and other tasks common in science and engineering.

 

Mathematical Community - Sage

SAGE: Open Source Mathematics Software. Creating a viable free open source alternative to Magma, Maple, Mathematica, and Matlab.

GUI toolkits

 

IDEs and Tools

Useful tools:

 

Web applications

 

Plone

  •  Unified installer (see installation pages)
  • Content Management System
  • A Zope application
  • Runs on the Zope application server

Community and References

  • Python Enhancement Proposals (PEPs)
  • Cheese Shop
  • #python and #plone on Freenode
Document Actions