Fibonacci terms up to n terms

Fibonacci terms up to n terms

EasyFunction Programs · 20 of 39

Write a Python function that returns the first n Fibonacci numbers starting 1, 1.

Input
9
Output
1 1 2 3 5 8 13 21 34

Approach

  1. Build a list in a loop.

Solution

Python
def fibonacci(n):    series = []    a, b = 1, 1    for _ in range(n):        series.append(a)        a, b = b, a + b    return series print(*fibonacci(9))
Output
1 1 2 3 5 8 13 21 34

Learn the concept

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

More function programs

← All 39 Function Programs

Factorial function (recursive)

Factorial function (recursive)

EasyFunction Programs · 19 of 39

Write a Python function to get the factorial of a number.

Input
5
Output
120

Approach

  1. n! = n × (n − 1)!, with 0! = 1.

Solution

Python
def factorial(n):    if n <= 1:        return 1    return n * factorial(n - 1) print(factorial(5))
Output
120

Learn the concept

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

More function programs

← All 39 Function Programs

Function with *args

Function with *args

EasyFunction Programs · 18 of 39

Write a Python function that accepts any number of numbers with *args and prints their cubes.

Input
5, 6, 8, 7
Output
tuple (5, 6, 8, 7)
125 216 512 343

Approach

  1. *args collects the values into a tuple.

Solution

Python
def cubes(*args):    print(type(args).__name__, args)    print(*(n ** 3 for n in args)) cubes(5, 6, 8, 7)
Output
tuple (5, 6, 8, 7)
125 216 512 343

Learn the concept

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

More function programs

← All 39 Function Programs

HCF function

HCF function

EasyFunction Programs · 17 of 39

Write a Python function to find the HCF of two numbers.

Input
24, 54
Output
6

Approach

  1. Euclid’s algorithm.

Solution

Python
def hcf(a, b):    while b:        a, b = b, a % b    return a print(hcf(24, 54))
Output
6

Line by line

Line 3

(24, 54) → (54, 24) → (24, 6) → (6, 0), so the HCF is 6.

Learn the concept

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

More function programs

← All 39 Function Programs

Sum from 0 to n

Sum from 0 to n

EasyFunction Programs · 16 of 39

Write a Python function to calculate the sum of numbers from 0 to 10.

Output
55

Approach

  1. sum(range(n + 1)).

Solution

Python
def sum_to(n=10):    return sum(range(n + 1)) print(sum_to())
Output
55

Learn the concept

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

More function programs

← All 39 Function Programs

LCM function

LCM function

EasyFunction Programs · 15 of 39

Write a Python function to find the LCM of two numbers.

Input
12, 20
Output
60

Approach

  1. LCM = a × b // HCF.

Solution

Python
def hcf(a, b):    while b:        a, b = b, a % b    return a def lcm(a, b):    return a * b // hcf(a, b) print(lcm(12, 20))
Output
60

Learn the concept

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

More function programs

← All 39 Function Programs

Function inside a function

Function inside a function

EasyFunction Programs · 14 of 39

Write a Python program that defines and uses a function inside another function.

Output
Hello, Asha!

Approach

  1. The inner function is only visible inside the outer one; it can read the outer variables.

Solution

Python
def greet(name):    def make_message():        return f"Hello, {name}!"    return make_message() print(greet("Asha"))
Output
Hello, Asha!

Line by line

Lines 2-3

make_message reads name from the enclosing function.

Learn the concept

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

More function programs

← All 39 Function Programs

Run Python code stored in a string

Run Python code stored in a string

MediumFunction Programs · 13 of 39

Write a Python function that executes Python code stored in a string.

Output
inside exec: 81
81

Approach

  1. exec() runs code; give it a separate namespace dictionary so it cannot touch your variables.

Solution

Python
def run_code(source):    namespace = {}    exec(source, namespace)    return {k: v for k, v in namespace.items() if not k.startswith("__")} code = """def square(x):    return x * xresult = square(9)print("inside exec:", result)"""print(run_code(code)["result"])
Output
inside exec: 81
81

Line by line

Lines 2-3

The code runs inside its own dictionary, which then holds the names it created.

Line 4

Python adds __builtins__ to the namespace, so names starting with __ are filtered out.

Watch out

Never pass text from users or files you do not trust to exec(); it can run any code on your machine.

Learn the concept

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

More function programs

← All 39 Function Programs

Squares of 1 to 10

Squares of 1 to 10

EasyFunction Programs · 12 of 39

Write a Python function that creates and prints a list of the squares of 1 to 10.

Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Approach

  1. Build the list, print it and return it.

Solution

Python
def squares(n=10):    result = [i * i for i in range(1, n + 1)]    print(result)    return result squares()
Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Learn the concept

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

More function programs

← All 39 Function Programs

Even numbers from a list

Even numbers from a list

EasyFunction Programs · 11 of 39

Write a Python function that returns the even numbers of a list.

Input
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output
[2, 4, 6, 8]

Approach

  1. A list comprehension in the return statement.

Solution

Python
def evens(numbers):    return [n for n in numbers if n % 2 == 0] print(evens([1, 2, 3, 4, 5, 6, 7, 8, 9]))
Output
[2, 4, 6, 8]

Learn the concept

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

More function programs

← All 39 Function Programs