Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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

Starting Python command line REPL

Now that Python is installed and running, you can immediately start using it. This is a good way to get to know the language, as well as a useful tool for experimentation and quick testing during normal development.

This Python command line environment is a Read-Eval-Print-Loop. Python will READ whatever input we type in, EVALuate it, PRINT the result and then LOOP back to the beginning. You'll often hear it referred to simply as the "REPL".

When started, the REPL will print some information about the version of Python you're running, and then it will give you a triple-arrow prompt. This prompt tells you that Python is waiting for you to type something.

Within an interactive Python session you can enter fragments of Python programs and see instant results. Let's start with some simple arithmetic:

>>> 2 + 2
4
>>> 6 * 7
42

As you can see, Python reads our input, evaluates it, prints the result, and loops around to do the same again.

We can assign to variables in the REPL:

>>> x = 5

Print their contents simply by typing their name:

>>> x
5

 Refer to them in expressions:

>>> 3 * x
15

Within the REPL you can use the special underscore variable to refer to the most recently printed value, this being one of very few obscure shortcuts in Python:

>>> _
15

Or you can use the special underscore variable in an expression:

>>> _ * 2
30
Remember that this useful trick only works at the REPL; the underscore doesn't have any special behavior in Python scripts or programs.

Notice that not all statements have a return value. When we assigned 5 to x there was no return value, only the side-effect of bringing the variable x into being. Other statements have more visible side-effects.

Try the following command:

>>> print('Hello, Python')
Hello, Python

You’ll see that Python immediately evaluates and executes this command, printing the string Hello, Python and returning you to another prompt. It's important to understand that the response here is not the result of the expression evaluated and displayed by the REPL, but is a side-effect of the print() function.

As an aside, print is one of the biggest differences between Python 2 and Python 3. In Python 3, the parentheses are required, whereas is Python 2 they were not. This is because in Python 3, print() is a function call. More on functions later.

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 $19.99/month. Cancel anytime
Banner background image