Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Slicing

As with strings, in order to get a single value from the list, you need to use its index in the square brackets after the value, as shown in the following code snippet:

>>> fruits[0]'banana'

You can also obtain a subset of list values by using slices: intervals of indexes that are defined by two numbers and separated by a colon. The numbers represent start and end indices; the former number is inclusive, but the latter number is not. If one or both numbers are missing, then Python assumes those to be the ends of the list. Here is an example:

>>> fruits[0:2]['banana', 'apple']>>> fruits[:2]['banana', 'apple']

In both cases, we're pulling the first two elements. In this case, it doesn't matter whether we include 0 or not.

This is exactly ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content