Python
Data structures
Mohan
Consultant
Certification
Mohan Arumugam
Technologies Specialist
E-mail : moohanan@gmail.com
Phone : +91 99406 53876
Profile
Blogger
Trainer
Who Am I ?
Agenda
• List
• Dictionaries
• Tuples
• Strings
• Sets
List
• WhatisList?
• WherewecanuseofList?
What is List?
In Python, a list is a built-in data structure that represents a
collection of items. Lists are ordered, mutable (modifiable), and can
contain elements of different data types, including other lists. They
are defined by enclosing comma-separated values within square
brackets [ ].
Here's an example of a list:
• my_list = [1, 2, 3, 4, 5]
In this example, my_list is a list containing five integer
elements.
Lists offer several key features:
 Ordered: Lists maintain the order of elements as they are inserted. Elements are indexed starting from 0, so you
can access elements by their position.
 Mutable: Lists can be modified after creation. You can add, remove, or modify elements within a list.
 Heterogeneous: Lists can contain elements of different data types. For example, you can have integers, strings,
floats, or even other lists as elements of a single list.
 Dynamic: Lists in Python can grow or shrink dynamically as elements are added or removed. There's no need to
predefine the size of a list.
Here are some basic operations you can perform on lists:
o Accessing elements by index: my_list[0]
o Slicing: my_list[1:3]
o Appending elements: my_list.append(6)
o Removing elements: my_list.remove(4)
o Checking if an element exists: element in my_list
o Length of the list: len(my_list)
Lists are incredibly versatile and are used extensively in Python for a wide range of tasks, including storing collections
of data, representing sequences, and implementing various algorithms and data structures.
 We can use the append() method or extend() method to add elements to a list.
The append() method adds an object to the end of a list and extend() method
adds each element of an iterable object to a list. To update an existing element in a
list, assign the new value to that position using its index. See the examples.
 sample_list.append(1) :
 Output : [‘sample’, ‘list’, 45, ‘test’, 1]
 sample_list.append([1, 2]) :
 Output : [‘sample’, ‘list’, 45, ‘test’, [1, 2]]
 sample_list.extend([1,3,2]) :
 Output : [‘sample’, ‘list’, 45, ‘test’, 1, 3, 2]
 sample_list[0] = ‘new_val’
 Output : [‘new_val’, ‘list’, 45, ‘test’]
List..
 To delete elements, we can use del statement.
 del sample_list[2] : //deletes element with index 2.
  The sample_list.insert() method will insert an element at a particular position. The format
is,
 sample_list.insert(i, x). //It will insert element ‘x’ at position ‘i’.
 The method, sample_list.remove(x) will remove the first element in the list whose value is ‘x’.
 sample_list.pop(i) removes the item at the given position in the list, and return it
 If no index is specified, sample_list.pop() removes and returns the last item in the list.
 sample_list.clear() removes all the items from the list.
 It is equivalent to del sample_list[:].
 sample_list.count(x) will return the number of times ‘x’ appears in the list.
 The reverse() method can be used to reverse the elements in a list
 copy() will create another copy of the list.
List..
• A dictionary contains a sequence of key-value pairs. We can access keys and values of a dictionary
independently. Dictionary elements are enclosed by curly brackets.
• d = {‘key1’: ‘first’, ‘key2’: [6, 7] }
• d.keys() will list all the keys and d.values() will list all the values of the dictionary.
• In addition to keys and values methods, there is also items() method which returns a list of items in
the form (key, value). The items are not returned in any particular order.
Directory..
Q & A
Mohan Arumugam
Technologies Specialist
E-mail : moohanan@gmail.com
Phone : +91 99406 53876
Profile
Thank You

Python - List, Dictionaries, Tuples,Sets

  • 1.
  • 2.
    Consultant Certification Mohan Arumugam Technologies Specialist E-mail: moohanan@gmail.com Phone : +91 99406 53876 Profile Blogger Trainer Who Am I ?
  • 3.
    Agenda • List • Dictionaries •Tuples • Strings • Sets
  • 4.
  • 5.
    What is List? InPython, a list is a built-in data structure that represents a collection of items. Lists are ordered, mutable (modifiable), and can contain elements of different data types, including other lists. They are defined by enclosing comma-separated values within square brackets [ ]. Here's an example of a list: • my_list = [1, 2, 3, 4, 5] In this example, my_list is a list containing five integer elements.
  • 6.
    Lists offer severalkey features:  Ordered: Lists maintain the order of elements as they are inserted. Elements are indexed starting from 0, so you can access elements by their position.  Mutable: Lists can be modified after creation. You can add, remove, or modify elements within a list.  Heterogeneous: Lists can contain elements of different data types. For example, you can have integers, strings, floats, or even other lists as elements of a single list.  Dynamic: Lists in Python can grow or shrink dynamically as elements are added or removed. There's no need to predefine the size of a list. Here are some basic operations you can perform on lists: o Accessing elements by index: my_list[0] o Slicing: my_list[1:3] o Appending elements: my_list.append(6) o Removing elements: my_list.remove(4) o Checking if an element exists: element in my_list o Length of the list: len(my_list) Lists are incredibly versatile and are used extensively in Python for a wide range of tasks, including storing collections of data, representing sequences, and implementing various algorithms and data structures.
  • 7.
     We canuse the append() method or extend() method to add elements to a list. The append() method adds an object to the end of a list and extend() method adds each element of an iterable object to a list. To update an existing element in a list, assign the new value to that position using its index. See the examples.  sample_list.append(1) :  Output : [‘sample’, ‘list’, 45, ‘test’, 1]  sample_list.append([1, 2]) :  Output : [‘sample’, ‘list’, 45, ‘test’, [1, 2]]  sample_list.extend([1,3,2]) :  Output : [‘sample’, ‘list’, 45, ‘test’, 1, 3, 2]  sample_list[0] = ‘new_val’  Output : [‘new_val’, ‘list’, 45, ‘test’] List..
  • 8.
     To deleteelements, we can use del statement.  del sample_list[2] : //deletes element with index 2.   The sample_list.insert() method will insert an element at a particular position. The format is,  sample_list.insert(i, x). //It will insert element ‘x’ at position ‘i’.  The method, sample_list.remove(x) will remove the first element in the list whose value is ‘x’.  sample_list.pop(i) removes the item at the given position in the list, and return it  If no index is specified, sample_list.pop() removes and returns the last item in the list.  sample_list.clear() removes all the items from the list.  It is equivalent to del sample_list[:].  sample_list.count(x) will return the number of times ‘x’ appears in the list.  The reverse() method can be used to reverse the elements in a list  copy() will create another copy of the list. List..
  • 9.
    • A dictionarycontains a sequence of key-value pairs. We can access keys and values of a dictionary independently. Dictionary elements are enclosed by curly brackets. • d = {‘key1’: ‘first’, ‘key2’: [6, 7] } • d.keys() will list all the keys and d.values() will list all the values of the dictionary. • In addition to keys and values methods, there is also items() method which returns a list of items in the form (key, value). The items are not returned in any particular order. Directory..
  • 10.
  • 11.
    Mohan Arumugam Technologies Specialist E-mail: moohanan@gmail.com Phone : +91 99406 53876 Profile Thank You