The Python Apprentice
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Condividi libro
  1. 352 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
The Python Apprentice è disponibile online in formato PDF/ePub?
Sì, puoi accedere a The Python Apprentice di Robert Smallshire, Austin Bingham in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatik e Programmierung in Python. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2017
ISBN
9781788298667
Edizione
1
Argomento
Informatik

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...

Indice dei contenuti