The Personal Web Pages of Chris X. Edwards

XED's Python Lessons for Beginners

--------------------------

Lesson Two - Definitely Your Type

A programming language does stuff and remembers stuff. Before we do stuff, let's have a quick look at how Python remembers stuff. At the interpreter, you can assign a value to a variable like this:
>>> x=9
That was pretty easy. Nothing much seems to happen, but now x is treated like 9. Try just typing x.
>>> x
9
The value of the variable is returned. Unlike other languages, there are no special symbols needed to signify variables (like $x). Now that we know how to establish a variable, what kinds of things can the variable contain? Python contains a good, but not excessive selection of variable "types". Some types (like tuples, sets, and longs) you won't have to worry about unless your problem starts to get complicated. The five most important Python types for beginners are float, int, string, list, and dict. Here is a brief introduction to them.

NUMERIC TYPES

We've seen float and int in lesson one. They are numbers. They can be converted explicitly if needed.
>>> int(69.94)
69
>>> float(1)
1.0
You may notice stuff like this sometimes:
>>> .3 + .4
0.69999999999999996
This is an advanced topic. Basically Python is showing you the error inherent in using 1s and 0s to represent our decimal numbers. Kind of interesting really. For most everyone, this is usually safe to ignore.

STRINGS

The next type is a critically important one, strings. Strings are just sequences of characters.
>>> bc='supernatural'
>>> bc
'supernatural'
Strings do useful tricks like this:
>>> len(bc)
12
What if you need the apostrophe in your string? This is a classic problem and Python has several answers. The easiest answer is to use "the other quotes" from the ones you need.
>>> "Chris' website"
And if you're really getting messy:
>>> '"There\'s always a way," according to Chris\' website.'

LISTS

The next type really helps out with remembering stuff. It is the list and it is the classic container type. Basically what the list does is hold on to an ordered collection of other data, even other lists. For example, a list could be used to hold name, height, and weight data.
>>> xed=[ 'Chris', 1.77, 70 ]
Many tricks that apply to strings also apply to lists:
>>> len(xed)
3
To get a component of a list out, you need to know it's position. Positions start counting at 0. This is kind of weird, but smart people have good reasons for making it this way.
>>> xed[0]
'Chris'

DICTIONARIES

The final basic type we'll look at is the dictionary. If you've used other languages, this might have been called a hash or map. The idea is similar to a list except that there is no inherent order to the data. Instead of by position, the data is fetched by a key. The key can be thought of as each component's name. Here's an example.
>>> d={'en':'dictionary', 'de':'Woerterbuch', 'es':'diccionario'}
Now d is a dictionary which holds the words for "dictionary" in other languages. The keys are the two letter language abreviations. To get the German version:
>>> d[de]
Woerterbuch
You may have noticed that this could be done with a list, but then you'd have to remember what the positions of everything were. Dictionaries are more natural to use when the order of the data is fundamentally not important.

There's a lot to learn about Python types, but that's a good start.

--------------------------
Lesson 1 LWM Home Python Lessons Index Lesson 3
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ March 2006