0% found this document useful (0 votes)
2 views14 pages

Chapter 2 Data Structures in Python

Uploaded by

shamlilasak
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)
2 views14 pages

Chapter 2 Data Structures in Python

Uploaded by

shamlilasak
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

CHAPTER 2: DATA STRUCTURES IN PYTHON

Introduc on

Data Structures are used to store and organize data efficiently.

Python provides several built-in data structures. The most commonly used are:

1. List

2. Tuple

3. Dic onary

Each data structure has different features and is used for different purposes.

1. LIST

What is a List?

A List is a collec on of mul ple items stored in a single variable.

Lists are:

 Ordered

 Mutable (can be modified)

 Allow duplicate values

 Can store different data types

Example

students = ["Arun", "Priya", "Kavin"]

Here:

 students → list name

 Arun, Priya, Kavin → list elements

Crea ng a List

Empty List

my_list = []

List with Values

numbers = [10, 20, 30, 40]

Mixed Data Types

data = ["Arun", 21, 85.5, True]


Accessing Elements

Lists use indexing.

Posi ve Indexing

students = ["Arun", "Priya", "Kavin"]

print(students[0])

Output:

Arun

Nega ve Indexing

print(students[-1])

Output:

Kavin

List Slicing

Syntax:

list[start:end]

Example:

numbers = [10,20,30,40,50]

print(numbers[1:4])

Output:

[20, 30, 40]

Upda ng List Elements

students = ["Arun", "Priya", "Kavin"]

students[1] = "Divya"
print(students)

Output:

['Arun', 'Divya', 'Kavin']

Adding Elements

append()

Adds an item at the end.

[Link]("Rahul")

Output:

['Arun', 'Priya', 'Kavin', 'Rahul']

insert()

Adds an item at a specific posi on.

[Link](1, "Divya")

Output:

['Arun', 'Divya', 'Priya', 'Kavin']

extend()

Adds mul ple elements.

[Link](["Rahul", "Meena"])

Removing Elements

remove()

Removes a specific value.

[Link]("Priya")
pop()

Removes element using index.

[Link](1)

pop() without index

Removes last element.

[Link]()

clear()

Removes all elements.

[Link]()

Output:

[]

List Func ons

len()

Returns total number of elements.

len(students)

max()

Returns largest value.

numbers = [10,20,30]

print(max(numbers))

Output:

30

min()

Returns smallest value.


print(min(numbers))

Output:

10

sum()

Returns total.

print(sum(numbers))

Output:

60

Sor ng Lists

sort()

Ascending order.

[Link]()

sort(reverse=True)

Descending order.

[Link](reverse=True)

sorted()

Creates a new sorted list.

sorted(numbers)

Reversing a List

[Link]()
Checking Membership

"Arun" in students

Output:

True

Nested Lists

A list inside another list.

data = [

["Arun", 85],

["Priya", 92]

Access:

print(data[0][1])

Output:

85

LIST COMPREHENSION

A short way to create lists.

numbers = [x for x in range(5)]

Output:

[0,1,2,3,4]

2. TUPLE

What is a Tuple?

A Tuple is a collec on of items.

Tuples are:

 Ordered

 Immutable (cannot be modified)


 Allow duplicates

Example

subjects = ("Python", "SQL", "Sta s cs")

Crea ng Tuples

Normal Tuple

t = (10,20,30)

Single Element Tuple

t = (10,)

Comma is mandatory.

Accessing Elements

subjects[0]

Output:

Python

Nega ve Indexing

subjects[-1]

Output:

Sta s cs

Tuple Slicing

subjects[0:2]

Output:

('Python', 'SQL')
Tuple Opera ons

Concatena on

t1 = (1,2)

t2 = (3,4)

print(t1+t2)

Output:

(1,2,3,4)

Repe on

t = (1,2)

print(t*3)

Output:

(1,2,1,2,1,2)

Tuple Func ons

len()

len(t)

max()

max(t)

min()

min(t)

sum()

sum(t)
Tuple Methods

count()

Counts occurrences.

t = (1,2,2,2,3)

[Link](2)

Output:

index()

Returns posi on.

[Link](3)

Output:

Tuple Packing

student = ("Arun", 21, "Chennai")

Tuple Unpacking

name, age, city = student

List vs Tuple

List Tuple

Mutable Immutable

[] ()

More memory Less memory

Slower Faster
3. DICTIONARY

What is a Dic onary?

Dic onary stores data in Key-Value pairs.

Example:

student = {

"name":"Arun",

"age":21

Characteris cs

 Key-Value Structure

 Mutable

 Keys must be unique

 Values can be duplicated

Crea ng Dic onaries

student = {

"name":"Arun",

"age":21,

"city":"Chennai"

Accessing Values

Using Key

student["name"]

Output:

Arun
Using get()

[Link]("age")

Output:

21

Adding New Items

student["college"] = "ABC College"

Upda ng Values

student["age"] = 22

Removing Items

pop()

[Link]("age")

del

del student["city"]

clear()

[Link]()

Dic onary Methods

keys()

Returns all keys.

[Link]()
values()

Returns all values.

[Link]()

items()

Returns key-value pairs.

[Link]()

update()

Updates dic onary.

[Link]({"city":"Chennai"})

Looping Through Dic onary

Keys

for key in student:

print(key)

Values

for value in [Link]():

print(value)

Key and Value

for key,value in [Link]():

print(key,value)

Nested Dic onary

students = {

"student1":{
"name":"Arun",

"age":21

},

"student2":{

"name":"Priya",

"age":22

Access:

students["student1"]["name"]

Output:

Arun

Dic onary Comprehension

squares = {x:x*x for x in range(5)}

Output:

0:0,

1:1,

2:4,

3:9,

4:16

}
Summary

List

 Ordered

 Mutable

 Allows duplicates

 Uses []

Tuple

 Ordered

 Immutable

 Allows duplicates

 Uses ()

Dic onary

 Key-Value pairs

 Mutable

 Keys must be unique

 Uses {}

These three data structures are the founda on for working with data in Python and are
widely used in Data Analy cs, Data Science, and Machine Learning projects.

You might also like