Introductory Programming in Python: Lesson 5
Basic Input

[Prev: Program State and Basic Variables] [Course Outline] [Next: Flow Control: Conditionals]

Literal Values

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?

The raw_input() Function

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.

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.

Exercises

  1. If there is a function named my_function, how do I call it?
  2. Are functions expressions or statements? What about when they are called?
  3. Start the python interactive interpreter:
    1. Assign what the user types to a variable called 's'.
    2. Print the value of the variable s.
    3. Print 's'.
    4. Use raw_input() to prompt the user for a number, get that number, and assign it to a variable called 'n'.
    5. Print double the value of the variable 'n'.
    Exit the python interactive interpreter.
  4. Write a program that asks the user user for their name and stores it in a variable. Then output "Hello", followed by the user's name
  5. Write a program that asks the user to enter two numbers, and prints the sum of those two numbers.
  6. Write a program that asks the user to enter two numbers, and prints the difference of those two numbers.
  7. Write a program that asks the user to enter two numbers, and prints the product of those two numbers. Are you sensing a pattern here?
  8. Write a program that asks the user for some text, and a number. The program prints out the text a number of times equal to the number entered, without line breaks or spaces in between each repetition.
[Prev: Program State and Basic Variables] [Course Outline] [Next: Flow Control: Conditionals]