0% found this document useful (0 votes)
92 views17 pages

List Tuples Set Dict

This document discusses Python lists, tuples, sets, and dictionaries. Lists are ordered collections that allow duplicate elements and support operations like slicing and modification. Tuples are similar to lists but are immutable. Sets only allow unique elements. Dictionaries store mappings of unique keys to values.

Uploaded by

alaa abu madi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views17 pages

List Tuples Set Dict

This document discusses Python lists, tuples, sets, and dictionaries. Lists are ordered collections that allow duplicate elements and support operations like slicing and modification. Tuples are similar to lists but are immutable. Sets only allow unique elements. Dictionaries store mappings of unique keys to values.

Uploaded by

alaa abu madi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy