The "Uncool" Python types

The "Cool" Python Types

/home/steder/Projects/Tutorials/python/BobChat-v0.3/BobClient.py
# Introduction to Python's Data Structures
#
# Int and Long int(Range?)
inta = 100
fact = def factorial (n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
intb = fact(100) # 100! is pretty damned big, notice python doesn't choke.

# Float/Double(Precision?)
floata = 1.2
floatb = 0.1 # isn't this peculiar?

# Strings(Indexing, Concatination, Searching, Matching)
s1 = "I am a string!\n"
c1 = s1[1]

# Tuples(What are they, simple examples)
t1 = (1, 2) # coordinates on a grid
t2 = ("username", "passcode") # Not a very useful "use" of a tuple
# Tuples are mostly going to be used for things like encapsulating a
# bunch of function arguments, especially things like coordinates
# that are very tightly coupled.

# Lists(What they are, append, concatenate, add, remove(del), searching, map, SLICES!)

# Now connect strings (w/slicing) to lists :~)

# Dictionaries(What they are, some neat uses, NOTES on SPEED?)

Typing

Python features a "dynamic" typing model, where types are allocated on the fly and can be changed mid program. From one line to the next a variable "x" could be an integer, a string, and a list. Not all at the same time of course, but x could be set to 3 different values, and Python would accept it as completely valid. This flexible and powerful type system is useful because it is generally very intuitive, and doesn't really intrude when you're writing code. You rarely have to go back and define variables, nor do you have to define lots of temporary variables for different types of data. Let's look at a simple example or two.

/home/steder/Projects/Tutorials/python/BobChat-v0.3/BobClient.py
x = []
for i in range(0,100):
    x.append(i)

y = x * 10

x = "Hello World!"

print x, y

# What are X and Y?

Type Specific Functions

Here I'd like to illustrate a bunch of type specific operations and functions as a reference. A lot of these functions you'll need often, so it seems only fair to introduce them early on. On to the examples:

/home/steder/Projects/Tutorials/python/BobChat-v0.3/BobClient.py
#############
# Arithmetic
#############
4 + 4 == 8 
3 - 5 == -2
16 / 2 == 8
16 * 4 == 64
3 % 2 == 1 # Modulus

# Long Int Conversion - Useful for function type mismatches - Not needed here.
100000000000000000000L + long(100) == 100000000000000000100L

##########
# Strings
##########
a = "Hello"
b = "World!"

# Concatenation
a + " " + b == "Hello World!"
# Repetition and Concatenation
a * 3 + " " + b == "HelloHelloHello World!"
(a + " ") * 3 + b == "Hello Hello Hello World!"
# Indexing
b[2] == "r"
# Conversion of data to string - Sesame Street:
c = 7 # Backquote ` ` or str() operations
( a + " number " + `c` ) and ( a + " number " + str(c) ) == "Hello number 7"

########
# Lists
########
x = [1, 2, 3, 4, 5]
y = [1, 2.0, (3,4), [5], {"Hello":"World!"}]

# Append 
x.append(y) == [ 1, 2, 3, 4, 5, [1, 2.0, (3,4), [5], {"Hello":"World!"}] ]
# Concatenate - Notice the similarity to Strings
[1,2,3] + [4,5] == [1,2,3,4,5]
# Multiply/Repeat - Again, strings are similar
[1,2] * 3 == [1,2,1,2,1,2]
# Delete
del y[2] == [1, 2.0, [5], {"Hello":"World!"}]
# Or
y[2] = None
y == [1, 2.0, {"Hello":"World!"}]
# Pop - Remove and delete the last element of a list
x.pop() == 5
x == [1,2,3,4]
# Length of a list
len(x) == 4

##########
# Slicing 
##########
z = range(100) # [0, 99]
z.reverse()
z[10:19] == [89, 88, 87, 86, 85, 84, 83, 82, 81]
z.reverse()
z[:10] == [0,1,2,3,4,5,6,7,8,9]
z[5:10] == [5,6,7,8,9]


###############
# Dictionaries
###############
alpha = { "Red Fish":"Blue Fish", "One Fish":"Two Fish"}
beta = { "Bob":"(555)555-5575", "Carol":"(555)555-7757" }

# Keys
alpha.keys() == ["Red Fish", "One Fish"]
beta.keys() == ["Bob", "Carol"]
# Values
alpha.values() == ["Blue Fish", "Two Fish"]
# Indexing
alpha["Red Fish"] == "Blue Fish"
beta["Carol"] == "(555)555-7757"
# Adding to a Dictionary
beta["Dave"] = "100"
beta == { "Bob":"(555)555-5575", "Carol":"(555)555-7757", "Dave":"100" }
# Deleting an entry
beta["Dave"] = None
# Or
del beta["Dave"]
beta == { "Bob":"(555)555-5575", "Carol":"(555)555-7757" }
# Length (Size) Of a dictionary
len(alpha) == 2
# Checking for membership in a dictionary
beta.has_key("Carol") == 1