Jump to content

Non-Programmer's Tutorial for Python 2.6/Who Goes There?

From Wikibooks, open books for an open world

Input and Variables

[edit | edit source]

Now I feel it is time for a really complicated program. Here it is:

print "Halt!"
user_reply = raw_input("Who goes there? ") 
print "You may pass,", user_reply

(user response: using Linux and Geany editor...only option shown was 'user_return'...output was correct. However, when manually typed 'user_reply' also worked correctly, even though not displayed as a option in Geany...what is the functional difference between these two?)

When I ran it, here is what my screen showed:

Halt!
Who goes there? Josh
You may pass, Josh

Note: After running the code by pressing F5, the Python shell will only give the output:

Halt!
Who goes there?

You need to enter your name in the Python shell, and then press Enter to get the rest of the output.

Of course when you run the program your screen will look different because of the raw_input() statement. When you ran the program you probably noticed (you did run the program, right?) that you had to type in your name and then press Enter. Then the program printed out some more text and also your name. This is an example of input. The program reaches a certain point and then waits for the user to input some data that the program can use later.

Of course, getting information from the user would be useless if we didn't have anywhere to put that information and this is where variables come in. In the previous program, user_reply is a variable. Variables are like a box that can store some piece of data. Here is a program to show examples of variables:

a = 123.4
b23 = 'Spam'
first_name = "Bill"
b = 432
c = a + b
print "a + b is", c
print "first_name is", first_name
print "Sorted Parts, After Midnight or", b23

And here is the output:

a + b is 555.4
first_name is Bill
Sorted Parts, After Midnight or Spam

The variables in the above program are a, b23, first_name, b, and c. A variable in Python can store any type of data - in this example we stored some strings (e.g. "Bill") and some numbers (e.g. 432).

Note the difference between strings and variable names. Strings are marked with quotation marks, which tells the computer "don't try to understand, just take this text as it is":

print "first_name"

This would print the text:

first_name

as-is. Variable names are written without any quotation marks and instruct the computer "use the value I've previously stored under this name":

print first_name

which would print (after the previous example):

Bill

Assignment

[edit | edit source]

Okay, so we have these boxes called variables and also data that can go into the variable. The computer will see a line like first_name = "Bill" and it reads it as "Put the string Bill into the box (or variable) first_name". Later on it sees the statement c = a + b and it reads it as "put the sum of a + b or 123.4 + 432 which equals 555.4 into c". The right hand side of the statement (a + b) is evaluated and the result is stored in the variable on the left hand side (c). This is called assignment, and you should not confuse the assignment equal sign (=) with "equality" in a mathematical sense here (that's what == will be used for later).

Here is another example of variable usage:

a = 1
print a
a = a + 1
print a
a = a * 2
print a

And of course here is the output:

1
2
4

Even if it is the same variable on both sides the computer still reads it as "First find out the data to store and then find out where the data goes".

One more program before I end this chapter:

number = input("Type in a number: ")
text = raw_input("Type in a string: ")
print "number =", number
print "number is a", type(number)
print "number * 2 =", number * 2
print "text =", text
print "text is a", type(text)
print "text * 2 =", text * 2

The output I got was:

Type in a Number: 12.34
Type in a String: Hello
number = 12.34
number is a <type 'float'>
number * 2 = 24.68
text = Hello
text is a <type 'str'>
text * 2 = HelloHello

Notice that number was gotten with input() while text was gotten with raw_input(). raw_input() returns a string while input() returns a number. When you want the user to type in a number use input() but if you want the user to type in a string use raw_input().

The second half of the program uses type() which tells what a variable is. Numbers are of type int or float, which are short for integer and floating point (mostly used for decimal numbers), respectively. Text strings are of type str, short for string. Integers and floats can be worked on by mathematical functions, strings cannot. Notice how when python multiplies a number by an integer the expected thing happens. However when a string is multiplied by an integer the result is that multiple copies of the string are produced (i.e., text * 2 = HelloHello).

The operations with strings do different things than operations with numbers. Here are some interactive mode examples to show that some more.

>>> "This" + " " + "is" + " joined."
'This is joined.'
>>> "Ha, " * 5
'Ha, Ha, Ha, Ha, Ha, '
>>> "Ha, " * 5 + "ha!"
'Ha, Ha, Ha, Ha, Ha, ha!'
>>> 

This could also be done as a program:

print "This" + " " + "is" + " joined."
print "Ha, " * 5
print "Ha, " * 5 + "ha!"

Here is the list of some string operations:

Operation Symbol Example
Repetition * "i" * 5 == "iiiii"
Concatenation + "Hello, " + "World!" == "Hello, World!"

Examples

[edit | edit source]

Rate_times.py

# This program calculates rate and distance problems
print "Input a rate and a distance"
rate = input("Rate: ")
distance = input("Distance: ")
print "Time:", (distance / rate)

Sample runs:

Input a rate and a distance
Rate: 5
Distance: 10
Time: 2
Input a rate and a distance
Rate: 3.52
Distance: 45.6
Time: 12.9545454545

Area.py

# This program calculates the perimeter and area of a rectangle
print "Calculate information about a rectangle"
length = input("Length: ")
width = input("Width: ")
print "Area", length * width
print "Perimeter", 2 * length + 2 * width

Sample runs:

Calculate information about a rectangle
Length: 4
Width: 3
Area 12
Perimeter 14
Calculate information about a rectangle
Length: 2.53
Width: 5.2
Area 13.156
Perimeter 15.46

temperature.py

# Converts Fahrenheit to Celsius
temp = input("Fahrenheit temperature: ")
print (temp - 32.0) * 5.0 / 9.0

Sample runs:

Fahrenheit temperature: 32
0.0
Fahrenheit temperature: -40
-40.0
Fahrenheit temperature: 212
100.0
Fahrenheit temperature: 98.6
37.0

Exercises

[edit | edit source]
  1. Write a program that gets 2 string variables and 2 integer variables from the user, concatenates (joins them together with no spaces) and displays the strings, then multiplies the two numbers on a new line.
Solution

Write a program that gets 2 string variables and 2 integer variables from the user, concatenates (joins them together with no spaces) and displays the strings, then multiplies the two numbers on a new line.

  
string1 = raw_input('String 1: ')
string2 = raw_input('String 2: ')
int1 = input('Integer 1: ')
int2 = input('Integer 2: ')
print string1 + string2
print int1 * int2

Another Solution

print "this is an exercise"
number_1 = input("please input the first number: ")
number_2 = input("Please input the second number: ")

string_1 = raw_input("Please input the first half of the word: ")
string_2 = raw_input("please input the second half of the word: ")

print "the words you input is '" + string_1 + string_2 + "'"
print "the result of the 2 numbers is:", number_1 * number_2