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,
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:
- You know how these work!
- 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.
- An object that is a collection of characters in a particular order.
- An object that is a collection of anything in a particular order.
- 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.
- 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.
- 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 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.
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.
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 = 'name'
print bigarray, bigarray, bigarray, bigarray
and you would end up with
my name is Jongleur
as output. These initially seem silly, but they are actually really useful.
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 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 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.
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 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 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.