Is an element in a tuple?

Python Practice › Tuple Programs › Program 11

Is an element in a tuple?

EasyTuple Programs · 11 of 70

Write a Python program to check whether an element exists in a tuple.

Input
("p", "y", "t", "h", "o", "n"), check "p" and "z"
Output
True False

Approach

  1. in.

Solution

Python
t = ("p", "y", "t", "h", "o", "n")print("p" in t, "z" in t)
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

2nd from the front, 3rd from the back

Python Practice › Tuple Programs › Program 10

2nd from the front, 3rd from the back

EasyTuple Programs · 10 of 70

Write a Python program to get the 2nd element from the front and the 3rd element from the back of a tuple.

Input
("s", "q", "a", "t", "o", "o", "l", "s")
Output
q
o

Approach

  1. Index 1 from the front, index -3 from the back.

Solution

Python
t = ("s", "q", "a", "t", "o", "o", "l", "s")print(t[1])print(t[-3])
Output
q
o

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Tuple of characters to a string

Tuple of characters to a string

EasyTuple Programs · 9 of 70

Write a Python program to join a tuple of characters into a string.

Input
("s", "q", "a", "t", "o", "o", "l", "s")
Output
Sqatools

Approach

  1. "".join(t).

Solution

Python
t = ("s", "q", "a", "t", "o", "o", "l", "s")print("".join(t).capitalize())
Output
Sqatools

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Add an item to a tuple

Add an item to a tuple

EasyTuple Programs · 8 of 70

Write a Python program to add an item to a tuple.

Input
(18, 65, 3, 45), item 15
Output
(18, 65, 3, 45, 15)

Approach

  1. Tuples cannot change, so create a new tuple with +.

Solution

Python
t = (18, 65, 3, 45)t = t + (15,)print(t)
Output
(18, 65, 3, 45, 15)

Line by line

Line 2

(15,) is a one-item tuple; the comma is required.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Unpack a tuple into variables

Unpack a tuple into variables

EasyTuple Programs · 7 of 70

Write a Python program to assign the values of a tuple to several variables and print them.

Input
(6, 7, 3)
Output
a, 6
b, 7
c, 3

Approach

  1. Tuple unpacking: one name per item.

Solution

Python
a, b, c = (6, 7, 3)print("a,", a)print("b,", b)print("c,", c)
Output
a, 6
b, 7
c, 3

Line by line

Line 1

The number of names must match the number of items.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Element by index

Element by index

EasyTuple Programs · 6 of 70

Write a Python program to get an element of a tuple by its index.

Input
(4, 8, 9, 1), index 2
Output
9

Approach

  1. Square brackets with the index.

Solution

Python
t = (4, 8, 9, 1)print(t[2])
Output
9

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Tuple with different data types

Tuple with different data types

EasyTuple Programs · 5 of 70

Write a Python program to create a tuple containing values of different data types.

Output
(2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
['float', 'int', 'str', 'bool', 'list', 'tuple', 'dict']

Approach

  1. A tuple can hold any objects, including lists, tuples and dicts.

Solution

Python
mixed = (2.6, 1, "Python", True, [5, 6, 7], (5, 1, 4), {"a": 123, "b": 456})print(mixed)print([type(x).__name__ for x in mixed])
Output
(2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
['float', 'int', 'str', 'bool', 'list', 'tuple', 'dict']

Line by line

Line 3

Listing the type names shows every item keeps its own type.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

List of (number, square) tuples

List of (number, square) tuples

EasyTuple Programs · 4 of 70

Write a Python program to create a list of tuples, each holding a number and its square.

Input
[4, 6, 3, 8]
Output
[(4, 16), (6, 36), (3, 9), (8, 64)]

Approach

  1. A comprehension builds (n, n * n) for each number.

Solution

Python
numbers = [4, 6, 3, 8]print([(n, n * n) for n in numbers])
Output
[(4, 16), (6, 36), (3, 9), (8, 64)]
Note

The square of 3 is 9; the original sample showed 27, which is 3 cubed.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Minimum value in a tuple

Minimum value in a tuple

EasyTuple Programs · 3 of 70

Write a Python program to find the minimum value in a tuple.

Input
(36, 5, 79, 25)
Output
5

Approach

  1. min().

Solution

Python
print(min((36, 5, 79, 25)))
Output
5

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Maximum value in a tuple

Maximum value in a tuple

EasyTuple Programs · 2 of 70

Write a Python program to find the maximum value in a tuple.

Input
(41, 15, 69, 55)
Output
69

Approach

  1. max() works on any iterable.

Solution

Python
print(max((41, 15, 69, 55)))
Output
69

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs