Sunday, June 11, 2023

Chapter 5: Data Structures: Lists, Tuples, and Dictionaries


5.1 Lists 5.1.1 Creating and accessing lists Explanation: Lists are ordered collections of items that can be of different data types. They can be created using square brackets and accessed using indexing and slicing. Example:

python

///Example

fruits = ["apple", "banana", "orange"]

print(fruits[0])  # Accessing the first element

print(fruits[1:3])  # Slicing to get a sublist

 

5.1.2 Modifying lists: adding, removing, and updating elements Explanation: Lists are mutable, which means their elements can be modified. Elements can be added using append(), removed using remove() or del, and updated using assignment. Example:

python

///Example

fruits = ["apple", "banana", "orange"]

fruits.append("grape")  # Adding a new element

fruits.remove("banana")  # Removing an element

fruits[0] = "kiwi"  # Updating an element

print(fruits)

 

5.1.3 List operations: concatenation, repetition, and length Explanation: Lists can be concatenated using the + operator, repeated using the * operator, and their length can be obtained using the len() function. Example:

python

///Example

list1 = [1, 2, 3]

list2 = [4, 5, 6]

result = list1 + list2  # Concatenation

print(result)

 

list3 = [0] * 4  # Repetition

print(list3)

 

length = len(result)  # Length

print(length)

 

5.2 Tuples 5.2.1 Creating and accessing tuples Explanation: Tuples are similar to lists but are immutable, meaning their elements cannot be modified. They can be created using parentheses and accessed using indexing and slicing. Example:

python

///Example

person = ("Alice", 25, "New York")

print(person[0])  # Accessing the first element

print(person[1:])  # Slicing to get a subtuple

 

5.2.2 Tuple packing and unpacking Explanation: Tuple packing allows you to create a tuple by grouping multiple values together. Tuple unpacking allows you to assign the elements of a tuple to individual variables. Example:

python

///Example

person = ("Alice", 25, "New York")

name, age, city = person  # Unpacking

print(name)

print(age)

print(city)

 

tuple1 = 1, 2, 3  # Packing

print(tuple1)

 

5.3 Dictionaries 5.3.1 Creating and accessing dictionaries Explanation: Dictionaries are unordered collections of key-value pairs. They can be created using curly braces or the dict() constructor and accessed using keys. Example:

python

///Example

student = {"name": "Alice", "age": 20, "grade": "A"}

print(student["name"])  # Accessing value using key

 

5.3.2 Modifying dictionaries: adding, removing, and updating key-value pairs Explanation: Dictionaries are mutable, so their key-value pairs can be added, removed, or updated using assignment and the del keyword. Example:

python

///Example

student = {"name": "Alice", "age": 20, "grade": "A"}

student["city"] = "New York"  # Adding a new key-value pair

del student["grade"]  # Removing a key-value pair

student["age"] = 21  # Updating a value

print(student)

 

5.3.3 Dictionary operations: keys, values, and length Explanation: Dictionaries provide methods to access keys, values, and obtain the length of the dictionary using len(). Example:

python

///Example

student = {"name": "Alice", "age": 20, "grade": "A"}

keys = student.keys()  # Getting keys

values = student.values()  # Getting values

length = len(student)  # Length

print(keys)

print(values)

print(length)

 

This chapter covers the fundamental data structures in Python: lists, tuples, and dictionaries. Lists are ordered collections that can be modified, tuples are immutable collections, and dictionaries are unordered key-value pairs. The examples provided demonstrate the usage and syntax of each topic, helping readers understand how to apply these concepts in their own programs.

0 comments:

Post a Comment