Introduction

Welcome to the next unit of 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:

# Defining a list and a string
my_list = [1, 2, 3, 4]
my_string = 'hello'

# Now let's try to change the first element of both these collections
my_list[0] = 100
# Uncommenting the below line will throw an error
# 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:

# Creating a list
fruits = ['apple', 'banana', 'cherry']

# Add a new element at the end
fruits.append('date') # ['apple', 'banana', 'cherry', 'date']

# Inserting an element at a specific position
fruits.insert(1, 'bilberry') # ['apple', 'bilberry', 'banana', 'cherry', 'date']

# Removing a particular element
fruits.remove('banana') # ['apple', 'bilberry', 'cherry', 'date']

# Accessing elements using indexing
first_fruit = fruits[0] # apple
last_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.

# Creating a string
greeting_string = "Hello, world!"

# Accessing characters using indexing
first_char = greeting_string[0]  # 'H'
last_char = greeting_string[-1]  # '!'

# Making the entire string lowercase
lowercase_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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal