Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Python Apprentice

You're reading from   The Python Apprentice Introduction to the Python Programming Language

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781788293181
Length 352 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Austin Bingham Austin Bingham
Author Profile Icon Austin Bingham
Austin Bingham
Robert Smallshire Robert Smallshire
Author Profile Icon Robert Smallshire
Robert Smallshire
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting started FREE CHAPTER 2. Strings and Collections 3. Modularity 4. Built-in types and the object model 5. Exploring Built-in Collection types 6. Exceptions 7. Comprehensions, iterables, and generators 8. Defining new types with classes 9. Files and Resource Management 10. Unit testing with the Python standard library 11. Debugging with PDB 12. Afterword – Just the Beginning
13. Virtual Environments 14. Packaging and Distribution 15. Installing Third-Party Packages

Obtaining and installing Python 3

There are two major versions of the Python language, Python 2 which is the widely deployed legacy language and Python 3 which is the present and future of the language. Much Python code will work without modification between the last version of Python 2 (which is Python 2.7 (https://www.python.org/download/releases/2.7/)) and recent versions of Python 3, such as Python 3.5 (https://www.python.org/download/releases/3.5.1/). However, there are some key differences between the major versions, and in a strict sense the languages are incompatible. We'll be using Python 3.5 for this book, but we'll point out key differences with Python 2 as we go. It's also very likely that, this being a book on Python fundamentals, everything we present will apply to future versions of Python 3, so don't be afraid to try those as they become available.

Before we can start programming in Python we need to get hold of a Python environment. Python is a highly portable language and is available on all major operating systems. You will be able to work through this book on Windows, Mac or Linux, and the only major section where we diverge into platform specifics is coming right up — as we install Python 3. As we cover the three platforms, feel free to skip over the sections which aren’t relevant for you.

Windows

The following are the steps to be performed for Windows platform:

  1. For Windows you need to visit the official Python website, and then head to the Downloads page by clicking the link on the left. For Windows you should choose one of the MSI installers depending on whether you're running on a 32- or 64-bit platform.

  2. Download and run the installer.

  3. In the installer, decide whether you only want to install Python for yourself, or for all users of your machine.

  4. Choose a location for the Python distribution. The default will be in
    C:\Python35 in the root of the C: drive. We don't recommended installing
    Python into Program Files because the virtualized file store used to
    isolate applications from each other in Windows Vista and later can interfere
    with easily installing third-party Python packages.

  5. On the Customize Python page of the wizard we recommend keeping the
    defaults, which use less than 40 MB of space.

  6. In addition to installing the Python runtime and standard library, the installer will register various file types, such as *.py files, with the Python interpreter.

  7. Once Python has been installed, you'll need to add Python to your system PATH environment variable. To do this, from the Control Panel choose System and Security, then System. Another way to get here easily is to hold down your Windows key and press the Break key on your keyboard. Using the task pane on the left choose Advanced System Settings to open the Advanced tab of the System Properties dialog. Click Environment variables to open the child dialog.

  8. If you have Administrator privileges you should be able to add the paths
    C:\Python35 and C:\Python35\Scripts to the semicolon separated list of
    entries associated with the PATH system variable. If not, you should be
    able to create, or append to, a PATH variable specific to your user
    containing the same value.

  9. Now open a new console window — either Powershell or cmd will work fine
    — and verify that you can run python from the command line:

> python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Welcome to Python!

The triple arrow prompt shows you that Python is waiting for your input.

At this point you might want to skip forward whilst we show how to install Python on Mac and Linux.

macOS

  1. For macOS you need to visit the official Python website at http://python.org. Head to the Download page by clicking the link on the left. On the Download page, find the macOS installer matching your version of macOS and click the link to download it.

  2. A DMG Disk Image file downloads, which you open from your Downloads
    stack or from the Finder.

  3. In the Finder window that opens you will see the file Python.mpkg
    multipackage installer file. Use the "secondary" click action to open the
    context menu for that file. From that menu, select Open.

  4. On some versions of macOS you will now be told that the file is from an
    unidentified developer. Press the Open button on this dialog to continue
    with the installation.

  5. You are now in the Python installer program. Follow the directions, clicking through the wizard.

  6. There is no need to customize the install, and you should keep the standard
    settings. When it's available, click the Install button to install Python.
    You may be asked for your password to authorize the installation. Once the
    installation completes click Close to close the installer.

  7. Now that Python 3 is installed, open a terminal window and verify that you
    can run Python 3 from the command line:

> python
Python 3.5.0 (default, Nov 3 2015, 13:17:02)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Welcome to Python!

The triple arrow prompt shows that Python is waiting for your input.

Linux

  1. To install Python on Linux you will want to use your system's package
    manager. We'll show how to install Python on a recent version of Ubuntu, but the process is very similar on most other modern Linux distributions.

  2. On Ubuntu, first start the Ubuntu Software Center. This can usually be run by clicking on it's icon in the launcher. Alternatively, you can run it from the dashboard by searching on Ubuntu Software Center and clicking the selection.

  3. Once you're in the software center, enter the search term python 3.5 in the search bar in the upper right-hand corner and press return.

  4. One of the results you'll get will say Python (v3.5) with Python Interpreter (v3.5) in smaller type beneath it. Select this entry and click the Install button that appears.

  5. You may need to enter your password to install the software at this point.

  6. You should now see a progress indicator appear, which will disappear when installation is complete.

  7. Open a terminal (using Ctrl+Alt+T) and verify that you can run Python 3.5 from the command line:

$ python3.5
Python 3.5.0+ (default, Oct 11 2015, 09:05:38)
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Welcome to Python!

The triple arrow prompt shows you that Python is waiting for your input.

You have been reading a chapter from
The Python Apprentice
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781788293181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime