Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.6 What Is a Programming Language?

If we were to have to write programs as sequences of numbers we wouldn't get very far. It would be so tedious to program that no one would want to be a programmer. In the spring of 2006 Money Magazine ranked Software Engineer [4] as the number one job in America in terms of overall satisfaction which included things like compensation, growth, and stress-levels. So it must not be all that tedious.

A programming language is really a set of tools that allow us to program at a much higher level than the 0's and 1's that exist at the lowest levels of the computer. Python and the Wing IDE provides us with a couple of tools. The lower right corner of the Wing IDE has a tab labeled Python Shell. The shell allows programmers to interact with the Python interpreter. The interpreter is a program that interprets the programs we write. If you have a Mac or Linux computer you can also start the Python interpreter by opening up a terminal window. If you use Windows you can start a Command Prompt by looking under the Accessories program group. Typing python at a command prompt starts a Python interpreter as shown in Fig. 1.11.

Consider computing the area of a shape constructed of overlapping regular polygons. In Fig. 1.12 all angles are right angles and all distances are in meters. Our job is to figure out the area in square meters. The lighter lines in the middle help us figure out how to compute the area. We can compute the area of the two rectangles and then subtract one of the overlapping parts since otherwise the overlapping part would be counted twice.

This can be computed on your calculator of course. The Python Shell is like a calculator and Fig. 1.11 shows how it can be used to compute the area of the shape. The first line sets a variable called R1_width to the value of 10. Then R1_height is set to 8. We can store a value in memory and give it a name. This is called an assignment statement. Your calculator can store values. So can Python. In Python these values can be given names that mean something in our program. R1_height is the name we gave to the height of the R1 rectangle. Anytime we want to retrieve that value we can just write R1_height and Python will retrieve its value for us.

Fig. 1.11 The Python shell

Fig. 1.12 Overlapping rectangles

Practice 1.6 Open up the Wing IDE or a command prompt and try out theassignment and print statements shown in Fig. 1.11. Make sure to type thestatements into the python shell. You DO NOT type the >>>. That is thePython shell prompt and is printed by Python. Notice that you can't fix a lineonce you have pressed enter. This will be remedied soon.

Practice 1.7 Take a moment and answer these questions from the materialyou just read.1. What is an assignment statement?2. How do we retrieve a value from memory?3. Can we retrieve a value before it has been stored? What happens when wetry to do that?

Interacting directly with the Python shell is a good way to quickly see how something works. However, it is also painful because mistakes can't be undone. In the next section we'll go back to writing programs in an editor so they can be changed and run as many times as we like. In fact, this is how most Python programming is done. Write a little, then test it by running it. Then write a little more and run it again. This is called prototyping and is an effective way to write programs. You should write all your programs using prototyping while reading this text. Write a little, then try it. That's an effective way to program and takes less time than writing a lot and then trying to figure out what went wrong.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science