Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Learn to Program: Types and Objects

created by Jongleur

(idea) by Jongleur (1.5 d) (print)   ?   (I like it!) Sat May 18 2002 at 11:52:36

So, believe it or not, you've come a long way in your journeys. You've learned how to say things to a user, how to make the computer do math for you, and you've started learning about how to store and manipulate data. It's continuing that last one that this chapter concerns itself with.

The reason that the previous was so short in the function section is that I wanted to introduce the notation without getting overly bogged down in the how and why. If what you know already is enough for you to do what you need, then that's great! Why are you still reading this? Go forth and solve your problems... In order for you to progress, however, you are going to have to learn about two concepts that appear very simple, but have a lot of hidden complexity. Remember - I am giving the broadest of overviews here; groundbreaking PhD theses continue to be written on aspects of parts of these chapters.

So, functions sound great, but one thing you have to realize is that you can't do everything to all types of data. This should make intuitive sense to some degree - there is no good answer to the question of "what is 'wertyop' divided by 'fobar' equal to?" This notion of functions only working when they are given particular types of data, is known as a type system. If you want the technical terms, then you should know that Python is manifestly or weakly typed, depending on how persnickety you want to be about your terminology.

What this means for you is that Python will try to perform all the operations you asked it to on all of the functions you gave it, and it will give you an error if you have passed in something it doesn't know how to deal with. This attempt to buckle down and try is pretty unique to a sub-variety of programming languages known (sometimes incorrectly) as scripting languages. If you end up programming in a compiled language instead, you should know that the rules are probably a lot stricter - caveat coder.

Python's type system also means that things act differently depending on their context. When you have two variables, var1 and var2, and you type var3 = var1 + var2, then, in order for you to know what's going on, you have to know what type each of those variables is. If the types are incompatible python will let you know. If they are two strings, then the answer is the concatenation of those two strings. If they are numbers, then they get summed. If they are arrays, then they get concatenated - more on what an array is in a minute.

I'll go into more detail on each of the below pretty soon, but right now, here are several fundamental types:

Numbers
You know how these work!
Objects
Ahh. Objects. Depending on who you talk to, objects are either a godsend or Satan's spawn. What they are in reality, however, is a sort of uber-variable that can hold other variables and functions inside. All of the above types are examples of special types of Object. It's a very general, and thus kind of tricky, concept.
Strings
An object that is a collection of characters in a particular order.
Arrays
An object that is a collection of anything in a particular order.
Tuples
An array whose members cannot be changed. Note that a tUple has nothing at all to do with the number three. That would be a tRIple. It's a math thing - blame the mathematicians.
Dictionaries
Also known as a hash, hashtable, or map, this object is a collection of objects that are indexed, not by number, but by other objects. Very useful, but pretty advanced as well.
Functions
Yes, even functions turn out to be objects. You can pass them around, assign them to variables, and generally have all sorts of fun.
Whoah. That list turned out a little longer than I wanted, and any desire to smack me for presenting a list loaded with technical terms would be pretty understandable. The good news is that you don't have to intuitively understand them yet. Intuition will come with time and practice. Right now, let's just go through them on-by-one...

Numbers

Numbers should be the most sensible of all. When a number is stored in a variable, anything it makes sense to do to a number, you can do to that variable. For more info, turn back to Learn to Program: Math.

Objects

An object is a container for data that can have sub-containers, and associated sub-functions. These sub- things can be accessed by using a period after the object. Anytime you see a period in Python, you know you are dealing with an object in some way. Files are objects, as are modules, strings, arrays, tuples, and many others. You can define your own objects, but we'll deal with that later. For now, you can just think of them as a sort of meta variable - a variable that can contain other data and functions.

Arrays

An array is an object that contains a bunch of other objects in a particular order. You then reference which object in the array you want by using the square brackets ([ and ]). In classic CS fashion they are indexed starting from 0 rather than 1. This is done mostly for historical reasons. As an example, you might do the following:

bigarray = ['my', 'is', 'blah']
bigarray[2] = 'name'
bigarray.append('Jongleur')
print bigarray[0], bigarray[2], bigarray[1], bigarray[3]
and you would end up with my name is Jongleur as output. These initially seem silly, but they are actually really useful.

Tuples

