whilefor
Use input to get numerical data from the
user,
name = input( "Prompt" )
This displays Prompt and then reads a line
of input. By default the line is read from the keyboard, and
reading is triggered when the Enter key is pressed.
input evaluates the line it reads as a Python
expression and stores the result as name.
Example:
distance = input( "Enter the distance in miles: " )
Notes:
". This
results in better spacing when the user enters their
value.Processing is specified in Python using assignment statements with the form,
name = expression
The value of expression is evaluated and
stored in memory as name.
Notes:
name must appear on the left and
expression on the right, i.e. you
cannot write,
expression = name
Arithmetic processing is expressed using standard mathematical notation within the limitations imposed by a keyboard, e.g.
3*(x + y)**4
Arithmetic Operators:
| + | addition |
|---|---|
| - | subtraction |
| / | division |
| % | modulo (remainder), e.g. 19 % 4 → 3 |
| * | multiplication |
| ** | exponentiation, e.g. 2 ** 4 → 16 |
In addition the abs() function for absolute value
is built-in, and less common mathematical functions can be
accessed by importing the math library, e.g.
import math
print "The value of pi is", math.pi
print "The square root of pi is", math.sqrt(math.pi)
Use print to display stored values:
print list
displays the value of each item in the list. Puts a space between each pair of values. Example:
print "The answer is", 5 + 2, "."
displays:
The answer is 7 .
Appending a comma to the end of a print statement holds the current output line open, e.g. the code,
print "The answer is",
print 5 + 2, "."
displays
The answer is 7 .
on a single line.
For more control over output appearance embed formatting codes into output strings. See section 2.2.6.2 String Formatting Operations of the Python Library Reference for the gory details.
ifUse if to execute one block of code or another,
but not both.
if test:
statement
elif test:
statement
else:
statement
N.B. the elif and else statements
are optional as shown in the first two examples below.
Examples:
if x < 0:
print x, "is negative"
if flip == 1:
print "You got heads"
else:
print "You got tails"
if num < 0:
print "The number", num, "is negative."
elif num == 0:
print "The number", num, "is neither positive nor negative."
else:
print "The number", num, "is positive."
Look like standard arithmetic comparisons (but again within the limitations imposed by using a standard keyboard), e.g.
x <= 4
Relational Operators:
| < | less than, e.g 5 < 3 → False |
|---|---|
| <= | less than or equal to, e.g. 3 <= 5 → True |
| == | equal, e.g. 5 == 5 → True |
| != | not equal, e.g. 5 != 5 → False |
| >= | greater than or equal to, e.g. 5 >= 3 → True |
| > | greater than, e.g. 4 > 5 → False |
| and |
a and b is only True when a is True
and b is True |
|---|---|
| or |
a or b is True when a is True, or
when b is True, or when both a and
b are True |
| not |
not a is True when a is False |
whileUse while to execute a block of code multiple
times.
while test:
statement
Common components:
x = 1 # initialization expression
while x < 10: # test expression
print x
x = x + 1 # increment expression
Input validation:
num = input( "Enter a number between 1 and 100: " )
while num < 1 or num > 100:
print "Oops, your input value (", num, ") is out of range."
num = input( "Be sure to enter a value between 1 and 100: " )
Repeating a program:
again = "y"
while again == "y" or again == "Y" or again == "yes" or again == "Yes":
#
# Put the body of your program here
#
again = raw_input( "Play again (y/n)? " )
print "Thanks for playing"
forUse for to repeat a block of code for every
member of a list.
print "Table of squares for 1-10"
print " x x**2"
for x in (1,2,3,4,5,6,7,8,9,10):
print "%5d%8d", % (x, x**2)
range functionThe built-in function range creates arithmetic
lists, and is often used to produce the lists to drive for
loops.
range(start_value, stop_value, stepsize)
Example:
>>> range(1, 20, 5)
[1, 6, 11, 16]
Notes:
Using range the example above can be
rewritten,
print "Table of squares for 1-10"
print " x x**2"
for x in range(1,11):
print "%5d%8d", % (x, x**2)