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

Tuple Assignment, Tuple As Return Value - Sets-Dictionaries

The document explains tuple assignment in Python, highlighting its immutability and ability to return multiple values from functions. It also covers sets as unordered collections of unique elements and dictionaries as collections of key-value pairs, emphasizing their creation, access, and limitations regarding duplicates. Examples are provided for each data structure to illustrate their usage.

Uploaded by

Dept CSCA
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)
24 views6 pages

Tuple Assignment, Tuple As Return Value - Sets-Dictionaries

The document explains tuple assignment in Python, highlighting its immutability and ability to return multiple values from functions. It also covers sets as unordered collections of unique elements and dictionaries as collections of key-value pairs, emphasizing their creation, access, and limitations regarding duplicates. Examples are provided for each data structure to illustrate their usage.

Uploaded by

Dept CSCA
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

V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

Tuple assignment, tuple as return value -Sets–Dictionaries

Tuple Assignment

❖ Tuples are immutable sequences.


❖ It is used to store collections of heterogeneous data.
❖ Tuple assignment is a method of assigning multiple values to
multiple variables in a single statement.

Example

a, b = 1, 2

print(a)

# Output: 1

print(b)

# Output: 2

❖ You can also use tuple assignment to swap values without a


temporary variable

Example

a,b=5,10

print('A =',a)

print('B =',b)

a,b=b,a

print('A =',a)

print('B =',b)

Output:
V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

A=5

B = 10

A = 10

B=5

Tuple As Return Value

❖ In Python, functions can only return one value.


❖ Functions in Python can return multiple values using tuples.
❖ This allows you to easily return multiple results from a function.

Example 1:

def get_student_info(name, age, grade):

return (name, age, grade)

student_info = get_student_info("Cyber", 16, "A")

print(student_info[0]) # Output: Cyber

print(student_info[1]) # Output: 16

print(student_info[2]) # Output: A

Example 2:

def get_coordinates():

x=5

y = 10

return x, y
V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

coordinates = get_coordinates()

print(coordinates)

Output: (5, 10)

Benefits of using tuples as return values

• Clarity: It clearly indicates that the function is returning a group of


related values.
• Convenience: It simplifies the process of returning multiple values from
a function.
• Flexibility: You can return values of different data types within the same
tuple.
• Efficiency: Tuples are lightweight and efficient to work

SETS

❖ Sets are unordered collections of unique elements.


❖ They are useful for removing duplicates and performing common
mathematical operations like unions and intersections.
❖ A set is a collection which is unchangeable
Example 1:
a={0,1,1,1,2,3,'A','B','A'}
print(a)
Output:
{0, 1, 2, 3, 'B', 'A'}
Example 2:
V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

a={1,2,3,4}
print(a)
a.add(5)
print(a)
a.remove(3)
print(a)
Output:

{1, 2, 3, 4}

{1, 2, 3, 4, 5}

{1, 2, 4, 5}

DICTIONARIES

Dictionaries are collections of key-value pairs. They are indexed by keys, which
can be of any immutable type.

Creating a dictionary:

my_dict = {'name': 'Alice', 'age': 25}

Accessing values:

print(my_dict['name'])

Adding or updating key-value pairs

my_dict['address'] = '123 Main St'


Example:
my_dict = {'name': 'Alice', 'age': 25, 'address':'Erode'}
print(my_dict['name'])
print(my_dict['address'])
V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

my_dict['address'] = 'Coimbatore'
print(my_dict['address'])
Output:
Alice
Erode
Coimbatore
Duplicates Not Allowed

Dictionaries cannot have two items with the same key.

Duplicate values will overwrite existing values

Example:

my_dict = {'name': 'Alice', 'age': 25, 'address':'Erode', 'age':30}

print(my_dict['name'])

print(my_dict['age'])

print(my_dict['address'])

my_dict['address'] = 'Coimbatore'

print(my_dict['address'])

Output:

Alice

30

Erode

Coimbatore
V SURESH KUMAR HEAD, COMPUTER APPLICATIONS, SVASC

You might also like