So far our programs have been pretty uninteresting. They consistently produce the same results because they consistently act on the same data, or input, which we have specified by hard coding values for our inputs in the form of literals. A 'literal' is sort of the opposite to a variable, in that its value does not change over the course of program execution, and more importantly, its value is specified literally, within the program code.
As mentioned before, python understands a fairly limited set of key words and symbols as atomic statements or operators. Previously, we said that python treats unrecognised words (used is a very loose sense) as variable names, but this is not strictly true. Python treats unrecognised words as expressions, and attempts to determine their value. Now, the word can specify a value directly, in which case it called a literal value, or simply 'literal' for short. Only if the word isn't a literal, does python treat is as a literal.
#An example differentiating literals and variables a = 3 #a is a variable name, 3 is an integer literal b = 'b' #b is a variable name, 'b' is a string literal c = True #c is a variable name, True is a boolean literal if 3*'b' == a*b: #3 and 'b' are literals, a and b are variables print c #is c literal or variable? else: print "False" #is "False" a literal or a variable? #Are False and "False" the same?
It's not very helpful to us if we must change our programs every time we want to change the data they work with. So instead we want to be able to tell our program to get input from somewhere. The simplest place to get input from is the keyboard, in the form of text entered by the user. Python provides a function to do just this, called raw_input:
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> name = raw_input("What is your name? ") What is your name? James >>> print name James >>>
We've seen 'raw_input' before in the basic concepts section, and there really isn't much to it. Just a few things to note really. Firstly, raw_input is a function. A function is a defined collection of statements that produce or return a value when executed. You could think of them as a way of turning a small set of statements into an expression. We will learn how to define our own functions later on in the course, but python provides quite a few basic built in ones which we are going to be using before that, so let's look briefly at how they work.
raw_input
is the name of the function. Like
variables, functions have names to identify them. Like a variable,
a function name is a label that points to the collection of
statements to be executed.raw_input()
"What is your name? "
is a parameter to the
function. Functions are like mini programs. They always do the same
thing, but they can do the same thing to different data. We put the
data we want a function to operate on in between the brackets when
we call it. Expressions and variables passed to
functions in this way are temporarily known as
parameters. Multiple parameters can be passed to a
function by separating them with commas, and in general any valid
expression is a valid parameter; e.g.minimum(6, 2,
3+4, 4**2, 8.3, 3*1)
Back to 'raw_input'! 'raw_input' captures a single line of text from the keyboard, as entered by the user. It returns a string containing the captured text, leaving out the "\n" produced when the user hits the Enter key. In addition, it takes one optional parameter, which is displayed as a prompt prior to accepting input from keyboard. If this parameter is left out, the default prompt is an empty string, so nothing is printed.