0% found this document useful (0 votes)
12 views15 pages

Python Data Structures Explained

The document provides an overview of four basic data structures in Python: Lists, Tuples, Dictionaries, and Sets. It details their syntax, characteristics, and common operations, including how to create, access, modify, and manipulate these data types. Each section includes examples to illustrate the concepts effectively.

Uploaded by

37 Atif Sawant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Python Data Structures Explained

The document provides an overview of four basic data structures in Python: Lists, Tuples, Dictionaries, and Sets. It details their syntax, characteristics, and common operations, including how to create, access, modify, and manipulate these data types. Each section includes examples to illustrate the concepts effectively.

Uploaded by

37 Atif Sawant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

keyboard_arrow_down Python has baisc 4 ways of storing data:


List
Tuple
sets
Dictionary

keyboard_arrow_down List
Syntax: variable_name = [ ]

#creation
my_list = [25, 4.5, "Python", "java"]

# Print the list


print(my_list)

[25, 4.5, 'Python', 'java']

#own
fruits=["apple" , "stawberry", "bannana", "grapes"]
print(fruits)

['apple', 'stawberry', 'bannana', 'grapes']

List Characteristics:

ordered
Mutable
duplicate value allow
indexing

list=["ïndia", "USA","UK", "Russia"]

print(list)

list=["ïndia", "USA","UK", "Russia"]


print(list)

[Link] 1/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

['ïndia', 'USA', 'UK', 'Russia']

Operations on List:

Accessing elements using [] Brackets.


append
add at specific index
remove
Clear
Sort

#Accessing elements using [] Brackets.


list=["ïndia", "USA","UK", "Russia"]
print(list[1])
print(list[3])

USA
Russia

#Accessing elements using [] Brackets.

# SORT
ages = [19, 26, 29,34,25]
print(ages)
[Link]()
print(ages)

[19, 26, 29, 34, 25]


[19, 25, 26, 29, 34]

#APPEND
ages = [19, 26, 29,34,25]
[Link](1000)
print(ages)

[Link] 2/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

[19, 26, 29, 34, 25, 1000]

#add at specific index


ages = [19, 26, 29,34,25,1000]
[Link](1 ,800)
print(ages)

[19, 800, 26, 29, 34, 25, 1000]

ages = [19,800, 26, 29,34,25,1000]


[Link](1000)
print(ages)

[19, 800, 26, 29, 34, 25]

ages = [19,800, 26, 29,34,25,1000]


[Link]()
print(ages)

[]

keyboard_arrow_down Tuple
Syntax: variable_name = ( )

thistuple = ("apple", 26, "cherry")


print(thistuple)

('apple', 26, 'cherry')

#create own

Tuple Characteristics:

ordered
Immutable
duplicate value allow
indexing

#immutable
my_tuple = (10, 20, 30)
my_tuple[0]=500

[Link] 3/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-8985f5a42fc2> in <cell line: 3>()
1 #immutable
2 my_tuple = (10, 20, 30)
----> 3 my_tuple[0]=500

TypeError: 'tuple' object does not support item assignment

Next steps: Explain error

#duplicatevalue
thistuple = ("apple", 26, "cherry","apple", 26, "cherry")
print(thistuple)

('apple', 26, 'cherry', 'apple', 26, 'cherry')

#indexing
colors = ("red", "green", "blue", "yellow")
print(colors[1])
print(colors[3])

green
yellow

Operations on Tuple:

Accessing elements using [] Brackets.


concatenation
repetation
Membership test
Deletion

[Link] 4/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

Start coding or generate with AI.

#toyBox Game
toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")
print(toybox)

#concatenation

new_toys=("Ironman", "cards")
new_toybox= toybox+new_toys
print(new_toybox)

('Car', 'Lego Set', 'Train', 'Robot', 'Puzzle')


('Car', 'Lego Set', 'Train', 'Robot', 'Puzzle', 'Ironman', 'cards')

#repetation

toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")


print(toybox)

repeat_toybox=toybox*2
print(repeat_toybox)

('Car', 'Lego Set', 'Train', 'Robot', 'Puzzle')


('Car', 'Lego Set', 'Train', 'Robot', 'Puzzle', 'Car', 'Lego Set', 'Train', 'Robot', 'Pu

#Membership

toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")


print("Car" in toybox)

True

[Link] 5/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

#Deletion

toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")


del toybox[0]
print(toybox)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-9e1f6b6d452a> in <cell line: 4>()
2
3 toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")
----> 4 del toybox[0]
5 print(toybox)

TypeError: 'tuple' object doesn't support item deletion

Next steps: Explain error

toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")


del toybox
print(toybox)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-24-d451aa6457cc> in <cell line: 3>()
1 toybox=("Car", "Lego Set", "Train" , "Robot", "Puzzle")
2 del toybox
----> 3 print(toybox)

NameError: name 'toybox' is not defined

Next steps: Explain error

keyboard_arrow_down Dictionary
[Link] 6/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

dict={"FY":"Python", "SY": "C++", "TY":89}


print(dict)

{'FY': 'Python', 'SY': 'C++', 'TY': 89}

[Link] 7/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}

