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.
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:
Python1# 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'
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:
Python1# 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
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.
Python1# 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.
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 find occurrence of a particular element!
Python1# 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# Finding the first occurrence of an element in a list or a string 18# Note that if the element is not found, `index` throws an exception 19# So we should initially check the existence by the operator `in`, 20# Then use the `index` method if it exists using the `if-else` construction. 21# If the element is not found, the indices are assigned to `-1` 22found_1_in_list = 1 in my_list # Returns: True 23found_8_in_list = 8 in my_list # Returns: False 24found_in_string = 'l' in my_string.lower() # Returns True 25index_1_in_list = my_list.index(1) if found_1_in_list else -1 # Returns: 0 26index_8_in_list = my_list.index(8) if found_8_in_list else -1 # Returns: -1 27index_in_string = my_string.lower().index('l') if found_in_string else -1 # Returns: 2 28 29# Sorting items in a list 30sorted_list = sorted(my_list, reverse=True) # [5, 4, 3, 2, 1]
And there we have it, a Python toolkit of lists and strings!
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!