The Python Apprentice
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Compartir libro
  1. 352 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

The Python Apprentice

Robert Smallshire, Austin Bingham

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es The Python Apprentice un PDF/ePUB en línea?
Sí, puedes acceder a The Python Apprentice de Robert Smallshire, Austin Bingham en formato PDF o ePUB, así como a otros libros populares de Informatik y Programmierung in Python. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2017
ISBN
9781788298667
Edición
1
Categoría
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...

Índice