Python

Appeared in:
1991
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.py .pyw .pyc .pyo .pyd
Versions and implementations (Collapse all | Expand all):
Programming language

Python is a general-purpose high-level programming language.

Python design philosophy emphasizes code readability.

For readability Python uses indentation for block delimiters, which is unusual among popular programming languages.

Elements of syntax:

Inline comments #
Non-nestable comments """ ~ """
Case-sensitivity yes
Variable identifier regexp [A-Za-z0-9_][A-Za-z0-9_]*
Function identifier regexp [A-Za-z0-9_][A-Za-z0-9_]*
Variable assignment =
Variable declaration ducktyped
Variable declaration with assignment variablename = value
Grouping expressions expression; expression
Block block using indents
Physical (shallow) equality ==
Physical (shallow) inequality !=
Deep equality is
Deep inequality is not
Comparison < <= >= > ≤ ≥
Function definition def functionname(a,b,c): ~
Function call functionname(a,b,c)
Function call with no parameters functionname()
If - then if ~: \n ~
If - then - else if ~: \n ~else: ~
Loop forever while True: ~
While condition do while ~: ~
Do until condition while True: ~\n if ~: break
For each value in a numeric range, 1 increment for i in xrange(start; stop; step): ~
For each value in a numeric range, 1 decrement for i in xrange(start; stop; -step): ~

Examples:

Hello, World!:

Example for versions Python 2.5.2
print "Hello, World!"

Factorial:

Example for versions Python 2.5.2, Python 2.6.5

This example uses recursive factorial definition.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

for n in range(16 + 1):
    print "%d! = %d" % (n, factorial(n))

Fibonacci numbers:

Example for versions Python 2.5.2, Python 2.6.5

This example uses recursive definition of Fibonacci numbers.

#! /usr/bin/env python
def fibonacci(n):
    if n < 3:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

for n in range(1, 16 + 1):
    print "%i," % fibonacci(n) ,
print "..."

Factorial:

Example for versions Python 2.5.2, Python 2.6.5

This example uses iterative method of calculating factorials.

def factorial(n):
    if n == 0:
        return 1
    
    f = 1
    for i in range(1, n + 1):
        f *= i
    return f

for n in range(16 + 1):
    print "%d! = %d" % (n, factorial(n))

Quadratic equation:

Example for versions Python 2.6.5
import math
import sys

A = float(raw_input("A = "))
if A == 0:
    print "Not a quadratic equation"
    sys.exit()

B = float(raw_input("B = "))
C = float(raw_input("C = "))

D = B * B - 4 * A * C
if D == 0:
    print "x =", -B / 2.0 / A
    sys.exit()
if D > 0: 
    print "x1 =", (-B + math.sqrt(D)) / 2.0 / A
    print "x2 =", (-B - math.sqrt(D)) / 2.0 / A
else:
    print "x1 = (", -B / 2.0 / A, ",", math.sqrt(-D) / 2.0 / A, ")"
    print "x2 = (", -B / 2.0 / A, ",", -math.sqrt(-D) / 2.0 / A, ")"

CamelCase:

Example for versions Python 2.6.5

The program uses Python standard library functions translate and title.

title counts all non-letters as word delimiters, so no need to change them to spaces before calling title.

non_letters = ''.join(c for c in map(chr, range(256)) if not c.isalpha())

def camel_case(s):
    return s.title().translate(None, non_letters)

print camel_case(raw_input())