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

Chapter 10

Chapter 10 covers tuples and dictionaries in programming, detailing their definitions, properties, and operations. It explains tuple characteristics such as immutability, indexing, and methods, as well as dictionary features including key-value pairs, mutability, and various methods for accessing and modifying data. The chapter also includes examples and practical applications for both data structures.

Uploaded by

manojdolekar9
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 views24 pages

Chapter 10

Chapter 10 covers tuples and dictionaries in programming, detailing their definitions, properties, and operations. It explains tuple characteristics such as immutability, indexing, and methods, as well as dictionary features including key-value pairs, mutability, and various methods for accessing and modifying data. The chapter also includes examples and practical applications for both data structures.

Uploaded by

manojdolekar9
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

CHPATER 10

TUPLES AND
DICTIONARIES
CHPATER 10:
TUPLES AND DISCTIONARIES
• TUPLES • DICTIONARIES
• INTRODUCTION • INTRODUCTION
• ACCESSING ELEMENTS • CREATING A DICTIONARY
• TUPLE IS IMMUTABLE • ACCESSING AND MODIFYING
• TUPLE OPERATIONS • DICTIONARY OPERATIONS
• TUPLE METHOD & BUILT-IN FUNCTIONS • DICTIONARY METHOD
• TUPLE ASSINGMENT • MANIPULATING DICTIONARIES
• NESTED TUPLES • CHAPTER BLUEPRINT
• TUPLE HANDLING
TUPLE: INTRODUCTION
• Definition: A tuple is an ordered, immutable sequence of elements, which can be of different types such as integers, floats, strings,
lists, or even other tuples. Tuples are defined by enclosing elements in parentheses () and separating them with commas.

• Examples:

• tuple1 = (1, 2, 3, 4, 5)
• tuple2 = ('Economics', 87, 'Accountancy', 89.6)
• tuple3 = (10, 20, 30, [40, 50])
• tuple4 = (1, 2, 3, 4, 5, (10, 20))

• Single Element Tuples: A tuple with one element requires a comma to differentiate it from a regular parenthesis.

• tuple5 = (20,)
TUPLE: ACCESSING ELEMENTS
• Indexing: Elements in a tuple can be accessed using their index,
starting from 0.

• tuple1 = (2, 4, 6, 8, 10, 12)


• print(tuple1[0]) # Output: 2

• Slicing: Extracts a part of the tuple using the syntax [start:stop].

• print(tuple1[1:4]) # Output: (4, 6, 8)


TUPLE IS IMMUTABLE
• Tuples cannot be modified once created. Any attempt to change the
value of an element results in a TypeError.

• tuple1 = (1, 2, 3, 4, 5)
• # tuple1[4] = 10 # This will raise a TypeError
TUPLE OPERATIONS
• CONCATENATION OPERATION
• REPETITION OPERATION
• MEMBERSHIP OPERATION
• SLICING OPERATION
TUPLE: CONCATENATION OPERATION
• Concatenation: Combines two tuples using the + operator.

• tuple1 = (1, 2, 3)
• tuple2 = (4, 5, 6)
• tuple3 = tuple1 + tuple2
• print(tuple3) # Output: (1, 2, 3, 4, 5, 6)
TUPLE: REPETITION OPERATION
• Repetition: Repeats the elements of a tuple a specified number of
times using the * operator.

• tuple1 = (1, 2, 3)
• tuple4 = tuple1 * 3
• print(tuple4) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
TUPLE: MEMBERSHIP OPERATION
• Membership: Checks if an element is in a tuple using in and not in.

• tuple1 = ('Red', 'Green', 'Blue')


• print('Green' in tuple1) # Output: True
TUPLE: SLICING OPERATION
• Slicing: Slicing in tuples allows you to extract a portion of a tuple by
specifying a start, stop, and step index.
• The general syntax for slicing is:
• tuple[start:stop:step]

• start: The index where the slice begins (inclusive).


• stop: The index where the slice ends (exclusive).
• step: The step size (default is 1).
TUPLE: SLICING OPERATION
• Basic Slicing • Extracting with Default Start and Stop

• Extracting a Sub-Tuple
• tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) • tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
• sub_tuple = tuple1[2:5] • sub_tuple = tuple1[:4]
• print(sub_tuple) # Output: (30, 40, 50) • print(sub_tuple) # Output: (10, 20, 30, 40)
• This extracts elements from index 2 to 4 (index 5 is • This extracts elements from the beginning to index
excluded). 3.

• sub_tuple = tuple1[5:]
• print(sub_tuple) # Output: (60, 70, 80, 90, 100)
• This extracts elements from index 5 to the end.
TUPLE: SLICING OPERATION
• Using Negative Indices • Slicing with Positive Step

• tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) • tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
• sub_tuple = tuple1[-5:-2] • sub_tuple = tuple1[1:8:2]
• print(sub_tuple) # Output: (60, 70, 80) • print(sub_tuple) # Output: (20, 40, 60, 80)
• This extracts elements from index -5 to -3 (index -2 • This extracts elements from index 1 to 7 with a
is excluded). step of 2.

