SlideShare a Scribd company logo
Data Structures in Python
DATA STRUCTURES IN PYTHON
Data Structures in Python :
Lists
Dictionary
Tuples
Sets
LISTS
 List is most versatile datatype available in Python, written as a list of comma-
separated values (items) between square brackets.
 Items in a list can be Heterogenous types(need not be of the same type).
 Lists are mutable
 Concatenation produces a new lists.
 Append function extends a list with a new value without changing it.
LISTS ARE MUTABLE
 A mutable object can be changed after it's created, and an immutable object
cannot be changed after its created.
 Examples of mutable objects are dictionary,lists etc
 Example of lists in mutable form is:
LIST FUNCTIONS
 list. append ( x) :Add an item to the end of the list.
 list. extend ( L): Extend the list by appending all the items in the given list.
 list. insert ( i, x): Insert an item at a given position.
 list. remove ( x) : Remove the first item
from the list whose value is x.
It gives an error if there is no such item.
LIST FUNCTIONS
 list. pop ( [i]): Remove the item at the given position in the list, and return it.
 list. clear ( ): Remove all items from the list. Equivalent to del a[:] .
Not supported in python 2.X.
 list.reverse(): Reverse a given list.
 list. index ( x): Return the index in the list of the first item whose value is x. It is an
error if there is no such item.
 list. count ( x): Return the number of times x appears in the list.
 list. sort ( ): Sort the items of the list in place.
USING LISTS AS STACKS
 We can also use lists as a stack in Python.
 Stack follows a property of last in first out(LIFO Rule).
 To add an item to the top of the stack, use append() .
 To retrieve an item from the top of the stack, use pop() without an explicit index.
 Example:
USING LISTS AS QUEUES
 Python also uses a list as a queue where the first element added is the first element
retrieved.
 Lists are not efficient when used as a queue.
 While appends and pops from the end of list are fast, doing inserts or pops from the
beginning of a list is slow.
 To implement a queue, use collections.deque which was designed to have fast
appends and pops from both ends.
 Example:
List Comprehensions
 List comprehensions is very similar to set theory or set builder form .
 List comprehensions provide a concise way to create a lists.
 Allows to build a new set from existing sets.
 Is an extension to the lists.
 Examples:
 Create a list of two tuples using list comprehension
 Flattens a list using a list comprehension using “for”.
VARIABLES
DICTIONARY
 Dictionary is defined as an unordered set of key: value pairs, with the
requirement that the keys are unique . 
 Dictionary is a Mutable datatype.
 Dictionaries are sometimes found in other languages as “associative memories”
or “associative arrays”.
 Dictionaries are indexed by keys, which can be any immutable type or could be
a string.
 A pair of braces creates an empty dictionary: {} , not [].
 INITIALISATION: example be
 Keys could be a string in dictionary. Example
DICTIONARY
 We can nest dictionaries. Example:
 Directly assign values to a dictionary.
 Dictionary comprehensions can be used to create dictionaries from arbitrary key
and value expressions:
 The dict() constructor builds dictionaries directly from sequences of key-value
pairs:
TUPLES
 Tuple is a sequence data type.
 Tuples are immutable, and usually contain a heterogeneous sequence of
elements.
 Simultaneous assignment is possible in tuples.
 In tuple, we can assign a tuple of value to a name.
TUPLES
 Tuples may be nested.
 A tuple consists of a number of values separated by commas.
 Extract positions in tuples using slices.
VARIABLES
SETS
 A set is an unordered collection with no duplicate elements.
 Set objects also support mathematical operations like union, intersection,
difference, and symmetric difference.
 Curly braces or the set() function can be used to create sets.
 Example:
 Creates an empty set using
 Set membership in sets
 We can convert a list into sets.
SETS OPERATIONS
 Union : union of two sets is done using “set1|set2”
 Intersection : intersection of two sets can be done using “set1&set2”
 Set difference: In this, elements present in set2 is not included in the resultant set.
It can be don using “set1-set2
 Exclusive or: The syntax for exclusive or is “set1^set2”
STRINGS
 String is defined as a sequence or list of characters.
 String is immutable in nature means once defined , they cannot be changed.
 str is the type for strings in python.
 Define strings using quotes (“ or ‘ or “““)
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “““This is a multi-line
string that uses triple quotes.”””
‘ ‘ can be used to escape quotes:
STRINGS
 The string is enclosed in double quotes, if the string contains a single quote
