DEV Community

Cover image for Python Programming for Beginners – Day 9
augustineowino357-design
augustineowino357-design

Posted on

Python Programming for Beginners – Day 9

Tuples, Sets, and Dictionaries in Python

In the previous lesson, we learned about Lists and how they are used to store multiple items in a single variable. Today, we will learn about three important Python data structures:

  • Tuples
  • Sets
  • Dictionaries

These data structures help programmers organize and manage data efficiently in different situations.


1. Tuples in Python

A Tuple is a collection of items stored in a single variable.

Tuples are:

  • Ordered
  • Unchangeable (Immutable)
  • Allow duplicate values

Tuples are created using parentheses "()".

Example

languages = ("Python", "Java", "C++")

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

('Python', 'Java', 'C++')
Enter fullscreen mode Exit fullscreen mode

Accessing Tuple Items

Tuple items are accessed using indexes.

Example

languages = ("Python", "Java", "C++")

print(languages[0])
print(languages[1])
Enter fullscreen mode Exit fullscreen mode

Output

Python
Java
Enter fullscreen mode Exit fullscreen mode

Negative Indexing in Tuples

Example

languages = ("Python", "Java", "C++")

print(languages[-1])
Enter fullscreen mode Exit fullscreen mode

Output

C++
Enter fullscreen mode Exit fullscreen mode

Tuple Length

The "len()" function returns the number of items in a tuple.

Example

numbers = (10, 20, 30)

print(len(numbers))
Enter fullscreen mode Exit fullscreen mode

Output

3
Enter fullscreen mode Exit fullscreen mode

Why Tuples are Important

Tuples are useful when data should not be modified accidentally.

They are commonly used for:

  • Fixed data
  • Coordinates
  • Database records
  • Returning multiple values from functions

2. Sets in Python

A Set is a collection of unique items.

Sets are:

  • Unordered
  • Unchangeable items
  • Do not allow duplicates

Sets are created using curly brackets "{}".

Example

numbers = {1, 2, 3, 4}

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

{1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Duplicate Values in Sets

Sets automatically remove duplicate values.

Example

numbers = {1, 2, 2, 3, 4}

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

{1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Adding Items to a Set

The "add()" method inserts a new item into a set.

Example

numbers = {1, 2, 3}

numbers.add(4)

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

{1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Removing Items from a Set

The "remove()" method removes an item from a set.

Example

numbers = {1, 2, 3, 4}

numbers.remove(2)

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

{1, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Looping Through a Set

Example

languages = {"Python", "Java", "C++"}

for language in languages:
    print(language)
Enter fullscreen mode Exit fullscreen mode

Why Sets are Important

Sets are useful for:

  • Removing duplicates
  • Membership testing
  • Mathematical set operations
  • Data filtering

3. Dictionaries in Python

A Dictionary stores data in key-value pairs.

Dictionaries are:

  • Ordered
  • Changeable
  • Do not allow duplicate keys

Dictionaries are created using curly brackets "{}".

Example

student = {
    "name": "Augustine",
    "age": 21,
    "course": "Python"
}

print(student)
Enter fullscreen mode Exit fullscreen mode

Output

{'name': 'Augustine', 'age': 21, 'course': 'Python'}
Enter fullscreen mode Exit fullscreen mode

Accessing Dictionary Values

Values are accessed using keys.

Example

student = {
    "name": "Augustine",
    "age": 21
}

print(student["name"])
Enter fullscreen mode Exit fullscreen mode

Output

Augustine
Enter fullscreen mode Exit fullscreen mode

Changing Dictionary Values

Example

student = {
    "name": "Augustine",
    "age": 21
}

student["age"] = 22

print(student)
Enter fullscreen mode Exit fullscreen mode

Output

{'name': 'Augustine', 'age': 22}
Enter fullscreen mode Exit fullscreen mode

Adding Items to a Dictionary

Example

student = {
    "name": "Augustine"
}

student["course"] = "Python"

print(student)
Enter fullscreen mode Exit fullscreen mode

Output

{'name': 'Augustine', 'course': 'Python'}


Removing Items from a Dictionary

The "pop()" method removes an item using its key.

Example

student = {
    "name": "Augustine",
    "age": 21,
    "course": "Python"
}

student.pop("age")

print(student)
Enter fullscreen mode Exit fullscreen mode

Output

{'name': 'Augustine', 'course': 'Python'}
Enter fullscreen mode Exit fullscreen mode

Looping Through a Dictionary

Dictionaries can be traversed using loops.

Example

student = {
    "name": "Augustine",
    "age": 21,
    "course": "Python"
}

for key, value in student.items():
    print(key, ":", value)
Enter fullscreen mode Exit fullscreen mode

Output

name : Augustine
age : 21
course : Python
Enter fullscreen mode Exit fullscreen mode

Dictionary Methods

Some commonly used dictionary methods include:

Method| Purpose
"keys()"| Returns all keys
"values()"| Returns all values
"items()"| Returns key-value pairs
"get()"| Returns the value of a key
"pop()"| Removes a specified key

Example

student = {
    "name": "Augustine",
    "age": 21
}

print(student.keys())
print(student.values())
Enter fullscreen mode Exit fullscreen mode

Output

dict_keys(['name', 'age'])
dict_values(['Augustine', 21])
Enter fullscreen mode Exit fullscreen mode

Why Dictionaries are Important

Dictionaries are useful for:

  • Storing related information
  • Fast data retrieval
  • Organizing records
  • Managing structured data

They are widely used in databases, APIs, and real-world applications.


Comparing Tuples, Sets, and Dictionaries

Feature| Tuple| Set| Dictionary
Ordered| Yes| No| Yes
Changeable| No| Yes| Yes
Allows Duplicates| Yes| No| Keys: No
Uses Indexes| Yes| No| No
Stores Key-Value Pairs| No| No| Yes


Conclusion

Tuples, Sets, and Dictionaries are powerful data structures in Python that help programmers organize and manage data efficiently.

  • Use Tuples when data should not change.
  • Use Sets when you need unique values.
  • Use Dictionaries when working with key-value pairs.

Understanding these data structures is essential because they are heavily used in Python applications, web development, data science, automation, and software development.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)