Graphics Reference
In-Depth Information
Figure 12-2
The Python Console window's command line
If you've used command-line interfaces such as on a Unix or Linux terminal or a DOS console, the idea of a
command-line shell is already familiar. The command line enables you to directly write commands or requests
(usually on a single line) and get an answer back, in this case directly from the Python interpreter. The line that
begins with three greater-than symbols (
>>>
) is the command-line prompt (the last line in
Figure 12-2
)
. If you
type your command on this line and press Enter, the Python interpreter's reply will be printed on the next line,
as shown here:
>>> print("Hello World!")
Hello World!
>>>
In this example, I requested that the Python interpreter print
Hello World!
As you can see, that's exactly
what it did.
As the command-line interface runs, the Python interpreter keeps track of the state it is in, which means that
variables' values can be assigned via the command line and then retrieved later, like this:
>>> my_variable = "Hello World!"
>>> print(my_variable)
Hello World!
>>>
Thisservesasagoodexample,butwhenyou'reworkingdirectlyinthecommandline,the
print
command
is unnecessary. Simply entering a value in the command line will have the effect of echoing the value. Sub-
sequent examples of working directly on the command line of the interpreter will omit the
print
command;
however, it is still necessary to use
print
when writing code in scripts, so this won't be the last you see of it.
Beingabletointeract withthecommandlinealsoenablesyoutolookmorecloselyatthevalueofindividual
variables during runtime, which can be useful for debugging. For example, it might be of interest to investig-
ate the data type of the variable assigned in the preceding code snippet. You can do this by using the
type()
function, which returns the data type of its argument. As you can see, the type returned is the
str
, which is the
label for the
string
class:
>>> type(my_variable)
<class 'str'>
You'll look more closely at data types later in this chapter.Fornow,it is enough to knowthat Python defines
a set of basic data types that reflect the kind of data a variable represents. The string class treats data as se-