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

1.9 Syntax Errors

Not every error is found using a debugger. Sometimes errors are syntax errors. A syntax error occurs when we write something that is not part of the Python language. Many times a syntax error can occur if we forget to write something. For instance, if we forget a parenthesis or a double quote is left out it will not be a correct Python program. Syntax errors are typically easier to find than bugs in our program because Python can flag them right away for us. These errors are usually highlighted right away by the IDE or interpreter. Syntax errors are those errors that are reported before the program starts executing. You can tell its a syntax error in Wing because there will not be any Stack Data. Since a syntax error shows up before the program runs, the program is not currently executing and therefore there is not state information in the stack data. When a syntax error is reported the editor or Python will typically indicate the location of the error after it actually occurs so the best way to find syntax errors is to look backwards from where the error is first reported.

Example 1.4 Forgetting a parenthesis is a common syntax error.p r i n t (R2_heightThis is not valid syntax in Python since the right parenthesis is missing. Ifwe were to try to run a Python program that contains this line, the Pythoninterpreter complains that this is not valid syntax. Figure 1.14 shows how theWing IDE tells us about this syntax error. Notice that theWing IDE announcesthat the syntax error occurs on the line after where it actually occurred.

There are other types of errors we can have in our programs. Syntax errors are perhaps the easiest errors to find. All other errors can be grouped into the category of run-time errors. Syntax errors are detected before the program runs. Run-time errors are detected while the program is running. Unfortunately, run-time errors are sometimes much harder to find than syntax errors. Many run-time errors are caused by the use of invalid operations being applied to values in our programs. It is important to understand what types of values we can use in our programs and what operations are valid for each of these types. That's the topic of the next section.

Fig. 1.14 A syntax error

1.10 Types of Values

Earlier in this chapter we found that bytes in memory can be interpreted in different ways. The way bytes in memory are interpreted is determined by the type of the value or object and the operations we apply to these values. Each value in Python is called an object. Each object is of a particular type. There are several data types in Python. These include integer (called int in Python), float, boolean (called bool in Python), string (called str in Python), list, tuple, set, dictionary (called dict in Python), and None.

In the next chapters we'll cover each of these types and discuss the operations that apply to them. Each type of data and the operations it supports is covered when it is needed to learn a new programming skill. The sections on each of these types can also serve as a reference for you as you continue working through the text. You may find yourself coming back to the sections describing these types and their operations over and over again. Reviewing types and their operations is a common practice among programmers as they design and write new programs.

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