[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

Unit 3 - Python Programming

This document provides an overview of Lists, Tuples, Dictionaries, and Sets in Python. It covers definitions, creation methods, accessing elements, and common operations for each data structure. Additionally, it highlights use cases and specific methods associated with each type.

Uploaded by

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

Unit 3 - Python Programming

This document provides an overview of Lists, Tuples, Dictionaries, and Sets in Python. It covers definitions, creation methods, accessing elements, and common operations for each data structure. Additionally, it highlights use cases and specific methods associated with each type.

Uploaded by

Ayush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 3: List, Tuple, Dictionaries, and Sets

Lists

Definition:​
A list is a mutable, ordered collection of items in Python, enclosed in square brackets []. Lists
can hold elements of different data types (int, float, string, etc.).

Creating a List:

Accessing Elements:

Use indices to access list items (starting from 0).

Slicing a List:

Retrieve a part of the list using the syntax list[start:stop:step].

Negative Indices:

Negative indexing starts from the end of the list.


List Methods:

Some commonly used methods include:

●​ append(x) – Adds item x to the end.


●​ insert(i, x) – Inserts item x at index i.
●​ remove(x) – Removes first occurrence of x.
●​ pop([i]) – Removes and returns element at index i.
●​ sort() – Sorts the list in ascending order.
●​ reverse() – Reverses the list.

Example:

List Comprehensions:

A concise way to create lists.


Tuples

Definition:​
Tuples are immutable, ordered collections of items, enclosed in parentheses ().

Creating a Tuple:

Indexing and Slicing:

Access elements using positive or negative indexing. Slicing works similarly to lists.

Operations on Tuples:

●​ Concatenation: tuple1 + tuple2


●​ Repetition: tuple * n
●​ Membership: x in tuple

Immutability:

●​ Once created, tuple elements cannot be changed. This provides data integrity.

Usage:

●​ Used when data should not change, e.g., storing days of the week.
Dictionaries

Definition:​
Dictionaries are unordered collections of key-value pairs, enclosed in {}.

Creating a Dictionary:

Accessing and Replacing Values:

Adding New Key-Value Pairs:

Dictionary Operations:

●​ keys(): Returns all keys.


●​ values(): Returns all values.
●​ items(): Returns key-value pairs.
●​ get(key): Returns value for the key.
●​ pop(key): Removes the key and returns value.
●​ update(): Updates with another dictionary.​
Example:

Use Case of Dictionaries:

●​ Efficient lookups, storing mappings like name-phone pairs, etc.

Sets

Definition:​
A set is an unordered collection of unique elements, defined using curly braces {} or the set()
function.

Creating a Set:

Operations on Sets:

●​ add(x): Adds an element.


●​ remove(x): Removes an element (raises error if not found).
●​ discard(x): Removes an element (no error if not found).
●​ pop(): Removes random element.
●​ clear(): Empties the set.

Set Operations:

●​ union(): Combines elements from two sets.


●​ intersection(): Elements common to both sets.
●​ difference(): Elements in one set but not the other.
●​ symmetric_difference(): Elements in either set but not both.​
Example:

Use Case:

●​ Used when duplicate values are not allowed, such as collecting unique elements from a
list.

You might also like