The following pages constitute a "quick start" tutorial to Python, designed to give you all the information you need to put together basic scripts and to point you toward resources that can take you further.
Fedora comes with Python installed by default, as do many other versions of Linux and Unix — even Mac OS X comes with Python preinstalled. This is partly for the sake of convenience: Because Python is such a popular scripting language, preinstalling it saves having to install it later if the user wants to run a script. However, in Fedora's case, part of the reason for preinstallation is that several of the core system programs are written in Python, including yum
itself.
The Python binary is installed into /usr/bin/python
; if you run that, you enter the Python interactive interpreter, where you can type commands and have them executed immediately. Although PHP also has an interactive mode (use php -a
to activate it), it is neither as powerful nor as flexible as Python's.
As with Perl, PHP, and other scripting languages, you can also execute Python scripts by adding a shebang line to the start of your scripts that points to /usr/bin/python
and then setting the file to be executable. If you haven't seen one of these before, they look something like this: #!/usr/bin/python
.
The third and final way to run Python scripts is through mod_python
, which is installed by default when you select the Web Server application group from the Add/Remove Packages dialog.
For the purposes of this introduction, we will be using the interactive Python interpreter because it provides immediate feedback on commands as you type them.
We will be using the interactive interpreter for this chapter, so it is essential that you are comfortable using it. To get started, open a terminal and run the command python
. You should see this:
[paul@caitlin ~]$ python
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
is where you type your input, and you can set and get a variable like this:
>>> python = 'great' >>> python
'great'
>>>
On line 1, the variable python
is set to the text great
, and on line 2 that value is read back from the variable when you type the name of the variable you want to read. Line 3 shows Python printing the variable; on line 4, you are back at the prompt to type more commands. Python remembers all the variables you use while in the interactive interpreter, which means you can set a variable to be the value of another variable.
When you are finished, press Ctrl+D to exit. At this point, all your variables and commands are forgotten by the interpreter, which is why complex Python programs are always saved in scripts!
Python is a language wholly unlike most others, and yet it is so logical that most people can pick it up very quickly. You have already seen how easily you can assign strings, but in Python nearly everything is that easy—as long as you remember the syntax!
The way Python handles numbers is more precise than some other languages. It has all the normal operators — such as +
for addition, -
for subtraction, /
for division, and *
for multiplication — but it adds %
for modulus (division remainder), **
for raise to the power, and //
for floor division. It is also very specific about which type of number is being used, as this example shows:
>>> a = 5
>>> b = 10
>>> a * b
50
>>> a / b
0
>>> b = 10.0
>>> a / b
0.5
>>> a // b
0.0
The first division returns 0
because both a
and b
are integers (whole numbers), so Python calculates the division as an integer, giving 0
. Because b
is converted to 10.0
, Python considers it to be a floating-point number and so the division is now calculated as a floating-point value, giving 0.5
. Even with b
being floating-point, using //
—floor division— rounds it down.
Using **
, you can easily see how Python works with integers:
>>> 2 ** 30
1073741824
>>>2 ** 31
2147483648L
The first statement raises 2 to the power of 30 (that is, 2×2×2×2×2× ...), and the second raises 2 to the power of 31. Notice how the second number has a capital L on the end of it — this is Python telling you that it is a long integer. The difference between long integers and normal integers is slight but important: Normal integers can be calculated with simple instructions on the CPU, whereas long integers — because they can be as big as you need them to be — need to be calculated in software and therefore are slower.
When specifying big numbers, you need not put the L at the end — Python figures it out for you. Furthermore, if a number starts off as a normal number and then exceeds its boundaries, Python automatically converts it to a long integer. The same is not true the other way around: If you have a long integer and then divide it by another number so that it could be stored as a normal integer, it remains a long integer:
>>> num = 999999999999999999999999999999999L
>>> num = num / 1000000000000000000000000000000
>>> num
999L
You can convert between number types by using typecasting, like this:
>>> num = 10
>>> int(num)
10
>>> float(num)
10.0
>>> long(num)
10L
>>> floatnum = 10.0
>>> int(floatnum)
10
>>> float(floatnum)
10.0
>>> long(floatnum)
10L
You need not worry whether you are using integers or long integers; Python handles it all for you, so you can concentrate on getting the code right.
Python stores a string as an immutable sequence of characters — a jargon-filled way of saying "it is a collection of characters that, after they are set, cannot be changed without creating a new string." Sequences are important in Python. There are three primary types, of which strings are one, and they share some properties. Mutability makes much more sense when you learn about lists in the next section.
As you saw in the previous example, you can assign a value to strings in Python with just an equal sign, like this:
>>> mystring = 'hello';
>>> myotherstring = "goodbye";
>>> mystring
'hello'
>>> myotherstring;
'goodbye'
>>> test = "Are you really Bill O'Reilly?"
>>> test
Читать дальше