Strings

Strings in Python are much like strings in C, in that they can be references either as the full string or treated as a character array.

>>> d = "Hello"
>>> d
'Hello'
>>> d[ 4 ]
'o'
>>> d[ 1:4 ]
'ell'

Strings can also be iterated over like a sequence.

>>> for i in d:
	print i
H
e
l
l
o
>>> list( d )
['H', 'e', 'l', 'l', 'o']

Numeric Types

As in any other modern programming language, there are a few numeric types available. In python these are the int, float, long, and complex. There are no unsigned types.

Dynamic Typing and Type Checking

Unlike many computer languages, Python does not have static variable types. Types are dynamic, meaning they are changed on an as-needed basis. This can sometimes create problems, but is usually only problematic when creating public libraries, where the input types to a function cannot be controlled. The type( ) function can be used to determine the type of an object.

>>> g = 4
>>> type( g )
<type 'int'>
>>> g = 4L
>>> type( g )
<type 'long'>
>>> g = "Hello"
>>> type( g )
<type 'str'>

Lists , Tuples, and Dictionaries

There are 3 major types of sequence types in Python. These, as I'm sure you can tell from the title, are the list, the tuple, and the dict (dictionary). These correspond to types in many other languages, lists and tuples are like normal arrays, dictionaries are like associative arrays.

PyBlackjack

This is a simple vegas-style blackjack game written in Python. This game was a very early project of mine when I was first learning Python ( and programming for that matter ), so the code isn't the best. Still, this might be entertaining or educational.

It currently supports up to 6 players. Network play is not currently supported.

Version 2.0.1
download

The fraction Module

This is a module I wrote for handling fractions in python. It allows increased accuracy for non-integer mathematic operations and may be useful for specialized applications.

Version: 0.1-rc2
download
documentation

Loops and Iterators

Unless you are new to programming, you have doubtlessly seen and used many loops. In Python, the while loop is just like any other language, though parentheses are optional:

sum = 0
i = 0
while ( i < 10 ):
    sum += i
    i += 1

print sum

This would sum the numbers 0 through 9, which is 45.

Code Blocks and Indentation

Unlike many traditional languages, in Python proper indentation of your code isn't just suggested, it's required. This is the number one complaint people give about using Python. I've personally never understood why. If you're used to indenting your code the way it should be, then this isn't a problem. If you're not, then maybe you should be, so your partners on your next team project aren't yelling at you for having hard to read code. Here is an example from a module I wrote:

Importing Modules

Like many languages, Python has a basic set of functions and objects available to it to start off with, but to really use the power of the language, you'll need to import modules or packages from the library. I won't get into the difference between the two here, but from a usage perspective, there isn't one. From this point forward I will refer to both as modules.

There are two basic ways to import modules in python:

import math
#or
from math import *

Powered by Drupal - Design by artinet