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

Python Unit 4 Presentation

The document discusses list aliasing and cloning methods, including list slicing, copy(), list constructor, and deepcopy(). It also covers tuples, their properties, and tuple assignment, as well as dictionaries, their operations, and methods. Additionally, it introduces advanced list processing techniques such as list comprehension, map(), and filter().
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)
4 views21 pages

Python Unit 4 Presentation

The document discusses list aliasing and cloning methods, including list slicing, copy(), list constructor, and deepcopy(). It also covers tuples, their properties, and tuple assignment, as well as dictionaries, their operations, and methods. Additionally, it introduces advanced list processing techniques such as list comprehension, map(), and filter().
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/ 21

List Aliasing

• When two variables refer to the same list in


memory.
Example:
list1 = [1, 2, 3]
list2 = list1
list2[0] = 100 Output
print(list1) [100, 2, 3]
Print(list2) [100, 2, 3]

• Note: Changing one affects the other.


Cloning Lists
• Creating a new list with the same elements
(Copying list elements).

Methods of List Cloning:


• List slicing – [:]
• copy() method
• List constructor (function) – list()
• deepcopy() method
List Cloning Using Slicing
Example:
list1 = [1, 2, 3]
list2 = list1[:]
list2[0] = 100 Output
print(list1) [1, 2, 3]
print(list2) [100, 2, 3]

Note: Changing one doesn’t affects the other.


List Cloning Using copy()
Example:
list1 = [1, 2, 3]
list2 = list1.copy()
list2[0] = 100 Output
print(list1) [1, 2, 3]
print(list2) [100, 2, 3]

Note: Changing one doesn’t affects the other.


List Cloning Using List Constructor
Example:
list1 = [1, 2, 3]
list2 = list(list1)
list2[0] = 100 Output
print(list1) [1, 2, 3]
print(list2) [100, 2, 3]

Note: Changing one doesn’t affects the other.


List Cloning Using deepcopy()
• The deepcopy() is used to copy the elements of
nested list.
Example:
import copy
list1 = [[1, 2, 3], [4,5,6], [7,8,9]
list2 = copy.deepcopy(list1)
list2[1][2] = 600 Output
print(list1) [[1, 2, 3], [4,5,6], [7,8,9]]
print(list2) [[1, 2, 3], [4,5,600],
[7,8,9]]
List Parameters
• Passing lists as arguments to functions.
Example:
def modify_list(list1):
list1.append(10)
my_list = [1, 2, 3]
modify_list(my_list) Output
print(my_list) [1, 2, 3, 10]

Note: Lists are mutable, changes inside function affect


original list.
Tuples
• Immutable Example:
• Ordered a = (1, 2, 3)
• Allows Duplicates
• Can Contain Multiple Data Types
• Faster than Lists
• Uses Parentheses ()
• Single Element Tuple Needs a Comma a = (1,)
Tuple Assignment
• Assigning multiple variables at once using
tuples.

Example:
a, b = (1, 2) Output
print(a) 1
print(b) 2
Tuple Assignment

fruits = (“apple”, “banana”, “cherry“)


x, y, z = fruits
print(x) ‘apple’
print(y) ‘banana’
‘cherry’
print(z)
Tuple as Return Value
• Functions can return multiple values using
tuples.

Example:
def min_max(numbers):
return min(numbers), max(numbers)
result = min_max([1, 2, 3, 4])
(1,4)
print(result)
Dictionaries
• Stores data as key-value pairs
• Mutable (can be changed after creation)
• Keys must be unique and hashable
• Fast lookup using keys
• Uses curly braces {} for definition
• Can store any data type as values (numbers,
strings, lists, even other dictionaries)
Dictionaries - Operations
student = { "name": "Alice", "age": 21, "course": "AI"}
Print(student["name"])
student["grade"] = "A”
print(student)
student["age"] = 22
print(student)
del student["course"]
print(student)

Alice
{'name': 'Alice', 'age': 21, 'course': 'AI', 'grade': 'A’}
{'name': 'Alice', 'age': 22, 'course': 'AI', 'grade': 'A’}
{'name': 'Alice', 'age': 22, 'grade': 'A’}
Dictionaries - Operations
Students = {‘name’: ‘Alice’, ‘age’: 22, ‘grade’: ‘A’}
for key, value in student.items():
print(key, ":", value)

name : Alice
age : 22
grade : A
Dictionaries - Methods
student = { "name": "Alice", "age": 18, "course": "AI"}
print(student.get("name"))
print(student.get("grade", "N/A"))
print(student.keys())
print(student.values())
print(student.items())

Alice
N/A
dict_keys(['name', 'age', 'course’])
dict_values(['Alice’,18, 'AI’])
dict_items([('name', 'Alice'), ('age’,18), ('course', 'AI’)])
Dictionaries - Methods
student.update({"age": 19, "grade": "A"})
print("Updated student:", student)
removed = student.pop("course")
print("Removed course:", removed)
print("After pop:", student)
student.clear()
print("After clear:", student)

Updated student: {'name': 'Alice', 'age’: 19, 'course': 'AI', 'grade': 'A’}
Removed course: AI
After pop: {'name': 'Alice', 'age’: 19, 'grade': 'A’}
After clear: {}
Advanced List Processing - List
Comprehension
• Creates a new list by applying an expression to
each item in a sequence, optionally filtering with
a condition
Syntax:
[expression for item in iterable if condition]
Example:
numbers = [1, 2, 3, 4]
doubled = [x * 2 for x in numbers] [2, 4, 6, 8]
print(doubled)
List Comprehension
squares = [x*x for x in range(1, 6)]
print(squares)

[1, 4, 9, 16, 25]

even_sqr = [x*x for x in range(10) if x%2==0]


print(even_sqr)

[0, 4, 16, 36, 64]


Advanced List Processing
• Map() – Apply a function to each item in a list

numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x*x, numbers))
print(squares)

[1, 4, 9, 16]
Advanced List Processing
• Filter() – Keep items that match a condition

numbers = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even)

[2, 4, 6]

You might also like