A maior rede de estudos do Brasil

Grátis
433 pág.
Apostila PyOpenGL

Pré-visualização | Página 6 de 50

serve as valuable comments and reminders, particularly in long 
programs. Do NOT skimp on remark statements. Use them wherever and whenever 
you need to make a note to yourself (or others) about what your program is doing7, the 
meaning of the variables you are using, and/or your intentions at that point in the 
program. Now the next line: 
 
import string 
 
 "import" statements bring in new commands that you can use to extend the 
Python language. They are almost always placed at the beginning of the program. The 
import string command adds some neat string handling commands for our 
programming pleasure. In Python, a string is any chain of letters or a mixture of letters 
and numbers.8 
 
i = input("Please enter the upper bound: ") 
 
 When Python encounters an input() statement, it stops and waits for the user 
to type something. Here, whatever the user types is stored in the variable i for later use 
when the Enter key is pressed.9 The "=" sign acts as an assignment statement in this 
line of code. Note that whatever you type between the " " is printed on the console 
screen. 
 
for n in range(i): 
 
 Computers are excellent at repetitive tasks. They never get tired! A for 
statement is a loop. A loop is a repetitive process a bit like a Ferris wheel… it goes 
around and around as many times as we specify. In Python, the indented statements 
below the for statement will be looped or iterated.10 In this case, we will loop through 
those indented statements as many times as the value we entered for i. If we entered 
1000 for i in the input statement, the variable n will take on ALL the values from 0 to 
99911 and we’ll instruct the computer to examine every number in this range to see if it’s 
a Super-3 number. How does the computer examine the numbers? Look at the next 
line of Python code: 
 
 x = 3*n**3 
 
 
7 You skip using remarks at your own peril. If you program anything of any complexity and try to 
figure out later what you’ve written, the program may as well have been written by someone else! 
8 "Hello" is a string. 1234 is a number. "R2D2" is a string. 3.1415 is a number. 
9 We can use any variable we wish as long as we don’t use a word that Python already knows. 
10 The word "iteration" is synonymous with repetition or looping. 
11 Those crazy computer scientists. They begin counting with 0. Go figure. 
 19
 This statement takes the values we generate for n (0 to 999) and one at a time 
will cube each of those values and then multiply each cubed value by 3. The n**3 is 
the cubing process, so you can infer that the double asterisk (**) raises a number to a 
power. The single asterisk (*) is the multiplication sign. Just as in algebra, the power is 
applied first, followed by the multiplication.12 The results of the calculation 3*n**3 is 
then stored in the variable x. As mentioned in the footnote, there is nothing sacred 
about the use of the variable n in this program. We could just have easily used 
testnumber or super3candidate13 as variables instead of x and n. Usually you choose 
variables with meaningful names and if needed, use remark statements to remind 
everyone how the variables are being used. For example, I might use the word endit 
to store a variable associated with terminating a program or function. Anyway… at this 
point, x contains the value of our calculation. 
 
 At this point, we need to see if n, whatever it is, is a Super-3 number. How do 
we do this? We know that a Super-3 number is an integer that when cubed and 
multiplied by 3 results in a value with a "333" somewhere in the number. We could look 
at every number visually and decide for ourselves whether or not it fits the Super-3 
conditions, but that would be tedious (and silly). We'll let the computer make the 
decision for us. Computers are excellent at making decisions, but we must first 
formulate the decision statement properly. Look at the next line: 
 
 if string.find(str(x), "333") <> -1: 
 
 Decision or conditional statements usually start with if and every indented line 
under the if statement is included in the conditional statement or block. Look at the 
following pseudo-code example: 
 
 if statement_true: 
 do something remarkable 
 
Generally the meaning is as follows: "if the statement is true, do something. if it’s false, 
ignore me". An if block of code makes decisions! You may have been warned as a 
child to "Look both ways before you cross the street!". Well, that is certainly an 
important instruction, but that alone is not enough to insure our safety. Somewhere in 
our brain we must have an if block of code such as: 
 
 if car_is_approaching: 
 stay on the sidewalk 
 
Think about this! How important are such decisions in our own life? Really, computers 
are nothing more than machines with the capability of looping endlessly through code, 
making as many decisions as we care to write. 
 
 
12 The order of arithmetic is the same in algebraic programming as in your math class. Please 
Excuse My Dear Aunt Sally… parentheses, exponentiation, multiplication, division, addition, 
subtraction. 
13 Python distinguishes between upper and lower case variables. The variable 'Cat' is different 
from the variable 'cat'. Be careful with your spelling! 
 20
Now back to our super3.py program. Let’s take this if string statement apart 
piece by piece. The string.find command is one of the neat string commands we 
can use after the import string statement. It allows us to search strings for patterns 
of letters, symbols, or numbers. In parentheses, we see str(x), which temporarily 
converts the number stored in x (calculated from the previous line of code) to a string so 
we can search it. “333” is the pattern we are trying to match. If a “333” pattern is 
NOT found in x, the string.find command returns (or equals) –1. So, in English, this 
statement says: if in str(x) we find a “333” pattern then string.find will NOT 
equal –1 (“<>” means NOT equal... we could also use "!=". Try it!) and that would make 
the statement TRUE (string.find is indeed NOT equal to -1). If in str(x) we 
DON’T find a “333”, that means that string.find equals –1 and the statement is 
FALSE.14 Remember that a FALSE statement means that we will ignore any indented 
code in if block. The colon symbol “:” finishes the “if” statement. 
 
 You may be wondering what happens if the string.find function actually does 
find the search string "333"? Does the function return a value other than -1? Well, 
obviously it does or our program would not work! But what value does the 
string.find function return if it finds a match and why can't we use THAT value in our 
if statement (if string.find(str(x), "333") == value:)? It turns out that 
the string.find function does not return the same value every time the search string 
is matched. The value the function returns is the location or index in the string (or 
number converted to a string in this case) of the first character in the search string. For 
example, the first Super-3 number is 261. This translates to a Super-3 value of 
53338743. The first "3" of the "333" search string in this number is located in the "1" 
location... remember that we start counting from 0 in computer science; the first number 
is in position 0! So, string.find would return a value of 1 for the number 53338743. 
The second Super-3 number is 462, which has a Super-3 value of 295833384. For this 
value, string.find would return a 4, indicating the position or index of the first 
character of the search string, again starting from 0. So we can't simply check for a 
single value for string.find in the if statement to indicate that a match has been 
found. We'll explore this concept further in the exercises at

Crie agora seu perfil grátis para visualizar sem restrições.