and no double quotes, otherwise it is enclosed in single quotes.
 The print() function produces a more readable output, by omitting the enclosing
quotes and by printing escaped and special characters:
 ‘n’ is used to place a string in newline.
STRINGS
 If you don’t want characters prefaced by  to be interpreted as special characters,
you can use raw strings by adding an r before the first quote:
 Two or more string literals (i.e. the ones enclosed between quotes) next to each
other are automatically concatenated.
 Attempting to use an index that is too large
result in an error.
ACCESSING A STRINGS
 To access substrings, use the square brackets for slicing along with the index or
indices to obtain your substring.
 Example −
POSITIVE AND NEGATIVE INDICES
OPERATIONS ON STRINGS
 CONCATENATION (+) :
 adds value on the either sde of operator.
 REPETITION (*) :
 creates new strings,concatenating multiple copies of the same strings.
 SLICE ([]) :
 gives the character from the given index
 RANGE SLICE ([ :]) :
 gives the character from the given range.
OPERATIONS ON STRINGS
 MEMBERSHIP (in) or (not in) : membership returns true or false if the character
exist in the given string.
 length (len): It is used to find the length of the string.
 upper() : upper() is used to uppercase the string .
 lower() : lower() operation is used to lowercase the string.
 THERE ARE MANY MORE OPERATIONS ON STRINGS
MODIFYING STRINGS
 Cannot update a string “in place” as strings are immutable in nature.
 Example:
 Instead ,use slices and concatenation for modification of strings:-
 Example :
SLICING IN STRINGS
 A slice is a segment of string.
 Slicing allows you to obtain substring
 Syntax: Str[start : end]
 Start: substring start from this element
 End: end of substring excluding the element at this index
 S[i:j] starts at s[i] and ends at s[j-1]
 S[:j] means start at s[0], so s[0:j]
 S[i:] means ends at s[len(s)-1]
STRING FORMATTING
 str.format () is used to format the string.
 Examples: Replace argument by position in message string.
 Replace argument by names in message string.
VARIABLES
REVERT BACK AT devashish19gupta@gmail.com
CONTRIBUTERS
• EXAMPLE 1:
• ‘3d’ describes how to display the value 6.
• ‘d ’ is a code that specifies that the value should be treated as an integer value
• ‘3’is the width of the area to show 6.
• EXAMPLE 2:
• ‘6.2f’ describes how to display the value 46.89
• ‘f’ is a code specifies that 46.829 should be treated as a floating point value
• ‘6’ shows the width of the area to show 46.89
• ‘.2’ shows number of digits to show after decimal point.

More Related Content

PPTX
Python Data Structures and Algorithms.pptx
PPTX
Datastructures in python
PPTX
Functions in python slide share
PPTX
Progressive web app
PPTX
Basics of Object Oriented Programming in Python
PPTX
Python Functions
PPT
Intrusion detection system ppt
PDF
Python final ppt
Python Data Structures and Algorithms.pptx
Datastructures in python
Functions in python slide share
Progressive web app
Basics of Object Oriented Programming in Python
Python Functions
Intrusion detection system ppt
Python final ppt

What's hot (20)

PDF
Datatypes in python
PPTX
Regular expressions in Python
PPTX
Data Structures (CS8391)
PPTX
Chapter 03 python libraries
PPTX
Functions in python
PPTX
Python-Classes.pptx
PDF
Python set
PPTX
Templates in C++
PPT
Abstract data types
PPTX
Data types in c++
PPTX
Data structures and algorithms
PPTX
python conditional statement.pptx
PPT
PDF
Python programming : Strings
PPTX
Chapter 05 classes and objects
PPT
Data Structures- Part5 recursion
PDF
Python list
PDF
Data visualization in Python
Datatypes in python
Regular expressions in Python
Data Structures (CS8391)
Chapter 03 python libraries
Functions in python
Python-Classes.pptx
Python set
Templates in C++
Abstract data types
Data types in c++
Data structures and algorithms
python conditional statement.pptx
Python programming : Strings
Chapter 05 classes and objects
Data Structures- Part5 recursion
Python list
Data visualization in Python
Ad

Similar to Data Structures in Python (20)

