Let's see how it goes.
What is Python, anyway? As has been noted, it is a scripting language. Scripting languages are translated into "computer-speak" as they run. This is as opposed to languages like C, which are pre-translated into a format the machine can digest by using compilers and linkers. Scripting programs generally execute more slowly for this reason, but have a faster development cycle, since you skip the whole compile/link step as you go through development iterations. In an age of multi-gigahertz processors, the perceived speed handicap of scripting languages is becoming less and less relevant.
Python is an object-oriented language. Everything in Python is an object. Generally, think of an object like a car - it has attributes, and methods. Car attributes might include four wheels, green paint, glass windows. Car methods might include accelerating, braking and steering. Similarly, objects in Python will (mostly) have attributes, and methods, which we'll get into shortly.
But first, open your favorite terminal program, such as Konsole, and type in what you see in green, then press enter:
[zccw01@LapNix ~]$ python
Python 2.5.2 (r252:60911, Jun 28 2008, 14:11:09)
[GCC 4.1.1 20060724 (prerelease) (4.1.1-4pclos2007)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you don't see something that resembles the above, scream now. This is the python interactive shell. Try the following, entering the text shown in green and pressing enter.
[zccw01@LapNix ~]$ python
Python 2.5.2 (r252:60911, Jun 28 2008, 14:11:09)
[GCC 4.1.1 20060724 (prerelease) (4.1.1-4pclos2007)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+3.14159
8.1415900000000008
>>>
Several important things just happened here.
First, Python parsed the expression, and discovered that it had two constants, 5 and 3.14159, with a binary operator, +.
Second, it determined the "type" of the constants. 5 is an integer number, and 3.14159 is a floating-point number.
Third, based on the type of the operands, 5 and 3.14159, it decided that the + operator should be a mathematical summation - addition. The + operator is "overloaded" in Python - depending on the operands, it may perform a different operation, such as concatenating two strings.
Fourth, it "upgraded" the integer, 5 to a floating point. You didn't ask Python to do this, but if there are mixed operand types, Python will always try to A) do what's expected, and B) give you the most precise result. This conversion was necessary because integers and floating-point numbers are stored in two very different ways - binary math operators require that the operand be of the same type before they can work properly.
Fifth, it calculated the result, formatted it and printed it for you.
And, sixth - it got the answer wrong. The wrong answer wasn't really Python's fault - it's a necessary evil of trying to store decimal fractions into a bunch of binary on/off switches All languages, to one degree or another, share this problem, and in programming, you have to keep it in mind - There are ways to be more accurate - much more accurate, but never completely accurate. Just be sure that your results are accurate enough for the task at hand.
So, since Python is so obliging with object typing, it should have no problem with the following (enter the green text into the Python shell and press enter):
5+"23" # Note the quotes - Yes, enter this too, on the same line
Did you get something that looks like the following?:
[zccw01@LapNix ~]$ python
Python 2.5.2 (r252:60911, Jun 28 2008, 14:11:09)
[GCC 4.1.1 20060724 (prerelease) (4.1.1-4pclos2007)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+3.14159
8.1415900000000008
>>> 5+"23" # Note the quotes - Yes, enter this too, on the same line
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
Congratulations - you've just crashed your first program. What happened? Was it that extra # Note the quotes - Yes, enter this too, on the same line?
Nope - the # sign is signifies a comment to python, and the interpreter ignores the comment, and everything that follows on the same line. So that wasn't the problem. If we examine the result, you'll see that your program died in line 1 (not surprising, as there's only one line) and that the operand types are unsupported for the + operator.
This is not strictly true. The plus operator does support string and integer types - just not in the same expression.
Enter the following lines into the python interpreter, pressing enter after each line:
a="53";b=27
a+str(b)
int(a)+b
(sleazy data entry tip - highlight the line with your mouse in the browser window, then, in the terminal window that's running the python shell, click down on your mousewheel, or middle button - I know, every one already knows this, but...)
Did you get something like the following?:
>>> a="53";b=27
>>> a+str(b)
'5327'
>>> int(a)+b
80
>>>
The first line is really two lines. The semicolon allows you to put more than one logical Python line on a single physical line - don't abuse this trick. Python reads easier vertically than horizontally. On this line, we created two new variables, a and b. Using the = assignment operator, we assigned the string constant '53' to variable a, and the integer constant, 27 to variable b.
On the next line, a+str(b), we brought a new trick to the party - explicit casting. Up til now, we've let Python imply the casting of variable and constant types, but sometimes, slick as it is, Python needs a little guidance. In this line, we used the explicit str() cast to force the variable, b, to be evaluated as a string type. As a result, the interpreter evaluated two string operands, and decided that the + operator should perform concatenation, instead of addition. The result was that the two strings, '53' and '27' were concatenated to give '5327'.
In the final line, we explicitly cast the string variable a as an integer. The result was the + operator dealing with two integers, and an addition that yielded 80 for an answer.
So, given the preceding, you should have some idea of what will happen when you enter the following:
'This ' * 8 + 'That'
Did that surprise you? Here's what I got:
>>> 'This ' * 8 + 'That'
'This This This This This This This This That'
>>>
When you think about it, it makes sense - after all, if Python can overload the + operator to mean either addition or concatenation, then it makes sense that the * operator is similarly overloaded to mean either multiplication (which is just repeated addition) or multiple concatenation.
So when the teacher asks you to stay after class and write 'I Love Python' twenty times on the chalkboard...
print('I Love Python\n' * 20)
Should do the job - and introduce you to the ideas of text formatting (\n), and functions, such as print()
Play with the above, and when you're finished, press CTRL-d to close the Python shell.
Next up: Functions and modules and bears, oh my!