Lesson 1

Exploring Python Collections: Lists and Strings Essential Guide

Introduction

Welcome to this course!

Before we get deeper into Python essentials for interview prep, we'll remind ourselves about some Python features - specifically, Python collections — Lists, and Strings. These collections allow Python to group multiple elements, such as numbers or characters, under a single entity.

Some of these concepts might already be familiar to you, so you can breeze through the beginning until we get to the meat of the course and the path.

Understanding Python's Collections

As our starting point, it's crucial to understand what Python collections are. They help us manage multiple values efficiently and are categorized into Lists, Tuples, Sets, and Dictionaries.

We will focus mainly on Lists and Strings. A fun fact here is that Lists are mutable (we can change them after creation), while strings are immutable (unalterable post-creation). Let's see examples:

Python
1# Defining a list and a string 2my_list = [1, 2, 3, 4] 3my_string = 'hello' 4 5# Now let's try to change the first element of both these collections 6my_list[0] = 100 7# Uncommenting the below line will throw an error 8# my_string[0] = 'H'
Diving Into Lists

Imagine having to take an inventory of all flora in a forest without a list at your disposal — seems near impossible, right? That's precisely the purpose Lists serve in Python. They let us organize data so that each item holds a definite position or an index. The index allows us to access or modify each item individually.

Working with Lists is as simple as this:

Python
1# Creating a list 2fruits = ['apple', 'banana', 'cherry'] 3 4# Add a new element at the end 5fruits.append('date') # ['apple', 'banana', 'cherry', 'date'] 6 7# Inserting an element at a specific position 8fruits.insert(1, 'bilberry') # ['apple', 'bilberry', 'banana', 'cherry', 'date'] 9 10# Removing a particular element 11fruits.remove('banana') # ['apple', 'bilberry', 'cherry', 'date'] 12 13# Accessing elements using indexing 14first_fruit = fruits[0] # apple 15last_fruit = fruits[-1] # date
Understanding Strings

Think of Strings as a sequence of letters or characters. So, whether you're writing down a message or noting a paragraph, it all boils down to a string in Python. Strings are enclosed by either single, double, or triple quotes.

Python
1# Creating a string 2greeting_string = "Hello, world!" 3 4# Accessing characters using indexing 5first_char = greeting_string[0] # 'H' 6last_char = greeting_string[-1] # '!' 7 8# Making the entire string lowercase 9lowercase_greeting = greeting_string.lower() # 'hello, world!'

Though strings are immutable, we can use string methods such as .lower(), .upper(), .strip(), etc., to effectively work with them. These methods essentially create a new string for us.

Indexing and Common Operations

Both lists and strings allow us to access individual elements through indexing. In Python, we start counting from 0, implying the first element is at index 0, the second at index 1, and so on. Negative indexing begins from the end, with -1 denoting the last element.

We have many operations we can perform on our lists and strings. We can slice them, concatenate them, repeat them, and even count occurrences of a particular element!

Python
1# Define a list and a string 2my_list = [1, 2, 3, 4, 5] 3my_string = "Hello" 4 5# Slicing: my_list[start:end], `start` inclusive, `end` exclusive 6slice_list = my_list[2:4] # [3, 4] 7slice_string = my_string[1:3] # "el" 8 9# Concatenation: my_list + another_list 10concatenate_list = my_list + [6, 7, 8] # [1, 2, 3, 4, 5, 6, 7, 8] 11concatenate_string = my_string + ", world!" # "Hello, world!" 12 13# Repetition: my_list * n # 14repeat_list = my_list * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] 15repeat_string = my_string * 2 # "HelloHello" 16 17# Counting occurrences of an element in a list or a string 18count_list = my_list.count(1) # Returns: 1 19count_string = my_string.lower().count('l') # Returns: 2 20 21# Sorting items in a list 22sorted_list = sorted(my_list, reverse=True) # [5, 4, 3, 2, 1]

And there we have it, a Python toolkit of lists and strings!

Lesson Summary and Practice

Give yourself a pat on the back! You've navigated through Python collections, primarily focusing on Lists and Strings, learning how to create, access, and manipulate them via various operations.

Up next, reinforce your understanding with plenty of hands-on practice. The comprehension of these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.