Computer Science, asked by harsh888879, 5 months ago

Which of the following statements will create a tuple ?

(a) Tp1 = (“a”, “b”)

(b) Tp1= (3) * 3

(c) Tp1[2] = (“a”, “b”)

(d) None of these​

Answers

Answered by challasrijha
11

Answer:

a is the correct answer

please follow me friends and like share friends

Answered by ravilaccs
2

Answer:

The correct option is option A

Explanation:

  • A tuple is an ordered sequence of items of various data kinds, such as integers, floats, strings, lists, and even tuples. A tuple's elements are surrounded in parentheses (round brackets) and separated by commas. T1 = (1, 2, 'a', 5) is an example. # T1 is a pair
  • In Python, a tuple can contain both integers and strings as its elements
  • Option A is correct
  • Because in Python, Tuple is represented in round brackets. So, all the other options are incorrect.
  • To create a tuple in Python, place all the elements in a () parenthesis, separated by commas. A tuple can have heterogeneous data items, a tuple can have string and list as data items as well.

Example – Creating tuple

In this example, we are creating few tuples. We can have tuple of same type of data items as well as mixed type of data items. This example also shows nested tuple (tuples as data items in another tuple).

# tuple of strings

my_data = ("hi", "hello", "bye")

print(my_data)

# tuple of int, float, string

my_data2 = (1, 2.8, "Hello World")

print(my_data2)

# tuple of string and list

my_data3 = ("Book", [1, 2, 3])

print(my_data3)

# tuples inside another tuple

# nested tuple

my_data4 = ((2, 3, 4), (1, 2, "hi"))

print(my_data4)

Output:

('hi', 'hello', 'bye')

(1, 2.8, 'Hello World')

('Book', [1, 2, 3])

((2, 3, 4), (1, 2, 'hi'))

Empty tuple:

# empty tuple

my_data = ()

Tuple with only single element:

  • Note: When a tuple has only one element, we must put a comma after the element, otherwise Python will not treat it as a tuple.
  • # a tuple with single data item
  • my_data = (99,)
  • If we do not put comma after 99 in the above example then python will treat my_data as an int variable rather than a tuple.

How to access tuple elements

We use indexes to access the elements of a tuple. Lets take few example to understand the working.

Accessing tuple elements using positive indexes

  • We can also have negative indexes in tuple, we have discussed that in the next section. Indexes starts with 0 that is why we use 0 to access the first element of tuple, 1 to access second element and so on.

# tuple of strings

my_data = ("hi", "hello", "bye")

# displaying all elements

print(my_data)

# accessing first element

# prints "hi"

print(my_data[0])

# accessing third element

# prints "bye"

print(my_data[2])

Output:

('hi', 'hello', 'bye')

hi

bye

Note:

1. TypeError: If you do not use integer indexes in the tuple. For example my_data[2.0] will raise this error. The index must always be an integer.

2. IndexError: Index out of range. This error occurs when we mention the index which is not in the range. For example, if a tuple has 5 elements and we try to access the 7th element then this error would occurr.

Negative indexes in tuples

  • Similar to list and strings we can use negative indexes to access the tuple elements from the end.
  • -1 to access last element, -2 to access second last and so on.

my_data = (1, 2, "Kevin", 8.9)

# accessing last element

# prints 8.9

print(my_data[-1])

# prints 2

print(my_data[-3])

Output:

8.9

2

Accessing elements from nested tuples

  • Lets understand how the double indexes are used to access the elements of nested tuple. The first index represents the element of main tuple and the second index represent the element of the nested tuple.
  • In the following example, when I used my_data[2][1], it accessed the second element of the nested tuple. Because 2 represented the third element of main tuple which is a tuple and the 1 represented the second element of that tuple.

my_data = (1, "Steve", (11, 22, 33))

# prints 'v'

print(my_data[1][3])

# prints 22

print(my_data[2][1])

Output:

v

22

Reference Link

  • https://brainly.in/question/32754554
  • https://brainly.in/question/47812271
Similar questions