PPTX
Data structures in Python
PPTX
dataStructuresInPython.pptx
PPTX
Chapter - 2.pptx
PPTX
11 Introduction to lists.pptx
PDF
Anton Kasyanov, Introduction to Python, Lecture4
PDF
ppt notes python language operators and data
PDF
13- Data and Its Types presentation kafss
PPTX
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PDF
DATA TYPES IN PYTHON.pdf
PDF
PPTX
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PPTX
PDF
Data structures in python
PPTX
The Datatypes Concept in Core Python.pptx
PDF
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
PPTX
Modulebajajajjajaaja shejjsjs sisiisi 4.pptx
PDF
advanced python for those who have beginner level experience with python
PDF
Advanced Python after beginner python for intermediate learners
PPTX
ADST university of Sussex foundation class
PPTX
Introduction To Programming with Python-3
Data structures in Python
dataStructuresInPython.pptx
Chapter - 2.pptx
11 Introduction to lists.pptx
Anton Kasyanov, Introduction to Python, Lecture4
ppt notes python language operators and data
13- Data and Its Types presentation kafss
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
DATA TYPES IN PYTHON.pdf
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
Data structures in python
The Datatypes Concept in Core Python.pptx
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
Modulebajajajjajaaja shejjsjs sisiisi 4.pptx
advanced python for those who have beginner level experience with python
Advanced Python after beginner python for intermediate learners
ADST university of Sussex foundation class
Introduction To Programming with Python-3
Ad

More from Devashish Kumar (6)

PPTX
Python: Data Visualisation
PPTX
Pandas csv
PPTX
Data Analysis packages
PPTX
Data Analysis in Python-NumPy
PPTX
Introduction to Python Part-1
PPTX
Cloud Computing Introductory-1
Python: Data Visualisation
Pandas csv
Data Analysis packages
Data Analysis in Python-NumPy
Introduction to Python Part-1
Cloud Computing Introductory-1

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Current and future trends in Computer Vision.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
web development for engineering and engineering
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
Construction Project Organization Group 2.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Current and future trends in Computer Vision.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Automation-in-Manufacturing-Chapter-Introduction.pdf
573137875-Attendance-Management-System-original
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
Sustainable Sites - Green Building Construction
Embodied AI: Ushering in the Next Era of Intelligent Systems
bas. eng. economics group 4 presentation 1.pptx
Internet of Things (IOT) - A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
web development for engineering and engineering
Model Code of Practice - Construction Work - 21102022 .pdf
Fundamentals of safety and accident prevention -final (1).pptx

