Opens in a new tab
Lesson 6: Basic Data Structures in Python

Basic Data Structures in Python

Basic Data Structures, Python Mastery Program, Stage 1 Python

Written by

Muhammad Atif

Last Updated

Introduction to Data Structures

Data structures are used to store and organize data in a program. Python has built-in data structures, such as lists, tuples, sets, and dictionaries.

Lists

A list is an ordered, mutable collection of elements. You can create a list using square brackets '[]' and access elements using indices.

Example:

Python
my_list = [1, 2, 3, 4, 5]
print(my_list[0])  # Output: 1

Tuples

A tuple is an ordered, immutable collection of elements. You can create a tuple using parentheses '()' and access elements using indices.

Example:

Python
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[0])  # Output: 1

Sets

A set is an unordered, mutable collection of unique elements. You can create a set using the 'set()' function or curly braces '{}'.

Example:

Python
my_set = {1, 2, 3, 4, 5}
print(len(my_set))  # Output: 5

Dictionaries

A dictionary is an unordered, mutable collection of key-value pairs. You can create a dictionary using curly braces '{}' and access elements using keys.

Example:

Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['a'])  # Output: 1

Hands-on Exercises

  1. Write a Python script that creates a list of numbers and finds the sum of all the even numbers in the list.
  2. Create a Python script that uses a dictionary to store and print student names and their corresponding grades.
  3. Write a Python script that takes a list of words and returns a dictionary with the words as keys and their frequencies as values.

Real-World Example

Suppose you are building an inventory management system for a store. You can use Python’s data structures, such as dictionaries and lists, to store product information (name, price, quantity) and manage the inventory efficiently.

Additional Resources

  1. Python documentation: https://docs.python.org/3/tutorial/datastructures.html
  2. Real Python: https://realpython.com/python-data-structures/
  3. W3Schools Python tutorial: https://www.w3schools.com/python/python_lists.asp

Summary and Next Steps

In this lesson, you have learned about:

  1. Basic data structures in Python
  2. Lists and their properties
  3. Tuples and their properties
  4. Sets and their properties
  5. Dictionaries and their properties

In the next lesson, you will learn about object-oriented programming in Python, including classes, objects, inheritance, and polymorphism. This knowledge will help you create more structured and modular programs.

Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!

Review Questions

  1. What are the differences between lists and tuples in Python?
  2. Explain how sets ensure that all elements are unique.
  3. Describe the key-value structure of dictionaries in Python.
  4. Write a Python script that removes duplicates from a list by converting it to a set and then back to a list.
  5. Create a Python script that uses a dictionary to store and manipulate a phonebook (add, delete, and search for contacts).

Muhammad Atif

Data Analyst & Web Development Professional | Focused on Machine Learning, SQL and Python

Transitioned from a background in finance to IT, I now focus on data science and web development, with specialties in Machine Learning, SQL, and Python. My articles aim to clarify complex IT concepts, informed by a rich history in analytical thinking and strategy.

You Might Also Like

Browse more topics and insights handpicked just for you. Stay updated and informed with our latest articles and news.