Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-9q27g Total loading time: 0 Render date: 2024-07-22T06:26:19.358Z Has data issue: false hasContentIssue false

4 - The core Python language II

Published online by Cambridge University Press:  05 February 2016

Christian Hill
Affiliation:
University College London
Get access

Summary

This chapter continues the introduction to the core Python language started in Chapter 2 with a description of Python error handling with exceptions, the data structures known as dictionaries and sets, some convenient and efficient idioms to achieve common tasks, and a survey of some of the modules provided in the Python standard library. Finally, we present a brief introduction to object-oriented programming with Python.

Errors and exceptions

Python distinguishes between two types of error: Syntax errors and other exceptions. Syntax errors are mistakes in the grammar of the language and are checked for before the program is executed. Exceptions are runtime errors: conditions usually caused by attempting an invalid operation on an item of data. The distinction is that syntax errors are always fatal: there is nothing the Python compiler can do for you if your program does not conform to the grammar of the language. Exceptions, however, are conditions that arise during the running of a Python program (such as division by zero) and a mechanism exists for “catching” them and handling the condition gracefully without stopping the program's execution.

Syntax errors

Syntax errors are caught by the Python compiler and produce a message indicating where the error occurred. For example,

>>> for lambda in range(8):

File “<stdin>”, line 1

for lambda in range(8):

^

SyntaxError: invalid syntax

Because lambda is a reserved keyword, it cannot be used as a variable name. Its occurrence where a variable name is expected is therefore a syntax error. Similarly,

>>> for f in range(8:

File “<stdin>”, line 1

for f in range(8:

^

SyntaxError: invalid syntax

The syntax error here occurs because a single argument to the range built-in must be given as an integer between parentheses: the colon breaks the syntax of calling functions and so Python complains of a syntax error.

Because a line of Python code may be split within an open bracket (“()”, “[]”, or “﹛﹜”), a statement split over several lines can sometimes cause a SyntaxError to be indicated somewhere other than the location of the true bug.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2016

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • The core Python language II
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.004
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • The core Python language II
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.004
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • The core Python language II
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.004
Available formats
×