Book Image

Python Fundamentals

By : Ryan Marvin, Mark Nganga, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Nganga, Amos Omondi

Overview of this book

After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions. As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation. By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.
Table of Contents (12 chapters)
Python Fundamentals
Preface

Accessing Tuple Elements


Tuples give us various ways to access their elements. These are as follows:

  • Indexing

  • Slicing

Indexing

Similar to lists, we can use the index operator [] to access an element in a tuple by using its index. Tuple indices start at zero, just like those of lists.

All of these parameters are optional, and this is how they work:

  • Start index: The index at which to start the slicing. The element at this index is included in the slice. If this parameter is absent, it is assumed to be zero, and thus, the slicing starts at the beginning of the tuple.

  • Stop index: The index at which to stop slicing. The element at this index is not included in the slice. This means that the last item in this slice will be the one just before the stop index. If this parameter is absent, the slice ends at the very end of the tuple.

  • Increment: This determines how many steps to take in the tuple when creating the slice. If this parameter is absent, it is assumed to be one.

Exercise 23: Accessing Tuple Elements...