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

Tuple of pairs from two lists

Tuple of pairs from two lists

EasyTuple Programs · 1 of 70

Write a Python program to create a tuple of pairs from two lists.

Input
[4, 6, 8], [7, 1, 4]
Output
((4, 7), (6, 1), (8, 4))

Approach

  1. zip pairs items at the same position; tuple() collects the pairs.

Solution

Python
list1 = [4, 6, 8]list2 = [7, 1, 4]print(tuple(zip(list1, list2)))
Output
((4, 7), (6, 1), (8, 4))

Line by line

Line 3

zip yields (4, 7), (6, 1), (8, 4); tuple() stores them in a tuple.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Import an Excel file

Python Practice › Pandas Programs › Program 95

Import an Excel file

EasyPandas Programs · 95 of 95

Write a pandas program to import an .xlsx file.

Output
   name  marks
0  Asha     91
1  Ravi     78

Approach

  1. pd.read_excel (needs openpyxl installed).

Solution

Python
import pandas as pdpd.DataFrame({"name": ["Asha", "Ravi"], "marks": [91, 78]}).to_excel("marks.xlsx", index=False)df = pd.read_excel("marks.xlsx")print(df)
Output
   name  marks
0  Asha     91
1  Ravi     78
Note

Install openpyxl first with python -m pip install openpyxl. The browser Try it does not include it.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs