KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 4.3

Tuples in Python

Class 10 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

What does the following image depict?

What does the following image depict. Tuples in Python, APC ICSE Robotics & Artificial Intelligence Solutions Class 10.
  1. List representation
  2. Items representation
  3. Tuple representation
  4. 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.

Question 2

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

  1. A tuple of integers
  2. A tuple of strings
  3. A tuple of symbols
  4. 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.

Question 3

A tuple is defined as:

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

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

  1. T[2:4]
  2. T[1:4]
  3. T[:3]
  4. 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).

Question 4

Which of the following represents a tuple?

  1. {1, 2, 3}
  2. (1, 2, 3)
  3. [1, 2, 3]
  4. ["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.

Question 5

What will the following relation result in?

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

  1. False
  2. Error
  3. True
  4. 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.

Question 6

Which of the following operators will concatenate two tuples?

  1. +
  2. *
  3. add
  4. join

Answer

+

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

State True or False

Question 1

State whether the following statements are True or False:

  1. Tuple is an ordered sequence of immutable data type of Python.
  2. An empty tuple is a tuple which contains at least one element.
  3. The tuple( ) function is also used to convert a complex number into a tuple.
  4. The '*' operator used with a tuple and an integer to give a replicated tuple.
  5. The purpose of index( ) function in tuple is to return last element.
  6. The sum( ) function is used to find the arithmetic sum of all integer values stored in a tuple.

Answer

  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

Give One Word Answer

Question 1

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

  1. Finding the arithmetical sum of the elements in a tuple
  2. Joining two or more tuples together
  3. Duplicating the elements of a tuple
  4. A value representing position of an element
  5. Arranging the elements of a tuple in a specific order

Answer

  1. sum( )
  2. Concatenation
  3. Replication
  4. Index
  5. sorted( )

Assertion and Reason Based Question

Question 1

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:

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true and R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
  5. 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.

Application Based Question

Question 1(a)

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.

Output
SyntaxError: invalid syntax
Explanation

The del keyword is used to delete a tuple, but it is a statement and cannot be used inside the print( ) function.

Question 1(b)

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)

Output
False
Explanation

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)

Output
True
Explanation

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)

Output
False
Explanation

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.

Programs on Tuples

Question 1

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

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)
Output
Enter integers in a tuple within ( ): (15, 23, 45, 60, 75, 82, 95)
Sum of the numbers ending with digit 5: 230

Question 2

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

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)
Output
Enter integers in a tuple within ( ): (12, -5, 8, -3, 15, -10, 6)
Sum of positive numbers: 41
Sum of negative numbers: -18

Question 3

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

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")
Output
Enter 10 integers in a tuple within ( ): (12, 7, 15, 8, 21, 10, 5, 18, 9, 14)
Minimum of odd numbers: 5

Question 4

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.

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)
Output
Enter a set of numbers in a tuple within ( ): (16, 13, 12, 17, 14)
Sorted numbers in descending order: [17, 16, 14, 13, 12]

Question 5

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

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)
Output
Enter integers in a tuple within ( ): (12, 7, 15, 11, 9, 5, 8)
Prime numbers are:
7
11
5

Question 6

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.

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)
Output
Enter integers in a tuple within ( ): (6, 10, 28, 12, 15)
Perfect numbers are:
6
28

Answer the Following Questions

Question 1

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)

Question 2

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

Question 3

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.

Question 4

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.

Question 5

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)

Question 6

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)

Question 7

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.

Question 8

Differentiate between a list and a tuple.

Answer

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.

Question 9

What is the purpose of count( ) function?

Answer

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

Question 10(a)

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

Question 10(b)

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

Question 10(c)

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

Question 10(d)

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]
PrevNext