print(country_capitals)

{'Germany': 'Berlin', 'Canada': 'Ottawa', 'England': 'London'}

#YOU CANNOT ACCESS ELEMENTS BY INDEXING BUT CAN ACCESS BY KEYS

country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}

print(country_capitals["Germany"])
print(country_capitals["Canada"])

Berlin
Ottawa

Dictionary Characteristics:

ordered
Mutable
duplicate value NOT allow (Duplicate Keys NOT allowed)
NO Indexing

[Link] 8/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

#mutable
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}

# change the value of "Italy" key to "Rome"


country_capitals["Canada"] = "Toronto"

print(country_capitals)

{'Germany': 'Berlin', 'Canada': 'Toronto', 'England': 'London'}

#duplicate value
dict={"FY":"Python", "SY": "C++", "SY": "C++", "TY":89}
print(dict)

#access element using key

Operations on Dictionary:

Accessing Values.
Adding and Updating Items
Removing Items
Merging Dictionaries

#accessing
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print(my_dict)
print(my_dict["city"])

{'name': 'Alice', 'age': 25, 'city': 'New York'}


New York

#adding and updating


print("Before:")
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print(my_dict)
print("After:")
my_dict["country"] = "USA"
my_dict["age"] = 35
print(my_dict)

Before:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
After:

[Link] 9/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab
{'name': 'Alice', 'age': 35, 'city': 'New York', 'country': 'USA'}

# Remove a specific key

my_dict={'name': 'Alice', 'age': 35, 'city': 'New York', 'country': 'USA'}

del my_dict["city"]
print(my_dict)
# Clear the dictionary
my_dict.clear()
print(my_dict)

{'name': 'Alice', 'age': 35, 'country': 'USA'}


{}

#merging
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}

# Using update()
[Link](dict2)
print(dict1)

{'a': 1, 'b': 3, 'c': 4}

keyboard_arrow_down Sets:
Syntax: variable_name = { }

student_id = {112, 114, 116, 118, 115}


print('Student ID:', student_id)

Student ID: {112, 114, 115, 116, 118}

[Link] 10/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

mixed_set = {'Hello', 101, 30.45, 'Bye'}


print(mixed_set)
vowel_letters = {'a', 'e', 'i', 'o', 'u'}
print('Vowel Letters:', vowel_letters)

{'Hello', 'Bye', 101, 30.45}


Vowel Letters: {'a', 'i', 'u', 'e', 'o'}

Sets Characteristics:

Unordered
Mutable(add/remove)
duplicate value NOT allow
NO Indexing

#duplicate value

my_set = {1, 2, 2, 3, 4, 4, 5}

print(my_set)

{1, 2, 3, 4, 5}

# No indexing
my_set = {1, 2, 3, 4, 5}

print(my_set[0])

Operations on Sets:

Adding Elements
Update Python Set
Removing Elements
Union of Two Sets
Set Intersection
Difference between Two Sets
Set Symmetric Difference
Subset and Superset

[Link] 11/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

#add
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

{1, 2, 3, 4}

# Update Python Set


companies = {'Lacoste', 'Ralph Lauren'}
tech_companies = ['apple', 'google', 'apple'] #try changing to tuple

[Link](tech_companies)

print(companies)

{'Ralph Lauren', 'apple', 'Lacoste', 'google'}

#Removing Elements
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set)
my_set.clear()
print(my_set)

{1, 2, 4}
set()

[Link] 12/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

#UNION
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Union
result = [Link](set2)
# OR
#result = set1 | set2
print(result)

{1, 2, 3, 4, 5}

[Link] 13/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

A = {1, 3, 5}

B = {1, 2, 3}

print('Intersection using &:', A & B)

print('Intersection using intersection():', [Link](B))

Intersection using &: {1, 3}


Intersection using intersection(): {1, 3}

[Link] 14/15
10/01/2025, 02:08 Copy of Welcome to Colaboratory - Colab

A = {2, 3, 5}

B = {1, 2, 6}

print('Difference using &:', A - B)

print('Difference using difference():', [Link](B))

Difference using &: {3, 5}


Difference using difference(): {3, 5}

A = {2, 3, 5}

B {1 2 6}

[Link] 15/15

You might also like