• sub_tuple = tuple1[-3:] • tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
• print(sub_tuple) # Output: (80, 90, 100) • sub_tuple = tuple1[8:1:-2]
• This extracts the last three elements. • print(sub_tuple) # Output: (90, 70, 50, 30)
• This extracts elements from index 8 to 2 (index 1 is
excluded) with a step of -2.
TUPLE: SLICING OPERATION
• Complex Slicing Example
• Reversing a Tuple
• tuple1 = (10, 20, 30, 40, 50, 60, 70,
• tuple1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
80, 90, 100) • sub_tuple = tuple1[1:-1:3]
• reversed_tuple = tuple1[::-1] • print(sub_tuple) # Output: (20, 50,
• print(reversed_tuple) # Output: 80)
(100, 90, 80, 70, 60, 50, 40, 30, 20, • This extracts elements from index 1
10) to -2 with a step of 3.
• This reverses the entire tuple.
TUPLE METHODS AND BUILT-IN FUNCTIONS
• len(): Returns the number of • count(): Returns the number of
elements in a tuple. occurrences of a value in a tuple.
• tuple1 = (1, 2, 3, 4, 5) • tuple1 = (1, 2, 2, 3, 4, 2)
• print(len(tuple1)) # Output: 5 • print([Link](2)) # Output: 3

• tuple(): Converts a list or string into • index(): Returns the index of the
a tuple. first occurrence of a value.
• print(tuple('Python')) # Output: • tuple1 = (1, 2, 3, 4, 5)
('P', 'y', 't', 'h', 'o', 'n') • print([Link](3)) # Output: 2
TUPLE METHODS AND BUILT-IN FUNCTIONS
• sorted(): Returns a sorted list of • min(), max(), sum(): Returns the
the tuple’s elements. minimum, maximum, and sum
• tuple1 = (5, 2, 8, 1, 3) of tuple elements respectively.
• print(sorted(tuple1)) # Output: • tuple1 = (5, 2, 8, 1, 3)
[1, 2, 3, 5, 8] • print(min(tuple1)) # Output: 1
• print(max(tuple1)) # Output: 8
• print(sum(tuple1)) # Output: 19
NESTED TUPLES
• Nested Tuples

• A tuple can contain other tuples, enabling multi-dimensional data


representation.

• st = ((101, "Aman", 98), (102, "Geet", 95))


• print(st[0][2]) # Output: 98
TUPLE HANDLING
• Swapping Values: • Functions Returning Tuples:

• num1, num2 = 10, 20 • def circle(r):


• num1, num2 = num2, num1 • return (3.14 * r * r, 2 * 3.14 * r)
• print(num1) # Output: 20
• print(num2) # Output: 10 • area, circumference = circle(5)
• print(area) # Output: 78.5
• print(circumference) # Output:
31.400000000000002
TUPLE HANDLING

• Storing Multiple Values:

• numbers = ()
• for i in range(5):
• num = int(input("Enter a number: "))
• numbers = numbers + (num,)
• print("Max number is:", max(numbers))
• print("Min number is:", min(numbers))
DICTIONARY
• Definition:
• A dictionary is a collection of key-value pairs. Each key must be
unique, and keys are used to retrieve their associated values.
Dictionaries are mutable, meaning they can be modified after
creation.
CREATING DICTIONARY AND
ACCESSING AND MODIFYING ITS ELEMENTS
• Creating a Dictionary: • Accessing and Modifying:

• dict1 = {'Name': 'Alice', 'Age': 25} • print(dict1['Name']) # Output:


• dict2 = {1: 'Python', 2: 'Java'} Alice
• print(dict1) • dict1['Age'] = 26
• print(dict2) • print(dict1) # Output: {'Name':
'Alice', 'Age': 26}
DICTIONARY METHODS &
BUILT-IN FUNCTIONS
• Adding Items: New key-value pairs can be • Updating Items: Existing items can be
added to a dictionary. updated.
• dict1['Gender'] = 'Female' • dict1['Name'] = 'Bob'
• print(dict1) # Output: {'Name': 'Alice', 'Age': • print(dict1) # Output: {'Name': 'Bob'}
26, 'Gender': 'Female’} • 10.7.2 Dictionary Methods
• Common Methods:
• Deleting Items: Items can be removed using
the del statement or the pop() method.
• del dict1['Age'] • clear(): Removes all items from the dictionary.
• print(dict1) # Output: {'Name': 'Alice', • [Link]()
'Gender': 'Female'} • print(dict1) # Output: {}
• [Link]('Gender')
• print(dict1) # Output: {'Name': 'Alice'}
DICTIONARY METHODS &
BUILT-IN FUNCTIONS
• get(): Returns the value for a specified • update(): Updates the dictionary with
key. another dictionary's key-value pairs.
• print([Link]('Name')) # Output: Alice • dict4 = {'Age': 30}
• [Link](dict4)
• items(): Returns a view object containing • print(dict1) # Output: {'Name': 'Alice',
key-value pairs. 'Gender': 'Female', 'Age': 30}
• print([Link]()) # Output:
dict_items([('Name', 'Alice'), ('Age', 25)]) • values(): Returns a view object containing
values.
• keys(): Returns a view object containing • print([Link]()) # Output:
keys. dict_values(['Alice', 'Female', 30])
• print([Link]()) # Output:
dict_keys(['Name', 'Age'])
MANIPULATING DICTIONARIES
#Program to create a dictionary which stores names of the employee
#and their salary

num = int(input("Enter the number of employees whose data to be stored: "))


count = 1
employee = dict() #create an empty dictionary
while count <= num:
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
count += 1

print("\n\nEMPLOYEE_NAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
CHAPTER BLUEPRINT
VSA SA LA E TOTAL

1 MARK 2 MARKS 3 MARKS 5 MARKS

2 MCQ 2 1 ------- 10 MARKS

1 FIB

You might also like