Skip to main content Accessibility help
×
Hostname: page-component-848d4c4894-xm8r8 Total loading time: 0 Render date: 2024-06-17T22:52:03.760Z Has data issue: false hasContentIssue false

2 - The core Python language I

Published online by Cambridge University Press:  05 February 2016

Christian Hill
Affiliation:
University College London
Get access

Summary

The Python shell

This chapter introduces the syntax, structure and data types of the Python programming language. The first few sections do not involve writing much beyond a few statements of Python code and so can be followed using the Python shell. This is an interactive environment: the user enters Python statements that are executed immediately after the Enter key is pressed.

The steps for accessing the “native” Python shell differ by operating system. To start it from the command line, first open a terminal using the instructions from Section 1.4 and type python.

To exit the Python shell, type exit().

When you start the Python shell, you will be greeted by a message (which will vary depending on your operating system and precise Python version). On my system, the message reads:

Python 3.3.5 |Anaconda 2.0.1 (x86\_64)| (default, Mar 10 2014, 11:22:25)

[GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>>

The three chevrons (>>>) are the prompt, which is where you will enter your Python commands. Note that this book is concerned with Python 3, so you should check that the Python version number reported on the first line is Python 3.X.Y where the precise values of the minor version numbers X and Y should not be important.

Many Python distributions come with a slightly more advanced shell called IDLE, which features tab-completion, and syntax highlighting (Python keywords are colored specially when you type them). We will pass over the use of this application in favor of the newer and more advanced IPython environment, discussed in Chapter 5.

It is also possible for many installations (especially on Windows) to start a Python shell directly from an application installed when you install the Python interpreter itself. Some installations even add a shortcut icon to your Desktop which will open a Python shell when you click on it.

Numbers, variables, comparisons and logic

Types of numbers

Among the most basic Python objects are the numbers, which come in three types: integers (type: int), floating point numbers (type: float) and complex numbers (type: complex).

Integers

Integers are whole numbers such as 1, 8, −72 and 3847298893721407. In Python 3, there is no limit to their magnitude (apart from the availability of your computer's memory). Integer arithmetic is exact.

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 I
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.002
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 I
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.002
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 I
  • Christian Hill, University College London
  • Book: Learning Scientific Programming with Python
  • Online publication: 05 February 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781139871754.002
Available formats
×