Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Python Data Structures

This document discusses the four main built-in data structures in Python: lists, tuples, sets, and dictionaries. Lists are mutable ordered sequences that can be indexed, sliced and changed. Tuples are immutable like lists but are used to group multiple objects together. Sets are unordered collections where membership is more important than order or frequency. Dictionaries contain key-value pairs where keys must be unique and values can be accessed by key lookup. Examples are provided for common operations on each data structure type.

Uploaded by

Daniel Rizvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Python Data Structures

This document discusses the four main built-in data structures in Python: lists, tuples, sets, and dictionaries. Lists are mutable ordered sequences that can be indexed, sliced and changed. Tuples are immutable like lists but are used to group multiple objects together. Sets are unordered collections where membership is more important than order or frequency. Dictionaries contain key-value pairs where keys must be unique and values can be accessed by key lookup. Examples are provided for common operations on each data structure type.

Uploaded by

Daniel Rizvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PYTHON

DATA
STRUCTURES
SHOOLINI
UNIVERSITY

DANIEL RIZVI
WHAT ARE DATA STRUCTURES?
WHAT ARE DATA STRUCTURES
WHAT ARE DATA STRUCTURES
WHAT ARE DATA STRUCTURES
They are structures which can hold
some data together. In other words,
they are used to store a collection of
related data.
THERE ARE FOUR BUILT-IN DATA

STRUCTURES IN PYTHON

LIST SET TPURPOLMEO 4 DICTIONARY

[] ([]) () {}
IVZIR
LEINAD
LIST
A LIST IS A DATA STRUCTURE IN PYTHON THAT
IS A MUTABLE, OR CHANGEABLE, ORDERED
SEQUENCE OF ELEMENTS

PYTHON
LOOKING AT THE EXAMPLES

IVZIR
LEINAD
ADDING TO THE LIST

shoplist = ['soap','cake','shampoo','pen']
shoplist.append('glasses')
print(shoplist)
D

OUTPUT -
['soap', 'cake', 'shampoo', 'pen', 'glasses']

PYTHON
*append - is the keyword used to add item in the list
LOOKING AT THE EXAMPLES

IVZIR
LEINAD
TO REMOVE ITEM FROM THE LIST

shoplist = ['soap','cake','shampoo','pen','glasses']
print(shoplist.remove('cake'))
print(shoplist)
D

OUTPUT -
['soap', 'shampoo', 'pen', 'glasses']

PYTHON
*remove - is the keyword used to remove item in the list
LOOKING AT THE EXAMPLES sham
soap cake pen

IVZIR
poo

LEINAD
TO PRINT THE INDEX
0 1 2 3
shoplist = ['soap','cake','shampoo','pen','glasses']
shoplist.index('cake')
print(shoplist)

OUTPUT - D

*index - which searches for given element from start of the list

PYTHON
and returns the lowest index where the element appears
LOOKING AT THE EXAMPLES sham
soap cake pen

IVZIR
poo

LEINAD
IN A LIST, THE 2NDAND 3RD ITEMS
CAN BE ACCESSED WITH: 0 1 2 3
shoplist = ['soap','cake','shampoo','pen','glasses']
print(shoplist[1:3]))

OUTPUT - D

['cake', 'shampoo']

*index - which searches for given element from start of the list

PYTHON
and returns the lowest index where the element appears
tuple( )
A Tuple is a collection of Python objects separated
by commas. In someways a tuple is similar to a list
in terms of indexing, nested objects and repetition
but a tuple is immutable unlike lists which are
mutable.
tuple is immutable
we can't make changes
tuple example
mytuple = (21,44,11)
print(mylist)

output-

(21, 44, 11)


DICTIONARY
DICTIONARY {}
A DICTIONARY IS A COLLECTION WHICH IS UNORDERED, CHANGEABLE AND
INDEXED. IN PYTHON DICTIONARIES ARE WRITTEN WITH CURLY BRACKETS,
AND THEY HAVE KEYS AND VALUES.
A GIVEN KEY CAN APPEAR IN A DICTIONARY ONLY ONCE.
DUPLICATE KEYS ARE NOT ALLOWED.

COMPONENTS OF DICTIONARY
GRADE = {"RAMESH":89}

key value
EXAMPLE OF DICTIONARY
GRADE = {"SURAJ":90,"SURYA":91,"ANMOL":92,"ANIL":77}
PRINT(GRADE)

OUTPUT-
{'SURAJ': 90, 'SURYA': 91, 'ANMOL': 92, 'ANIL': 77}

*dictionary is mutable
EXAMPLE OF DICTIONARY
CALLING BY KEY

GRADE = {"SURAJ":90,"SURYA":91,"ANMOL":92,"ANIL":77}
PRINT(GRADE["SURAJ"])

OUTPUT-
90

The values of a dictionary can be accessed using keys but the


keys of a dictionary can't be accessed using values.
sets
Sets are unordered collections of simple objects.
These are used when the existence of an object in
a collection is more important than the order or
how many times it occurs.
components of set
myset = set(['Mithun',22119921,33.23,'Ram'])

set keyword compulsory to use

OUTPUT FORMAT-

{22119921, 'Mithun', 'Ram', 33.23}

output comes with curly braces


some operations in sets
to check item is present in set
myset = (['Anshul',19,'Sharma'])
print('Sharma' in myset)
OUTPUT -

True

making a new set by usage of another


myset = (['Anshul',19,'Sharma']) OUTPUT -

yourset = myset.copy() ['Anshul', 19, 'Sharma']


print(yourset)
OVERVIEW
[] ()
list tuples
A list is a mutable, ordered sequence
Tuples are used to hold together
of items. As such, it can be indexed,
multiple objects. are immutable
sliced, and changed.

set([]) {}
sets dictionary
A set is an unordered collection of A dictionary is
items. a set itself is mutable. a key:value pair, Dictionaries themselv
es are mutable
thank you
DANIEL RIZVI

You might also like