Tubles are arrays that you can't change. They are immutable. These seems extra silly, but their immutability allows them to be used for many situations where you don't want your data to change. You have to use tuples and not arrays if you want to use an array-like thing as a dictionary entry. You create a new tuple using ( and ) instead of the square brackets. After they are created, however, you treate them just like arrays - just don't put them to the left of an =. For the example above, we would remove the second line, and replace the first line with bigarray = ('my', 'is', 'name', 'Jongleur'). The output would be the same.

Strings

Strings are actually just arrays of one-element strings. An oddly recursive sounding explanation, but very useful for finding out what the 5th character in a string is. Just use that same square-bracket syntax and go for it!

Mapping Types

Mapping types are any type that uses the square brackets. The only type of these that you have to worry about are the previously mentioned dictionaries. There are others, but they only come up in rarified situations, and by then you will know enough to be able to teach yourself about them.

Sequence Types

The general name for any mapping type that you can can only address using integers (starting at 0) is a sequence type. There are several language constructs that use these types to do some interesting things - Learn to Progam: Loops, but for now, you should just marvel at the fact that there is a heirarchy of types - kind of like how every integer is a rational number, but not vice versa. This means that a string is a kind of sequence type, which is a kind of mapping type, which is a kind of object. Heady stuff.

Functions

Functions are objects too, but that shouldn't affect your life too deeply -- just keep that info in the back of your head when you are doing more advanced stuff. To call a function, simply put down the function's name and follow it up with the parentheses. You saw how to do this earlier. If you would like to use the function as an objects, simply use its name, but don't put the parentheses at the end.

Modules

Modules are groups of functions and variables that have been collected together. To use (for example) the math module, you put import math somewhere in your code prior to calling one of the functions it contains. You can then call any functions contained in the math module by using math. to prepend any function. For example math.sin(3.14159265359) will find the sine of pi.


Gah. What a list. What a lot of concepts. Don't worry, you don't have to remember them all. There won't be a test later, and you won't have to recall all the attributes at the drop of a hat. What this was meant to serve as is a quick overview, so that later, when you are faced with a question, you'll have some hazy memory to fall back on.

<< | Learn to Program | >>


This section is the weakest one so far in terms of explanatory goodness. If you can think of some way to improve it, please don't hesitate to /msg me.


printable version
chaos

Learn to Program: If/Then Learn to Program Learn to Program: Variables and Function Calls Learn to Program bash
Sine strings weakly typed Perl 5 Pocket Reference: Data Types
Teach Yourself Scheme: 2.3 Other Data Types Gertrude Stein Object Oriented Software Construction meta
immutable Orson Scott Card blame Everything Praise and blame are forms of manipulation that you no longer require
Concatenation Manifestly How I got over my homophobia or the reasons that I blame my grades on a gay man God module
Guns don't kill people; people kill people Visual Basic Object Properties There is no weirdness like the weirdness of a novel written by a mathematician Kiwi
No more writeups are being accepted for this node. We don't need to repeat this in every programming language known. Jongleur's choice of Python was exemplary, but these nodes are about programming. If you feel you have something to add to this node, post it on your Scratch Pad and contact an editor.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
The War of the Worlds
Carnal Harvest
The Happy Prince
The crack whores vs. hellfire
Kings Cross by Coke-light
Stonewall Riots
Nobel Prize in Literature
President of the United States of America
This is just pure cool! It's cryptic, mystical, lovely. I had no choice!
It will be in Georgia. It will be Taipei.
Poetry you found that you wrote when you were ten but secretly still like
Joey Bishop
New York subway
New Writeups
LostPsion
"Shut the Fuck Up" Theaters(idea)
Vanish
The line between normal and not(place)
beatrice
You've been slowly taking me over for nearly a year, do you know that?(idea)
Berek
YouTube(thing)
shaogo
How to Pretend to Have a Job(idea)
hapax
Les Provinciales(review)
zoeb
The Scene(review)
aneurin
Telephone Numbers for drama purposes(idea)
Alnilamski
Cosmicopolis(fiction)
eien_meru
measure(idea)
Dreamvirus
pussy willow(thing)
czeano
Three "T"s(idea)
UncleM
Vantage Point 2: Fractal Thread Count(idea)
LostPsion
First Fiction: Night(fiction)
Lysander Peregrine
How to Pretend to Have a Job(idea)
E2 is a by-product of the existence of The Everything Development Company