Python provides several built-in data types that allow developers to store and manipulate data efficiently. In this lesson, you will explore lists, tuples, ranges, dictionaries, and sets, their properties, and their applications.
Lists in Python are like dynamic arrays in JavaScript. They can store a sequence of items and are mutable, meaning they can be modified after creation. You can add, remove, and sort elements in a list.
# Creating an empty list
empty_list = []
# Creating a list with initial values
departments = ['HR', 'Development', 'Sales', 'Finance', 'IT', 'Customer Support']
# Another way to create a list using the list() function
specials = list()
# Checking membership using the 'in' operator
print(1 in [1, 2, 3]) # Output: True
print(4 in [1, 2, 3]) # Output: False
Real-world example: A shopping cart in an e-commerce application is best represented as a list. Items can be added, removed, or reordered easily.
Tuples are similar to lists but immutable. Once created, they cannot be modified, making them ideal for data that should remain constant.
# Creating a tuple
time_blocks = ('AM', 'PM')
# Tuples can also be declared without parentheses
colors = 'red', 'blue', 'green'
numbers = 1, 2, 3
# Converting a list to a tuple
tuple_example = tuple([1, 2, 3])
# Checking membership
print(1 in (1, 2, 3)) # Output: True
Real-world example: Tuples are commonly used for storing fixed configurations, such as days of the week or country codes.
A range represents a sequence of numbers and is often used in loops. Ranges are immutable, meaning they cannot be changed after creation.
# Creating ranges
range1 = range(5) # [0, 1, 2, 3, 4]
range2 = range(1, 5) # [1, 2, 3, 4]
range3 = range(0, 25, 5) # [0, 5, 10, 15, 20]
# Ranges with negative numbers
range4 = range(5, 0, -1) # [5, 4, 3, 2, 1]
Real-world example: Generating pagination numbers on a website is an excellent use of ranges.
Dictionaries are key-value pairs, making them ideal for storing related data. Unlike lists and tuples, dictionaries are unordered but allow quick lookups.
# Creating dictionaries
a = {'one': 1, 'two': 2, 'three': 3}
b = dict(one=1, two=2, three=3)
# Checking key existence
print('one' in a) # Output: True
print(4 in a) # Output: False
Real-world example: Storing user profile information, such as name, email, and age, is best handled using dictionaries.
Sets are unordered collections of unique elements. They are particularly useful for removing duplicates and performing mathematical set operations.
# Creating sets
school_bag = {'book', 'paper', 'pencil', 'pencil', 'book'}
print(school_bag) # Output: {'book', 'pencil', 'paper'}
# Using the set() constructor
letters = set('abracadabra')
print(letters) # {'a', 'r', 'b', 'c', 'd'}
# Checking membership
print(1 in {1, 2, 3}) # Output: True
Real-world example: Removing duplicate items from a list of email addresses in a marketing campaign.
in operator for membership testing.By understanding these built-in data types, you will be able to store and manipulate data effectively in Python.