Tuples in Python

ICSE Class 10 Robotics & AI · Exploring Robotics & AI · 30 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

What does the following image depict?

What does the following image depict. Tuples in Python, APC ICSE Robotics & Artificial Intelligence Solutions Class 10.
Show SolutionHide Solution↓

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.

21 Mark · Multiple Choice Questions

Which of the following is returned by tuple() function?

Show SolutionHide Solution↓

An empty tuple

Reason — The tuple() function without any argument returns an empty tuple, meaning a tuple that contains no elements.

31 Mark · Multiple Choice Questions

A tuple is defined as:

T = (1, 2, 3, 4, 5, 6)

Which of the following tuple slice will produce (1, 2, 3)?

Show SolutionHide Solution↓

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).

41 Mark · Multiple Choice Questions

Which of the following represents a tuple?

Show SolutionHide Solution↓

(1, 2, 3)

Reason — A tuple is represented by elements enclosed within parentheses ( ) and separated by commas. Therefore, (1, 2, 3) represents a tuple.

51 Mark · Multiple Choice Questions

What will the following relation result in?

(1, 2, 3) < (1, 2, 8, 9)

Show SolutionHide Solution↓

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.

61 Mark · Multiple Choice Questions

Which of the following operators will concatenate two tuples?

Show SolutionHide Solution↓

+

Reason — The + operator is used to concatenate two or more tuples to form a single tuple.

1State True or False

State whether the following statements are True or False:

Show SolutionHide Solution↓
  1. True
  2. False
    Corrected Statement: An empty tuple is a tuple which contains no element.
  3. False
    Corrected Statement: The tuple() function is used to convert iterable objects into a tuple, not a complex number.
  4. True
  5. False
    Corrected Statement: The purpose of index( ) function in tuple is to return the position of the first occurrence of a specified value.
  6. True
1Give One Word Answer

Give one word answer to perform the following tasks in tuple.

Show SolutionHide Solution↓
  1. sum( )
  2. Concatenation
  3. Replication
  4. Index
  5. sorted( )
1Assertion and Reason Based Question

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:

Show SolutionHide Solution↓

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.

1(a)Application Based Question

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.

Show SolutionHide Solution↓

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.

1(b)Application Based Question

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))

Show SolutionHide Solution↓

(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.

1Programs on Tuples

Write a Python program to accept integers in a tuple. Find and display the sum of the numbers ending with digit 5.

Show SolutionHide Solution↓
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
2Programs on Tuples

Write a Python program to accept integers (negative as well as positive) in a tuple. Display the sum of negative and positive numbers separately.

Show SolutionHide Solution↓
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
3Programs on Tuples

Write a program to accept 10 integers in a tuple. Find and display the minimum of odd numbers.

Show SolutionHide Solution↓
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
4Programs on Tuples

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.

Show SolutionHide Solution↓
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]
5Programs on Tuples

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).

Show SolutionHide Solution↓
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
6Programs on Tuples

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.

Show SolutionHide Solution↓
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
1Answer the Following Questions

Define a tuple with an example.

Show SolutionHide Solution↓

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)

2Answer the Following Questions

What is meant by a null or empty tuple?

Show SolutionHide Solution↓

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 ( ).

3Answer the Following Questions

Can a tuple element be changed? Justify your answer.

Show SolutionHide Solution↓

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.

4Answer the Following Questions

What is meant by slicing a tuple?

Show SolutionHide Solution↓

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.

5Answer the Following Questions

What is meant by traversing a tuple? Give an example.

Show SolutionHide Solution↓

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)
6Answer the Following Questions

Define replication of the tuple elements. Given an example.

Show SolutionHide Solution↓

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)
7Answer the Following Questions

Name two operators that resolve searching an element in a tuple.

Show SolutionHide Solution↓

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.

8Answer the Following Questions

Differentiate between a list and a tuple.

Show SolutionHide Solution↓

Differences between a list and a tuple are:

ListTuple
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.
9Answer the Following Questions

What is the purpose of count( ) function?

Show SolutionHide Solution↓

The count( ) function is responsible to find the frequency of an element in a tuple.

10(a)Answer the Following Questions

Write short notes on len( ).

Show SolutionHide Solution↓

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
10(b)Answer the Following Questions

Write short notes on index( ).

Show SolutionHide Solution↓

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
10(c)Answer the Following Questions

Write short notes on sum( ).

Show SolutionHide Solution↓

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
10(d)Answer the Following Questions

Write short notes on sorted( ).

Show SolutionHide Solution↓

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]

Press ↓ to reveal a solution and move to the next question. Back to Exploring Robotics & AI.

Free sample bank