__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> Introduction of tuple
--> creating homogeneous tuple
--> creating heterogeneous tuple
--> Element accessing
--> Built-in tuple Methods , tuple concatination, .format()
--> Interview Questions List&Tuple
"""
""" ############### tuple ##############################
Tuple is hetrogenious immutable sequence
Tuples are used to return multiple values from a function.
"" ==> string
[] ==> list
() ==> tuple
# ganesh = 10
# ganesh = 20
# print(ganesh) # it will print 20.
"""
"""To store 2 or more values in single variable, till now we have used list concept
now we can use tuple concept for storing multiple values, but tuple is immutable
Note : if user tried to assign multiple values to a single variable at a time,then those values will be stored in tuple
"""
venkat = 11,"narendra", 4.5, 66
# print(venkat, type(venkat))
x = [30,40,"venkat"]
# print(x)
# x = [30]
# print(x)
# y = ("rindha")
# print(y) # it will print
# y = ("rindha",)
# print(y)
# print(len(y)) # no of elements 1
"""
Definiton: Tuple is a heterogeneous & immutable sequence of elements"""
"""creating homogeneous tuple"""
tuple_1 = (1, 2, 3, 4, 5, 6) # similar data type elements
"""creating heterogeneous tuple"""
tuple_2 = (1, "Narendra", 44.5, 66, [22, 44, "Surendra", "LakshyaSree"], (1, "N", 444), 55) # Different data type elements
# print(tuple_2, "\n", type(tuple_2), "\nNumber of elements present in the given tuple are : ", len(tuple_2)) # printing tuple
# print(tuple_2[0]) # single element accessing from tuple using positive index
# print(tuple_2[-1]) # single element accessing from tuple using Negative index
# print(tuple_2[1:4]) # range of element accessing from tuple
# print(tuple_2[5][2]) # element accessing from Nested tuple
# print(tuple_2[-2][-1]) # element accessing from Nested tuple
# print(tuple_2[-3][-2]) # element accessing from Nested tuple
# tuple_2[1] = "srinivas" # tuple is immutalble due to that, TypeError: 'tuple' object does not support item assignment
# print(tuple_2)
tuple_2 = (1, "Narendra", 44.5, 66, [22, 44, "Surendra","LakshyaSree"], (1,"N",444), 55)
"""Omitting the stop index access to the end"""
# print(tuple_2[2: ]) # (44.5, 66, [22, 44, 'Surendra', 'LakshyaSree'], (1, 'N', 444), 55)
"""Omitting the starting address, accessing from the beginning."""
# print(tuple_2[ :4]) # (1, 'Narendra', 44.5, 66)
"""Omitting the ending address, accessing from the respective index till end"""
# print(tuple_2[3: ])
"""step element accessing from a tuple"""
s = ('show', 'how', 'to', 'index', 'into', 'sequence')
# print(s[1:5:2]) # ('how', 'index')
# print(s[ : :2]) # ('show', 'to', 'into')
# print(s[ : :-1]) # Reverse the ('sequence', 'into', 'index', 'to', 'how', 'show')
# print(s[ : :-3]) # ('sequence', 'to')
""" Required o/p is as a tuple, except 1st element and last element"""
# tuple_2 = (1, 'Narendra', 44.5, 66, [22, 44, 'Surendra', 'LakshyaSree'], (1, 'N', 444), 55)
# print(tuple_2[1:-1] ) # ('Narendra', 44.5, 66, [22, 44, 'Surendra', 'LakshyaSree'], (1, 'N', 444))
# print(tuple_2) # (1, 'Narendra', 44.5, 66, [22, 44, 'Surendra', 'LakshyaSree'], (1, 'N', 444), 55)
""" Converting a list into tuple"""
# list_1 = [1, 'Narendra', 44.5, [22, 44, 'Surendra', 'LakshyaSree']]
# tuple_2 = tuple(list_1) # where tuple() is a constructer
# print(tuple_2, type(tuple_2)) # (1, 'Narendra', 44.5, [22, 44, 'Surendra', 'LakshyaSree']) <class 'tuple'>
""" Converting a tuple into list"""
tuple_2 = (1, "Narendra", 44.5, 66, [22, 44, "Surendra","LakshyaSree"], (1,"N",444), 55)
# B = list(tuple_2)
# print(B, type(B)) # [1, 'Narendra', 44.5, [22, 44, 'Surendra', 'LakshyaSree']] <class 'list'>
# B.insert(2, "venkat")
# B.remove(55)
# print(B)
# c = tuple(B)
# print(c)
# print(tuple_2) # original tuple will not change
"""====Built-in tuple Methods (2)====================
immutable:
You can’t add elements to a tuple. There’s no append() or extend(), no insert() methods for tuples,
You can’t remove elements from a tuple. Tuples have no remove() or pop(), clear() methods
You can find elements in a tuple since this doesn’t change the tuple.
You can also use the "in" operator to check if an element exists in the tuple.
So, if you’re defining a constant set of values and
you’re going to do with it is iterate through it, use a tuple instead of a list.
tuple will be faster than working with lists and also safer, as the tuples contain “write-protect” data.
tuple have only 2 inbuilt methods --> count(), index()"""
tuple_2 = (1, "Narendra", 22, 44.5, 66, [22, 44, "Surendra",22, "LakshyaSree"], (1, "N", 22, 444), 55, 22)
# print(tuple_2.count(22))
# print(tuple_2.index(44.5))
input_tuple =("Divya", "Swathi", "bhagyasree", "Aruna", "aparna","swarupa","swapna","swathi")
# print(min(input_tuple))
# print(max(input_tuple))
# A = 65 # ASCII number of A
# a = 97 # ASCII number of A
""" Tuple concatination """
x = ("Radha", 2,5.5,"Bhagyasree",2, "c")
y = (1, 2, 3)
z = x + y
# print(z) # ('Radha', 2, 5.5, 'Bhagyasree', 2, 'c', 1, 2, 3)
# print(x)
# print(y)
# print("siri" in x) # Membership Output:False
# print("siri" not in x) # Membership Output:True
""" .format() ==> inbuilt method of string"""
a = [1, 23, 4, 77, 45, 5.5, 5]
f = (4, 623, 4, 2, 4, 4.5,4,5,6,7)
c = ["Narendra", "Swathi", 7, 7, 5.5]
# print("Length of the list1 is {} Length of 2nd list {} 3rd list Length is {}".format(len(a), len(f), len(c)))
name = input("enter name: ")
qualification = input("enter qualification: ")
designation = input("enter designation: ")
x = "He is Mr.{0} and {0}'s qualification is {1} and {0}'s designation is {2} ".format(name, qualification, designation)
print(x)
# ###============ Interview Questions List&Tuple ======================
"""Can you modify List elements which are placed in Tuple ?
input_tuple =(1, 2, 3.5,"Meera", [16, 17, 23], 45, 67)
Answer:Yes we can modify"""
# input_tuple =(1, 2, 3.5,"Meera", [16, 17, 23], 45, 67)
# input_tuple[4][1]=18 # Tuple elements we can't modify. Here I have modified list elements which are placed in Tuple.
# print(input_tuple)
""" Can you modify Tuple elements which are placed in List ?
a=[1, 2, 3, (16,17,23), 45, 67]
Answer: No we can't modify"""
# input_list = [1, 2, 3, (16, 17, 23), 45, 67]
# input_list[3][3] = 25
# print(input_list) # TypeError: 'tuple' object does not support item assignment
# List elements we can modify. Here I have tried to modify Tuple elements which are placed in List (It will not happen).
"""Question : a = [[1], [1]]
# Required Output : [[1, 1], [1, 1]]"""
a = [[1], [1]]
a[0] = [1,1]
a[1] = [1,1]
# print(a)
b = ('Iam', 'taking', 'guidance', 'from', 'Narendra', 'in', 'understanding', 'Python')
# print(max(b))
# print(min(b))
# #Returns item from the list, with min value.
#Realtime examples
# List of students with their grades
students = [
("Alice", 85),
("Bob", 92),
("Charlie", 78),
("David", 90)
]
# print("First stundent name, grade",students[0][0], students[0][1])
# List of employees with their details
employees = [
(101, "John Doe", "Software Engineer", 75000),
(102, "Jane Smith", "Project Manager", 90000),
(103, "Sam Brown", "Designer", 65000),
(104, "Emily Davis", "Data Scientist", 85000)
]
# List of contacts
contacts = [
("John Doe", "john@example.com", "555-1234"),
("Jane Smith", "jane@example.com", "555-5678"),
("Sam Brown", "sam@example.com", "555-8765"),
("Emily Davis", "emily@example.com", "555-4321")
]
# List of products in inventory
inventory = [
("Laptop", 15, 800.00),
("Mouse", 50, 20.00),
("Keyboard", 30, 45.00),
("Monitor", 10, 150.00)
]
# List of trips
trips = [
("New York", "2023-07-10", "2023-07-15"),
("Paris", "2023-08-01", "2023-08-10"),
("Tokyo", "2023-09-05", "2023-09-15")
]
# List of events with their scheduled weekdays
event_schedule = [
("Meeting with Team", "Monday"),
("Project Presentation", "Wednesday"),
("Client Call", "Friday"),
("Code Review", "Tuesday"),
("Weekly Sync", "Thursday")
]
# List of locations with their coordinates and weather conditions
weather_data = [
(("New York", "USA"), (40.7128, -74.0060), "Sunny"),
(("Los Angeles", "USA"), (34.0522, -118.2437), "Cloudy"),
(("Toronto", "Canada"), (43.651070, -79.347015), "Snowy"),
(("London", "UK"), (51.5074, -0.1278), "Rainy"),
(("Tokyo", "Japan"), (35.6895, 139.6917), "Clear")
]
weather_data[2][0] == "New york"
# print("New york data", weather_data[0][0], weather_data[0][2])Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
No comments:
Post a Comment