Python

From FUKTwiki

Jump to: navigation, search

Python is a programming language.

It is:

  • Interpreted, which means it is not normally compiled into machine code.
  • Garbage collecting, which means it allocates memory and creates and destroys objects automatically.
  • Dynamically typed, which means that variables do not have a type, only values have a type.
  • Widely used.
  • Very actively developed and improved.
  • Object-oriented, although it does not force this onto the programmer.
  • Powerful, since it has many advanced data types and a very large standard library.
  • Platform-independent.

Contents

[edit] Example Code

import locale
locale.setlocale(locale.LC_ALL, "")
 
print u"Hello world!"

[edit] Functions

def do_nothing():
    pass
 
def one():
    return 1
 
def add_one(x):
    return x+1
 
def print_number(n):
    print "Number:", n

With the above functions defined, print_number(add_one(one())) would print “Number: 2”.

# A more complex function demonstrating more of the language.
 
def blargh(x, y, z=None):
    """This function is just nonsense."""
    if z is None:
        z = x + y
    else:
        z += x
    while z > x:
        y += z
        z -= 1
    return y, z

Note that the language does not have any { braces } like C or Java, or any BEGIN/END constructs. Instead, the whitespace determines where a block of code ends.

[edit] Classes

# This class does nothing.
 
class Empty(object):
    pass
 
# This class inherits from “Empty” and has a method.
 
class Foo(Empty):
    def bar(self):
        return "baz"
 
# This class has a constructor which takes no arguments.  The
# constructor creates an attribute named "some_string".
 
class Bar(object):
    def __init__(self):
        self.some_string = "whatever"
    def pretty_value(self):
        return "The value is: " + self.some_string

The language has many datatypes, like unicode strings, lists (like a C++ std::vector), dictionaries (an associative array, like a C++ std::map), sets and more. The language also has advanced features like exceptions, iterators, generators, introspection, metaclasses, etc.

[edit] See Also

Personal tools
FUKT
../local/fukt.png);" href="/wiki/Main_Page" title="Visit the Main Page [z]" accesskey="z">