• No se han encontrado resultados

I. 1 ¿Qué dice la Constitución? Interpretación y aplicación del texto constitucional

I. 2. La dimensión extraordinaria Constitución y poder político

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why. For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they are marked with the # symbol:

# compute the percentage of the hour that has elapsed

percentage = (minute * 100) / 60

In this case, the comment appears on a line by itself. You can also put comments at the end of a line:

percentage = (minute * 100) / 60 # caution: integer division

Everything from the # to the end of the line is ignored—it has no effect on the program. The message is intended for the programmer or for future programmers who might use this code. In this case, it reminds the reader about the ever-surprising behaviour of integer division.

5.4

Glossary

local variable: A variable defined inside a function. A local variable can only be used inside its function.

stack diagram: A graphical representation of a stack of functions, their variables, and the values to which they refer.

frame: A box in a stack diagram that represents a function call. It contains the local variables and parame- ters of the function.

traceback: A list of the functions that are executing, printed when a runtime error occurs. A traceback is also commonly referred to as a stack trace, since it lists the functions in the order in which they are stored in the runtime stack.

comment: Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.

5.5

Laboratory exercises

1. Create a script named params.py with the following contents:

def print_param(x):

print x x = "Hello"

print_param("Goodbye")

Record the output of the program here:



zend



Explain the output in terms of what’s happening to the variable x and the parameter x:



zend



2. Create a script called changeparam.py with the following contents:

def change_param(x): x = "Goodbye"

x = "Hello"

change_param(x)

print x

Record the output of the program here:



zend



Explain how the output comes about:



zend



3. Place a comment character at the start of the last line (the one containing print x) in either of the last two programs and record what happens when you rerun it.



zend



def print_all(first, second, third):

print first, second, third

def print_reverse(first, second, third):

print third, second, first

def print_add(first, second, third):

print first + second + third

def print_add_reverse(first, second, third):

print third + second + first one = 'fish'

two = 'and'

three = 'chips'

print_all(one, two, three) print_reverse(one, two, three) print_add(one, two, three)

print_add_reverse(one, two, three) print_all(1, 2, 3)

print_reverse(1, 2, 3) print_add(1, 2, 3)

print_add_reverse(1, 2, 3)

Put your answer in the space below, and then type in and run the code to check that you are right.



zend



5. Type the following code into a file called input fun.py:

print "Enter some text:"

text = raw_input()

print "You entered: " + text

print "Enter some text:"

text = raw_input()

print "You entered: " + text

print "Enter some text:"

text = raw_input()

print "You entered: " + text

6. Create a script called convert.py which uses the function raw input to read a string from the keyboard. Attempt to convert the string to a float using float(x) and also to an integer using int(x). Print out the resulting float and integer. Test the script with the following input:

10 103 1e12 0.000001 blah

Explain the output produced:



zend



7. Try to complete up to level nine on the game you played in the previous labs.

Lecture 6

Conditionals

6.1

Boolean values and expressions

The Python type for storing true and false values is called bool, named after the British mathematician, George Boole. George Boole created Boolean algebra , which is the basis of all modern computer arithmetic. There are only two boolean values : True and False. Capitalization is important, since true and false are not boolean values.

>>> type(True) <type ’bool’> >>> type(true)

Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ’true’ is not defined

A boolean expression is an expression that evaluates to a boolean value. The operator == compares two values and produces a boolean value:

>>> 5 == 5 True >>> 5 == 6 False

In the first statement, the two operands are equal, so the expression evaluates to True; in the second state- ment, 5 is not equal to 6, so we get False. The == operator is one of the comparison operators; the others are:

x != y # x is not equal to y

x > y # x is greater than y

x < y # x is less than y

x >= y # x is greater than or equal to y

x <= y # x is less than or equal to y

Although these operations are probably familiar to you, the Python symbols are different from the math- ematical symbols. A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = is an assignment operator and == is a comparison operator. Also, there is no such thing as =< or =>.

Documento similar