Data Structures in Python

  • 2. DATA STRUCTURES IN PYTHON Data Structures in Python : Lists Dictionary Tuples Sets
  • 3. LISTS  List is most versatile datatype available in Python, written as a list of comma- separated values (items) between square brackets.  Items in a list can be Heterogenous types(need not be of the same type).  Lists are mutable  Concatenation produces a new lists.  Append function extends a list with a new value without changing it.
  • 4. LISTS ARE MUTABLE  A mutable object can be changed after it's created, and an immutable object cannot be changed after its created.  Examples of mutable objects are dictionary,lists etc  Example of lists in mutable form is:
  • 5. LIST FUNCTIONS  list. append ( x) :Add an item to the end of the list.  list. extend ( L): Extend the list by appending all the items in the given list.  list. insert ( i, x): Insert an item at a given position.  list. remove ( x) : Remove the first item from the list whose value is x. It gives an error if there is no such item.
  • 6. LIST FUNCTIONS  list. pop ( [i]): Remove the item at the given position in the list, and return it.  list. clear ( ): Remove all items from the list. Equivalent to del a[:] . Not supported in python 2.X.  list.reverse(): Reverse a given list.  list. index ( x): Return the index in the list of the first item whose value is x. It is an error if there is no such item.  list. count ( x): Return the number of times x appears in the list.  list. sort ( ): Sort the items of the list in place.
  • 7. USING LISTS AS STACKS  We can also use lists as a stack in Python.  Stack follows a property of last in first out(LIFO Rule).  To add an item to the top of the stack, use append() .  To retrieve an item from the top of the stack, use pop() without an explicit index.  Example:
  • 8. USING LISTS AS QUEUES  Python also uses a list as a queue where the first element added is the first element retrieved.  Lists are not efficient when used as a queue.  While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow.  To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.  Example:
  • 9. List Comprehensions  List comprehensions is very similar to set theory or set builder form .  List comprehensions provide a concise way to create a lists.  Allows to build a new set from existing sets.  Is an extension to the lists.  Examples:  Create a list of two tuples using list comprehension  Flattens a list using a list comprehension using “for”.
  • 11. DICTIONARY  Dictionary is defined as an unordered set of key: value pairs, with the requirement that the keys are unique .  Dictionary is a Mutable datatype.  Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”.  Dictionaries are indexed by keys, which can be any immutable type or could be a string.  A pair of braces creates an empty dictionary: {} , not [].  INITIALISATION: example be  Keys could be a string in dictionary. Example
  • 12. DICTIONARY  We can nest dictionaries. Example:  Directly assign values to a dictionary.  Dictionary comprehensions can be used to create dictionaries from arbitrary key and value expressions:  The dict() constructor builds dictionaries directly from sequences of key-value pairs:
  • 13. TUPLES  Tuple is a sequence data type.  Tuples are immutable, and usually contain a heterogeneous sequence of elements.  Simultaneous assignment is possible in tuples.  In tuple, we can assign a tuple of value to a name.
  • 14. TUPLES  Tuples may be nested.  A tuple consists of a number of values separated by commas.  Extract positions in tuples using slices.
  • 16. SETS  A set is an unordered collection with no duplicate elements.  Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.  Curly braces or the set() function can be used to create sets.  Example:  Creates an empty set using  Set membership in sets  We can convert a list into sets.
  • 17. SETS OPERATIONS  Union : union of two sets is done using “set1|set2”  Intersection : intersection of two sets can be done using “set1&set2”  Set difference: In this, elements present in set2 is not included in the resultant set. It can be don using “set1-set2  Exclusive or: The syntax for exclusive or is “set1^set2”
  • 18. STRINGS  String is defined as a sequence or list of characters.  String is immutable in nature means once defined , they cannot be changed.  str is the type for strings in python.  Define strings using quotes (“ or ‘ or “““) >>> st = “Hello World” >>> st = ‘Hello World’ >>> st = “““This is a multi-line string that uses triple quotes.””” ‘ ‘ can be used to escape quotes:
  • 19. STRINGS  The string is enclosed in double quotes, if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes.  The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:  ‘n’ is used to place a string in newline.
  • 20. STRINGS  If you don’t want characters prefaced by to be interpreted as special characters, you can use raw strings by adding an r before the first quote:  Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.  Attempting to use an index that is too large result in an error.
  • 21. ACCESSING A STRINGS  To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.  Example −
  • 23. OPERATIONS ON STRINGS  CONCATENATION (+) :  adds value on the either sde of operator.  REPETITION (*) :  creates new strings,concatenating multiple copies of the same strings.  SLICE ([]) :  gives the character from the given index  RANGE SLICE ([ :]) :  gives the character from the given range.
  • 24. OPERATIONS ON STRINGS  MEMBERSHIP (in) or (not in) : membership returns true or false if the character exist in the given string.  length (len): It is used to find the length of the string.  upper() : upper() is used to uppercase the string .  lower() : lower() operation is used to lowercase the string.  THERE ARE MANY MORE OPERATIONS ON STRINGS
  • 25. MODIFYING STRINGS  Cannot update a string “in place” as strings are immutable in nature.  Example:  Instead ,use slices and concatenation for modification of strings:-  Example :
  • 26. SLICING IN STRINGS  A slice is a segment of string.  Slicing allows you to obtain substring  Syntax: Str[start : end]  Start: substring start from this element  End: end of substring excluding the element at this index  S[i:j] starts at s[i] and ends at s[j-1]  S[:j] means start at s[0], so s[0:j]  S[i:] means ends at s[len(s)-1]
  • 27. STRING FORMATTING  str.format () is used to format the string.  Examples: Replace argument by position in message string.  Replace argument by names in message string.
  • 29. CONTRIBUTERS • EXAMPLE 1: • ‘3d’ describes how to display the value 6. • ‘d ’ is a code that specifies that the value should be treated as an integer value • ‘3’is the width of the area to show 6. • EXAMPLE 2: • ‘6.2f’ describes how to display the value 46.89 • ‘f’ is a code specifies that 46.829 should be treated as a floating point value • ‘6’ shows the width of the area to show 46.89 • ‘.2’ shows number of digits to show after decimal point.