Prime check function

Prime check function

EasyFunction Programs · 10 of 39

Write a Python function that returns True if a number is prime.

Input
7, 12, 1
Output
[(7, True), (12, False), (1, False)]

Approach

  1. Test divisors up to the square root.

Solution

Python
def is_prime(n):    if n < 2:        return False    for d in range(2, int(n ** 0.5) + 1):        if n % d == 0:            return False    return True print([(n, is_prime(n)) for n in [7, 12, 1]])
Output
[(7, True), (12, False), (1, False)]

Learn the concept

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

More function programs

← All 39 Function Programs

Unique elements in order

Unique elements in order

EasyFunction Programs · 9 of 39

Write a Python function that returns a new list with the unique elements of a list, in order.

Input
[2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
Output
[2, 3, 1, 4, 6]

Approach

  1. Append items not yet seen.

Solution

Python
def unique(items):    result = []    for x in items:        if x not in result:            result.append(x)    return result print(unique([2, 2, 3, 1, 4, 4, 4, 4, 4, 6]))
Output
[2, 3, 1, 4, 6]

Learn the concept

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

More function programs

← All 39 Function Programs

Is a number in a range?

Is a number in a range?

EasyFunction Programs · 8 of 39

Write a Python function to check whether a number is within a range (inclusive).

Input
7 in 2 to 20; 25 in 2 to 20
Output
7 is in the range
25 is outside the range

Approach

  1. A chained comparison.

Solution

Python
def in_range(n, low, high):    return low <= n <= high for n in [7, 25]:    print(n, "is in the range" if in_range(n, 2, 20) else "is outside the range")
Output
7 is in the range
25 is outside the range

Learn the concept

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

More function programs

← All 39 Function Programs

Reverse a string

Reverse a string

EasyFunction Programs · 7 of 39

Write a Python function to reverse a string.

Input
"Python1234"
Output
4321nohtyP

Approach

  1. Return text[::-1].

Solution

Python
def reverse(text):    return text[::-1] print(reverse("Python1234"))
Output
4321nohtyP

Learn the concept

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

More function programs

← All 39 Function Programs

Product of a list

Product of a list

EasyFunction Programs · 6 of 39

Write a Python function to multiply all the numbers in a list.

Input
[-8, 6, 1, 9, 2]
Output
-864

Approach

  1. Start at 1 and multiply.

Solution

Python
def product(numbers):    result = 1    for n in numbers:        result *= n    return result print(product([-8, 6, 1, 9, 2]))
Output
-864

Learn the concept

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

More function programs

← All 39 Function Programs

Maximum of three numbers

Maximum of three numbers

EasyFunction Programs · 4 of 39

Write a Python function to find the maximum of three numbers.

Input
17, 21, -9
Output
21

Approach

  1. Reuse a two-number max function twice.

Solution

Python
def max_of_two(a, b):    return a if a > b else b def max_of_three(a, b, c):    return max_of_two(a, max_of_two(b, c)) print(max_of_three(17, 21, -9))
Output
21

Line by line

Lines 4-5

Building on a smaller function keeps each one simple.

Learn the concept

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

More function programs

← All 39 Function Programs

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

Python Function Programs with Solutions

Python Practice › Function Programs

Python Function Programs with Solutions

39 programs35 easy4 medium0 hard

39 function programs that practise defining functions, parameters, return values, *args, nested functions and small menu-driven systems. Each solution is written as a reusable function and tested with several inputs.

Before you start

Read the Functions and Lambda and Scope lesson first if the topic is new to you.

All programs