The Python Apprentice
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Share book
  1. 352 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Book details
Book preview
Table of contents
Citations

About This Book

Learn the Python skills and culture you need to become a productive member of any Python project.About This Book• Taking a practical approach to studying Python• A clear appreciation of the sequence-oriented parts of Python• Emphasis on the way in which Python code is structured• Learn how to produce bug-free code by using testing toolsWho This Book Is ForThe Python Apprentice is for anyone who wants to start building, creating and contributing towards a Python project. No previous knowledge of Python is required, although at least some familiarity with programming in another language is helpful.What You Will Learn• Learn the language of Python itself• Get a start on the Python standard library• Learn how to integrate 3rd party libraries• Develop libraries on your own• Become familiar with the basics of Python testingIn DetailExperienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it's not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.Style and approachWe introduce topics gently and then revisit them on multiple occasions to add the depth required to support your progression as a Python developer. We've worked hard to structure the syllabus to avoid forward references. On only a few occasions do we require you to accept techniques on trust, before explaining them later; where we do, it's to deliberately establish good habits.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is The Python Apprentice an online PDF/ePUB?
Yes, you can access The Python Apprentice by Robert Smallshire, Austin Bingham in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung in Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781788298667

Exploring Built-in Collection types

We've already encountered some of the built-in collections
  • str – the immutable string sequence of Unicode code points
  • list – the mutable sequence of objects
  • dict – the mutable dictionary mapping from immutable keys to mutable
    objects
We've only scratched the surface of how these collections work, so we'll explore their powers in greater depth in this chapter. We'll also introduce three new built-in collections types:
  • tuple - the immutable sequence of objects
  • range - for arithmetic progressions of integers
  • set - a mutable collection of unique, immutable objects
We won't cover the bytes type any further here. We've already discussed its essential differences with str, and most of what we learn about str can also be applied to bytes.
This is not an exhaustive list of Python collection types, but it's completely sufficient for the overwhelming majority of Python 3 programs you'll encounter in the wild or are likely to write yourself.
In this chapter we'll be covering these collections in the order mentioned above, rounding things off with an overview of the protocols that unite these collections and which allow them to be used in consistent and predictable ways.

tuple – an immutable sequence of objects

Tuples in Python are immutable sequences of arbitrary objects. Once created, the objects within them cannot be replaced or removed, and new elements cannot be added.

Literal tuples

Tuples have a similar literal syntax to lists, except that they are delimited by parentheses rather than square brackets. Here is a literal tuple containing a string, a float and an integer:
>>> t = ("Norway", 4.953, 3)
>>> t
('Norway', 4.953, 3)

Tuple element access

We can access the elements of a tuple by zero-based index using square brackets:
>>> t[0]
'Norway'
>>> t[2]
3

The length of a tuple

We can determine the number of elements in the tuple using the built-in len() function:
>>> len(t)
3

Iterating over a tuple

We can iterate over it using a for-loop:
>>> for item in t:
>>> print(item)
Norway
4.953
3

Concatenating and repetition of tuples

We can concatenate tuples using the plus operator:
>>> t + (338186.0, 265E9)
('Norway', 4.953, 3, 338186.0, 265000000000.0)
Similarly, we can repeat them using the multiplication operator:
>>> t * 3
('Norway', 4.953, 3, 'Norway', 4.953, 3, 'Norway', 4.953, 3)

Nested tuples

Since tuples can contain any object, it's perfectly possible to have nested tuples:
>>> a = ((220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368))
We use repeated application of the indexing operator to get to the inner elements:
>>> a[2][1]
2924

Single-element tuples

Sometimes a single element tuple is required. To write this, we can't just use a simple object in parentheses. This is because Python parses that as an object enclosed in the precedence controlling parentheses of a math expression:
>>> h = (391)
>>> h
391
>>> type(h)
<class 'int'>
To create a single-element tuple we make use of the trailing comma separator which, you'll recall, we're allowed to use when specifying literal tuples, lists, and dictionaries. A single element with a trailing comma is parsed as a single element tuple:
>>> k = (391,)
>>> k
(391,)
>>> type(k)
<class 'tuple'>

Empty tuples

This leaves us with the problem of how to specify...

Table of contents