Book Image

The Python Workshop

By : Olivier Pons, Andrew Bird, Dr. Lau Cher Han, Mario Corchero Jiménez, Graham Lee, Corey Wade
Book Image

The Python Workshop

By: Olivier Pons, Andrew Bird, Dr. Lau Cher Han, Mario Corchero Jiménez, Graham Lee, Corey Wade

Overview of this book

Have you always wanted to learn Python, but never quite known how to start? More applications than we realize are being developed using Python because it is easy to learn, read, and write. You can now start learning the language quickly and effectively with the help of this interactive tutorial. The Python Workshop starts by showing you how to correctly apply Python syntax to write simple programs, and how to use appropriate Python structures to store and retrieve data. You'll see how to handle files, deal with errors, and use classes and methods to write concise, reusable, and efficient code. As you advance, you'll understand how to use the standard library, debug code to troubleshoot problems, and write unit tests to validate application behavior. You'll gain insights into using the pandas and NumPy libraries for analyzing data, and the graphical libraries of Matplotlib and Seaborn to create impactful data visualizations. By focusing on entry-level data science, you'll build your practical Python skills in a way that mirrors real-world development. Finally, you'll discover the key steps in building and using simple machine learning algorithms. By the end of this Python book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Python.
Table of Contents (13 chapters)

String Indexing and Slicing

Indexing and slicing are crucial parts of programming. In data analysis, indexing and slicing DataFrames is essential to keep track of rows and columns, something we will practice in Chapter 10, Data Analytics with pandas and NumPy. The mechanics behind indexing and slicing dataFrames is the same as indexing and slicing strings, which we will learn in this chapter.

Indexing

The characters of Python strings exist in specific locations; in other words, their order counts. The index is a numerical representation of where each character is located. The first character is at index 0, the second character is at index 1; the third character is at index 2, and so on.

Note

We always start at 0 when indexing.

Consider the following string:

destination = 'San Francisco'

'S' is in the 0th index, 'a' is in the 1st index, 'n' is in the 2nd index, and so on. The characters of each index are accessed using bracket notation as follows:

destination[0]

You should get the following output:

'S'

To access the data from the first index, enter the following:

destination[1]

You should get the following output:

'a'

To access the data from the second index, enter the following:

destination[2]

You should get the following output:

'n'

The character value for San Francisco and the corresponding index count is shown in Figure 1.15:

Figure 1.15: Diagrammatic representation of the character values and the corresponding positive index values

Figure 1.15: Diagrammatic representation of the character values and the corresponding positive index values

Now, try adding a -1 as the index value and observe the output:

destination[-1]

You should get the following output:

'o'

Note

Negative numbers start at the end of the string. (It makes sense to start with -1 since -0 is the same as 0.)

To access the data from the rear of San Francisco, we use the negative sign in this case -2:

destination[-2]

You should get the following output:

'c'

The following figure 1.16 mentions the characters sco from the word Francisco, and the corresponding index count:

Figure 1.16: Index value for the negative values for San Francisco

Figure 1.16: Index value for the negative values for San Francisco

Here is one more example:

bridge = 'Golden Gate'
bridge[6]

You should get the following output:

' '

You may be wondering whether you did something wrong because no letter is displayed. On the contrary, it's perfectly fine to have an empty string. In fact, an empty string is one of the most common strings in programming.