2 - The core Python language I
Published online by Cambridge University Press: 05 February 2016
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
- Learning Scientific Programming with Python , pp. 8 - 83Publisher: Cambridge University PressPrint publication year: 2016
- 1
- Cited by