Python Notes - Input/Output

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips  

We have seen print before. It can display a series of items, with commas between, as in:
print a, b, "message", 2*x+1

The items are displayed with a space between them. If you don't want thi space, you can convert items to a string with the str function, and join them with +, as in:
print "area=" + str(area)
which displays
area=3
with no extra spaces

input, raw_input

We will extend our area program, so that the program asks the user for the dimensions:

width = input("Enter the width: ")
height = input("Enter the height: ")
area = width * height
print "Area is" , area, "sq metres"
When we run the program, the input instruction prints our prompt:
Enter the width:

we can then key in our number, which gets assigned the variable width

To input text, such as a name or address, we use raw_input, as in:

name = raw_input("Who are you: ")
print "Welcome to the program", name

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips