Tuple Assignment, Tuple As Return Value - Sets-Dictionaries
Tuple Assignment, Tuple As Return Value - Sets-Dictionaries
Tuple Assignment
Example
a, b = 1, 2
print(a)
# Output: 1
print(b)
# Output: 2
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
Example 1:
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)
SETS
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:
Accessing values:
print(my_dict['name'])
my_dict['address'] = 'Coimbatore'
print(my_dict['address'])
Output:
Alice
Erode
Coimbatore
Duplicates Not Allowed
Example:
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