0% found this document useful (0 votes)
4 views

Python Lec 05

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Lec 05

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Tuples

• Ordered, immutable sequences: Tuples represent ordered collections of


elements, similar to lists. However, once a tuple is created, its elements cannot
be changed. This makes them ideal for storing data that shouldn't be modified
after creation.
• Code example:


Immutability: Tuples are designed to be fixed. This ensures data integrity and
prevents accidental modifications.
Tuple Methods
• .index(x): Returns the index of the first occurrence of element x in the tuple.
Raises a ValueError if the element is not found.
• .count(x): Counts the number of times element x appears in the tuple.
• Code example:

Tuple Slicing
• Extracting sub-tuples: Similar to lists, you can extract portions of a tuple using
slicing syntax [start:end:step]. The elements from start (inclusive) to end
(exclusive) are extracted with a step of step.
• Code example:

Dictionaries
• Unordered collections: Dictionaries store key-value pairs, allowing you to
associate unique keys with corresponding values. Unlike tuples, dictionaries are
mutable, meaning you can modify their contents after creation.
• Real-life example: Phonebook entries where the name (key) is associated with
a phone number (value).
• Code example:

What Data Types Can Be Stored in Dictionaries?


• Dictionaries can store any data type as keys and values, including: | Data Type |
Example | |---|---| | Integers | 10 | | Floats | 3.14 | | Strings | "hello" | | Booleans |
True, False | | Lists | [1, 2, 3] | | Tuples | (x, y) | | Even other dictionaries! |
{...} |
Properties of Dictionaries
• Unordered: The order in which key-value pairs are added or appear in the
dictionary is not guaranteed.
• Mutable: You can modify elements (add, remove, or change key-value pairs)
after creation.
Nested Dictionaries
• Nested dictionaries allow you to create hierarchical structures within a single
dictionary. You can store other dictionaries as values within a dictionary. This is
useful for representing complex data with multiple levels of organization.
• Code example:

How to Access Nested Dictionaries


• You can access elements in nested dictionaries by chaining keys using square
brackets []. Treat each level of the hierarchy as a separate dictionary and use
the corresponding key to access the value at that level.
Dictionary Methods
• keys(): Returns a view of all the keys in the dictionary.
• values(): Returns a view of all the values in the dictionary.
• items(): Returns a view of all the key-value pairs in the dictionary as tuples.
• get(key, default): Returns the value associated with the key. If the key is not
found, it returns the default value (if provided) or None otherwise.
• update(other_dict): Updates the dictionary by adding key-value pairs from the
other_dict.

Code example:

You might also like