List Tuples Set Dict

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Python

Lists, Tuples, Sets, and Dictionaries


Lists
• list is an ordered collection of elements.
• Each element in a list can be of any data type, such as strings,
integers, or other objects.
• Lists are mutable, which means that you can add, delete, or modify
the elements in the list.
Lists
• Creating List:
You can create a list by enclosing a comma-separated sequence of
elements in square brackets []. For example:
Lists
• Accessing Elements in a List:
You can access elements in a list by their index, which starts from 0. For
example:
Lists
• Slicing a List:
You can also access a range of elements in a list by using slicing. Slicing
is done by specifying the start and end indices separated by a colon.

For example:
Lists
• More on Slicing
Lists
• Modifying Elements in a List:
You can modify an element in a list by assigning a new value to it using
its index. For example:
Lists
• Adding Elements to a List:
You can add elements to a list using the append() method. For example:
Lists
• Removing Elements from a List:
You can remove elements from a list using the remove() method. For
example:
Tuples
• A tuple is a collection of elements enclosed in parentheses and
separated by commas.
• Tuples are similar to lists, but they are immutable, which means you
cannot modify their contents after they have been created.

• It can also be created without parentheses:


Tuples
• Tuples support indexing and slicing, just like lists.
Here's an example of indexing a tuple:
Tuples
• Tuples can be nested:
Set
• A set is an unordered collection of unique elements.
• Sets are defined by enclosing a comma-separated list of elements
within curly braces {}. Here's an example:

• You can also create a set using the set() function and passing a list,
tuple or any other iterable object as an argument:
Set
• Sets do not allow duplicate elements, so any duplicate elements in the
input are automatically removed when creating a set. Here's an
example:
Set
• Adding elements to Set:

• Removing element from set:


Dictionary
• A dictionary is an unordered collection of key-value pairs.
• Dictionaries are defined using curly braces {} and key-value pairs are
separated by a colon :. Here's an example of a dictionary:
Dictionary
• You can also add, remove, and modify key-value pairs in a dictionary.
Here's an example of adding a new key-value pair:
my_dict = {"apple": 2.99, "banana": 1.99, "orange": 0.99}
my_dict["kiwi"] = 3.99
print(my_dict) # Output: {"apple": 2.99, "banana": 1.99, "orange": 0.99, "kiwi": 3.99}

• Removing a key-value pair:

You might also like