Multiplication table function

Multiplication table function

EasyFunction Programs · 3 of 39

Write a Python function that prints the multiplication table of a number.

Input
9
Output
9 x  1 = 9
9 x  2 = 18
9 x  3 = 27
9 x  4 = 36
9 x  5 = 45
9 x  6 = 54
9 x  7 = 63
9 x  8 = 72
9 x  9 = 81
9 x 10 = 90

Approach

  1. Loop from 1 to 10 and print each line.

Solution

Python
def table(n, upto=10):    for i in range(1, upto + 1):        print(f"{n} x {i:2} = {n * i}") table(9)
Output
9 x  1 = 9
9 x  2 = 18
9 x  3 = 27
9 x  4 = 36
9 x  5 = 45
9 x  6 = 54
9 x  7 = 63
9 x  8 = 72
9 x  9 = 81
9 x 10 = 90

Learn the concept

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

More function programs

← All 39 Function Programs

Print a string 10 times

Print a string 10 times

EasyFunction Programs · 2 of 39

Write a Python function that prints a string 10 times.

Input
"sqa"
Output
sqa sqa sqa sqa sqa sqa sqa sqa sqa sqa 
hi hi hi 

Approach

  1. Loop 10 times inside the function; make the count a default parameter.

Solution

Python
def repeat(text, times=10):    for _ in range(times):        print(text, end=" ")    print() repeat("sqa")repeat("hi", 3)
Output
sqa sqa sqa sqa sqa sqa sqa sqa sqa sqa 
hi hi hi 

Line by line

Line 1

times=10 is a default value, used when the caller leaves it out.

Learn the concept

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

More function programs

← All 39 Function Programs

Function to add two numbers

Function to add two numbers

EasyFunction Programs · 1 of 39

Write a Python function that adds two numbers and returns the result.

Input
12, 30
Output
42
3.0

Approach

  1. Define add(a, b) that returns a + b.

Solution

Python
def add(a, b):    return a + b print(add(12, 30))print(add(2.5, 0.5))
Output
42
3.0

Line by line

Line 1

a and b are parameters; their values come from the call.

Line 2

return hands the result back so the caller can use it.

Learn the concept

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

More function programs

← All 39 Function Programs

Last element with negative indexing

Python Practice › Tuple Programs › Program 70

Last element with negative indexing

EasyTuple Programs · 70 of 70

Write a Python program to get the last element of a tuple with negative indexing.

Input
("p", "y", "t", "o", "n")
Output
n

Approach

  1. t[-1].

Solution

Python
print(("p", "y", "t", "o", "n")[-1])
Output
n

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Is the value a tuple?

Python Practice › Tuple Programs › Program 69

Is the value a tuple?

EasyTuple Programs · 69 of 70

Write a Python program that returns True if a value is a tuple.

Input
(7, 4, 9, 2, 0), [7, 4]
Output
(7, 4, 9, 2, 0) True
[7, 4] False

Approach

  1. isinstance(x, tuple).

Solution

Python
for value in [(7, 4, 9, 2, 0), [7, 4]]:    print(value, isinstance(value, tuple))
Output
(7, 4, 9, 2, 0) True
[7, 4] False

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Swap two tuples

Python Practice › Tuple Programs › Program 68

Swap two tuples

EasyTuple Programs · 68 of 70

Write a Python program to swap two tuple variables.

Input
A = (7, 4, 9), B = (3,)
Output
A = (3,)
B = (7, 4, 9)

Approach

  1. Tuple unpacking swaps the names.

Solution

Python
A, B = (7, 4, 9), (3,)A, B = B, Aprint("A =", A)print("B =", B)
Output
A = (3,)
B = (7, 4, 9)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Average of a tuple

Python Practice › Tuple Programs › Program 67

Average of a tuple

EasyTuple Programs · 67 of 70

Write a Python program to calculate the average of a tuple’s elements.

Input
(5, 3, 9, 6)
Output
5.75

Approach

  1. sum / len.

Solution

Python
t = (5, 3, 9, 6)print(sum(t) / len(t))
Output
5.75

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Binary tuple to an integer

Python Practice › Tuple Programs › Program 65

Binary tuple to an integer

EasyTuple Programs · 65 of 70

Write a Python program to convert a tuple of binary digits into an integer.

Input
(1, 0, 0)
Output
4

Approach

  1. Join the digits and use int(text, 2).

Solution

Python
bits = (1, 0, 0)print(int("".join(map(str, bits)), 2))
Output
4

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Count unique tuples

Python Practice › Tuple Programs › Program 66

Count unique tuples

EasyTuple Programs · 66 of 70

Write a Python program to count the distinct tuples in a list.

Input
[(8, 9), (4, 7), (3, 6), (8, 9)]
Output
3

Approach

  1. len(set(data)).

Solution

Python
print(len(set([(8, 9), (4, 7), (3, 6), (8, 9)])))
Output
3

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Common tuples ignoring order

Python Practice › Tuple Programs › Program 64

Common tuples ignoring order

MediumTuple Programs · 64 of 70

Write a Python program to find the tuples that appear in both lists, treating (3, 9) and (9, 3) as the same.

Input
A = [(1, 5), (4, 8), (3, 9)], B = [(9, 3), (5, 6), (5, 1), (0, 4)]
Output
{(3, 9), (1, 5)}

Approach

  1. Compare sorted versions of each tuple.

Solution

Python
A = [(1, 5), (4, 8), (3, 9)]B = [(9, 3), (5, 6), (5, 1), (0, 4)]norm_b = {tuple(sorted(t)) for t in B}print({t for t in A if tuple(sorted(t)) in norm_b})
Output
{(3, 9), (1, 5)}

Line by line

Line 3

Sorting each tuple makes (9, 3) and (3, 9) identical.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs