What does the following image depict?

- List representation
- Items representation
- Tuple representation
- None of these
Answer
Tuple representation
Reason — According to the image shown above, the elements are enclosed within parentheses () and separated by commas, which represents a tuple in Python.
Which of the following is returned by tuple() function?
- A tuple of integers
- A tuple of strings
- A tuple of symbols
- An empty tuple
Answer
An empty tuple
Reason — The tuple() function without any argument returns an empty tuple, meaning a tuple that contains no elements.
A tuple is defined as:
T = (1, 2, 3, 4, 5, 6)
Which of the following tuple slice will produce (1, 2, 3)?
- T[2:4]
- T[1:4]
- T[:3]
- T[3:4]
Answer
T[:3]
Reason — In tuple slicing, the elements are extracted from the starting index up to (final index − 1). Since indexing starts from 0, T[:3] means elements from index 0 to 2, which are (1, 2, 3).
Which of the following represents a tuple?
- {1, 2, 3}
- (1, 2, 3)
- [1, 2, 3]
- ["1, 2, 3"]
Answer
(1, 2, 3)
Reason — A tuple is represented by elements enclosed within parentheses ( ) and separated by commas. Therefore, (1, 2, 3) represents a tuple.
What will the following relation result in?
(1, 2, 3) < (1, 2, 8, 9)
- False
- Error
- True
- None of these
Answer
True
Reason — In tuple comparison, corresponding elements are compared starting from index 0. Since 1 = 1 and 2 = 2, the comparison moves to the next element where 3 < 8, which is True. Therefore, the entire relation evaluates to True.
Which of the following operators will concatenate two tuples?
- +
- *
- add
- join
Answer
+
Reason — The + operator is used to concatenate two or more tuples to form a single tuple.
State whether the following statements are True or False:
- Tuple is an ordered sequence of immutable data type of Python.
- An empty tuple is a tuple which contains at least one element.
- The tuple( ) function is also used to convert a complex number into a tuple.
- The '*' operator used with a tuple and an integer to give a replicated tuple.
- The purpose of index( ) function in tuple is to return last element.
- The sum( ) function is used to find the arithmetic sum of all integer values stored in a tuple.
Answer
- True
- False
Corrected Statement: An empty tuple is a tuple which contains no element. - False
Corrected Statement: The tuple() function is used to convert iterable objects into a tuple, not a complex number. - True
- False
Corrected Statement: The purpose of index( ) function in tuple is to return the position of the first occurrence of a specified value. - True
Give one word answer to perform the following tasks in tuple.
- Finding the arithmetical sum of the elements in a tuple
- Joining two or more tuples together
- Duplicating the elements of a tuple
- A value representing position of an element
- Arranging the elements of a tuple in a specific order
Answer
- sum( )
- Concatenation
- Replication
- Index
- sorted( )
Assertion (A): We can create a tuple from non-tuple structures such as lists and strings.
Reason (R): This task can be performed using tuple( ) function. When a string is converted to a tuple, each character of the string becomes a separate element of the tuple.
Based on the above assertion and reasoning, pick an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.
Answer
Both A and R are true and R is the correct explanation of A.
Reason — A tuple can be created from non-tuple structures such as lists and strings using the tuple( ) function. When a string is converted to a tuple, each character of the string becomes a separate element of the tuple.
You have performed some tasks in tuple which are given below:
T1 = (23, 34, 51, 62, 45)
print(del T1)Predict the expected output when the above tasks is executed.
Answer
The given code will result in a syntax error.
SyntaxError: invalid syntax
The del keyword is used to delete a tuple, but it is a statement and cannot be used inside the print( ) function.
There are some tasks performed on conditional operators in tuples. Predict the output when the following tasks are executed.
(i) print((12, 4, 6, 8) < (5, 9))
(ii) print(('a', 'e', 'i', 'o', 'u') != ('A', 'E', 'I', 'O', 'U'))
(iii) print((1, 2, 8, 5) > (1, 2, 13, 5))
Answer
(i)
False
In tuple comparison, corresponding elements are compared from the 0th index. Since 12 is greater than 5, the condition (12, 4, 6, 8) < (5, 9) evaluates to False.
(ii)
True
The corresponding elements of both tuples are compared. Since 'a' and 'A' are different, the two tuples are not equal. Therefore, the condition using != evaluates to True.
(iii)
False
In tuple comparison, corresponding elements are compared starting from index 0. First, 1 = 1 and 2 = 2, so comparison proceeds to the next elements. At index 2, 8 is less than 13, therefore, (1, 2, 8, 5) is not greater than (1, 2, 13, 5), and the result is False.
Write a Python program to accept integers in a tuple. Find and display the sum of the numbers ending with digit 5.
T1 = eval(input("Enter integers in a tuple within ( ): "))
s = 0
for x in T1:
if x % 10 == 5:
s = s + x
print("Sum of the numbers ending with digit 5:", s)Enter integers in a tuple within ( ): (15, 23, 45, 60, 75, 82, 95)
Sum of the numbers ending with digit 5: 230
Write a Python program to accept integers (negative as well as positive) in a tuple. Display the sum of negative and positive numbers separately.
T1 = eval(input("Enter integers in a tuple within ( ): "))
pos = 0
neg = 0
for x in T1:
if x > 0:
pos = pos + x
elif x < 0:
neg = neg + x
print("Sum of positive numbers:", pos)
print("Sum of negative numbers:", neg)Enter integers in a tuple within ( ): (12, -5, 8, -3, 15, -10, 6)
Sum of positive numbers: 41
Sum of negative numbers: -18
Write a program to accept 10 integers in a tuple. Find and display the minimum of odd numbers.
T1 = eval(input("Enter 10 integers in a tuple within ( ): "))
found = False
mn = 0
for x in T1:
if x % 2 != 0:
if found == False:
mn = x
found = True
elif x < mn:
mn = x
if found == True:
print("Minimum of odd numbers:", mn)
else:
print("No odd number found")Enter 10 integers in a tuple within ( ): (12, 7, 15, 8, 21, 10, 5, 18, 9, 14)
Minimum of odd numbers: 5
Write a program in Python to accept a set of numbers in a tuple. Sort the numbers in descending order. Finally, display the sorted numbers. Make use of sorted( ) function.
T1 = eval(input("Enter a set of numbers in a tuple within ( ): "))
s = sorted(T1, reverse = True)
print("Sorted numbers in descending order:", s)Enter a set of numbers in a tuple within ( ): (16, 13, 12, 17, 14)
Sorted numbers in descending order: [17, 16, 14, 13, 12]
Write a program in Python to accept a set of integers in a tuple. Find and display the numbers that are prime numbers. A number is said to be a prime number, if it has only two factors (i.e.,1 and the number itself).
T1 = eval(input("Enter integers in a tuple within ( ): "))
print("Prime numbers are:")
for a in T1:
c = 0
for b in range(1, a+1):
if(a % b == 0):
c = c + 1
if(c == 2):
print(a)Enter integers in a tuple within ( ): (12, 7, 15, 11, 9, 5, 8)
Prime numbers are:
7
11
5
A number is said to be Perfect, if the sum of all its factors including 1 (one) and excluding the number itself, is same as the original number.
For example, 6 is a perfect number.
Factors of 6 = 1, 2, 3 and 1 + 2 + 3 = 6
Write a Python program to accept a set of integers in a tuple. Display only Perfect numbers available in the tuple.
T1 = eval(input("Enter integers in a tuple within ( ): "))
print("Perfect numbers are:")
for a in T1:
s = 0
if a > 1:
for b in range(1, a):
if(a % b == 0):
s = s + b
if(s == a):
print(a)Enter integers in a tuple within ( ): (6, 10, 28, 12, 15)
Perfect numbers are:
6
28
Define a tuple with an example.
Answer
Tuple is an ordered sequence of data. The elements of a tuple are immutable. It means, any change made in the elements of a tuple cannot be updated in its place.
Example: T = (1, 2, 3, 4)
What is meant by a null or empty tuple?
Answer
A tuple that does not contain any element as its member, is known as an empty tuple. An empty tuple is represented by empty braces ( ).
Can a tuple element be changed? Justify your answer.
Answer
No, a tuple element cannot be changed because the elements of a tuple are immutable. It means that any change made in the elements of a tuple cannot be updated in its place.
What is meant by slicing a tuple?
Answer
Tuple slicing is a technique to create a tuple after ignoring some elements of the existing tuple. It can be done by slicing a tuple from consecutive indexes.
What is meant by traversing a tuple? Give an example.
Answer
Traversing a tuple means accessing the elements of a tuple consecutively one after the other to perform any operation. It can be done with the help of a for loop.
Example:
T1 = (2, 4, 6, 8)
for x in T1:
print(x)Define replication of the tuple elements. Given an example.
Answer
Replicating a tuple means duplicating the elements within a tuple, i.e., appending the tuple with the elements of the same tuple. It is done with the help of * operator.
Example:
T1 = (1, 3, 5)
print(T1 * 2)Output:
(1, 3, 5, 1, 3, 5)
Name two operators that resolve searching an element in a tuple.
Answer
The two operators that resolve searching an element in a tuple are in and not in. These operators are used to check whether an element is present or not present in a tuple.
Differentiate between a list and a tuple.
Answer
Differences between a list and a tuple are:
| List | Tuple |
|---|---|
The elements are enclosed within square brackets [ ] separated by commas. | The elements are enclosed within braces ( ) separated by commas. |
| Elements of a list are mutable. | Elements of a tuple are immutable. |
What is the purpose of count( ) function?
Answer
The count( ) function is responsible to find the frequency of an element in a tuple.
Write short notes on len( ).
Answer
The len( ) function is used to find the length of a tuple, i.e., the number of elements available in the tuple.
Syntax: p = len(<tuple>)
Example:
T1 = (8, 3, 2, 9, 4)
p = len(T1)
print("Number of elements in the tuple:", p)Output:
Number of elements in the tuple: 5
Write short notes on index( ).
Answer
The index( ) function returns the index of the first occurrence of an element in the tuple.
Syntax: <tuple variable>.index(<tuple element>)
Example:
T1 = (12, 14, 16, 18, 20)
print(T1.index(18))Output:
3
Write short notes on sum( ).
Answer
The sum( ) function is used to find the arithmetic sum of all integer values stored in a tuple.
Syntax: sum(Tuple)
Example:
T1 = (13, 14, 12, 15)
s = sum(T1)
print(s)Output:
54
Write short notes on sorted( ).
Answer
The sorted( ) function is used to arrange a set of elements stored in a tuple either in ascending or descending order and returns the elements as a list.
Syntax:
sorted(tuple)
sorted(tuple, reverse = True)
sorted(tuple): It will sort the elements of given tuple in ascending order.sorted(tuple, reverse = True): It will sort a set of tuple data into descending order.
Example:
T1 = (16, 13, 12, 17, 14)
print(sorted(T1))
print(sorted(T1, reverse = True))Output:
[12, 13, 14, 16, 17]
[17, 16, 14, 13, 12]