Cryptography Reference
In-Depth Information
ent in the language; and in which the reader won't get bogged down in housekeeping details (like #include
<stdio.h >-like statements in C, import java.util. *; statements in Java, etc.). A few languages fulfill
these requirements, and one that I like in particular is Python.
If you are not familiar with Python, or Python is long forgotten in the tomes of computing history when this
topic is read, the implementations in Python should be simple enough to easily reproduce in whatever language
you are interested in.
3.2.2 A Crash Course in Python
Weshallstartwithavery, very shortcourseinPythonsyntaxandafewtricks.Thisisbynomeansrepresentative
of all of Python, nor even using all of the features of any given function, but merely enables us to examine some
simple programs that will work and are easy to read.
A typical Python program looks like that shown in Listing 3-1 .
Listing 3-1 A simple program in Python. The brackets inside quotes indicate a space character
x = 5
y = 0
print "x =", x, " y = ", y
for i in range(0, 4):
y = y + x
print "x =", x, "
y =", y, "
i =", i
print "x =", x, "
y =", y
The output of the program in Listing 3-1 will be
x = 5 y = 0
x = 5 y = 5 i = 0
x = 5 y = 10 i = 1
x = 5 y = 15 i = 2
x = 5 y = 20 i = 3
x = 5 y = 20
We don't want to get bogged down in too many details, but a few things are worth noting. Assignments are done
with the simple = command, with the variable on the left and the value to put in on the right.
Program structure is determined by white space. Therefore the for loop's encapsulated code is all the code
following it with the same indentation as the line immediately following it — no semicolons or braces to muck
up.
The for loop is the most complicated thing here. It calls on the range function to do some of its work. For
our uses, range takes two arguments: the starting point (inclusive) and the ending point (not inclusive), so that
in Listing 3-1 , range ( 0, 4 ) will expand to [0, 1, 2, 3] (the Python code for an array).
The for loop uses the in word to mean “operate the following code one time for each element in the array,
assigning the current element of the array to the for -loop variable.” In Listing 3-1 , the for -loop variable is i .
The colon is used to indicate that the for line is complete and to expect its body immediately after.
 
 
Search WWH ::




Custom Search