Most of this is covered in Chapter 2 of both Practical Programming and Think Python
Throughout we will pay attention to problems and mistakes, both real and potential.
We will start class by using Python as an interactive calculator, working through a number of examples.
Values include
The operators on integers include +, -, *, /, **, %.
The biggest surprise is division. The best way to think about integer / and % is in terms of the old “dividend” and “remainder” calculation from grade school:
This integer division rule, common to many programming languages, has been changed in Python 3.
What happens with negative numbers? We’ll try it out in class and then figure out what is happening.
Floats are the Python types most like real numbers and most like what we see when we use a calculator.
Playing around with calculations like
will help illustrate float numbers and operators.
The ’e’ notation we will see in these examples is Python’s version of scientific notation.
Python can store a limited range of possible values, both in decimals and in the exponent.
Most calculators have one or several memory keys. Python, and all other programming languages, use “variables” as their memory.
We’ll start with a simple example of the area of a circle, typed in class. You will notice as we go through this that there is no output until we use a print statement.
Here is a more extensive example of computing the volume and surface area of a cylinder:
>>> pi = 3.14159
>>> radius = 2
>>> height = 10
>>> base_area = pi * radius ** 2
>>> volume = base_area * height
>>> surface_area = 2 * base_area + 2 * pi * radius * height
>>> print "volume is", volume, ", surface area is", surface_area
volume is 125.6636 , surface area is 150.79632
A variable is a name that has a value “associated” with it.
The value is substituted in for the variable when the variable appears on the right hand side of the =.
The value is assigned to the variable when the variable name appears on the left hand side of the =.
The operator = is an assignment of a value (calculated on the right side) to a variable (on the left). Thus, in the line
>>> base_area = pi * radius ** 2
Thus, the meaning of = in Python is quite different from the meaning of = in mathematics.
The statement
>>> base_area * height = volume
is not legal Python code. Try it!
It takes a while to get accustomed to the meaning of an assignment statement in Python.
Notice that our example variable names include letters and the _ (underscore) character.
Legal variable names in Python must
Characters that are none of these, including spaces, signal the end of a variable name.
Capital letters and small letters are different
We will look at many examples in class.
Not all variable names that follow the above rules are legal Python names.
The (only) exception is the set of “keywords” that have special meaning to Python and allow use to write more complicated operations — involving logic and repetition — than just calculating.
print is the first keyword we used in this lecture
You can get a list of Python keywords by typing
>>> import keyword
>>> print keyword.kwlist
Over the next few lectures, we will soon understand the detailed meaning of the . in the above statement.
Which of these are legal variable names?
import
56abc
abc56
car-talk
car_talk
car talk
Create 2 invalid variable names and 4 valid variable names from the _ character, the digit 0, and the letter a.
Which of these lines of code contain syntax errors?
pi = 3.14159
area = pi * r * r
r = 6.5
r + 5 = r_new
Write Python code that computes and prints the surface area and volume of a rectangular solid that has length 5, width 16.5, and height 12, all in inches. Convert the result to cubic feet. Use variables as appropriate. Test it by typing it into the Python interpreter.
In our volume and surface area calculation, the right hand side of the = in the line
>>> surface_area = 2 * base_area + 2 * pi * radius * height
is what’s known as an expression.
Expressions are formed from syntactically-legal combinations of values (the number 2), operators (+ and *), and variables (base_area, radius and height)
Expressions can be as simple as a single value, but Python allows them to be arbitrarily complicated.
Assignments of the form
>>> i = i + 1
are commonly seen in Python. We will take a careful look at what is happening here.
Python contains a short-hand for these:
>>> i += 1
These two statements are exactly equivalent.
Other mixed operators include
-= *= /=
but += is used most commonly for reasons that will gradually become clear.
Assuming you start with $100 and earn 5% interest each year, how much much will you have at the end of one year, two years and three years? Write Python expressions to calculate these, using variables as appropriate.
What is the output of the following Python code? Try to figure it out by hand before typing the statements into the Python interpreter.
x = 12
y = 7.4
x -= y
print x, y
y = y-x +7
z = 1
x *= 2 + z
print x, y
x += x*y
print x, y
What is the output of the following?
z = 2
z = z**2**3
print z
x = 4
x = (x**2)**3 + 6 - z / 4 * 2
print x