Part of the series

Several example codes

~1 min read • Updated Oct 7, 2025

Program Overview

This Python program reads two numbers a and b from the user.
It then evaluates a mathematical expression over the range between these two numbers.
In this example, the expression used is f(x) = x + 1, but you can replace it with any function you like.


Python Code:


def f(x):
    return x + 1  # Replace this with any expression you want

# Input
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))

# Normalize range
start = min(a, b)
end = max(a, b)

# Evaluate and display
for x in range(int(start), int(end) + 1):
    print(f"f({x}) = {f(x)}")

Sample Output:


Enter the first number: 3  
Enter the second number: 6  

f(3) = 4  
f(4) = 5  
f(5) = 6  
f(6) = 7

Step-by-Step Explanation:

- The function f(x) is defined (you can customize it)
- Two numbers are read from the user and ordered correctly
- A loop evaluates the function for each integer in the range
- Results are printed line by line


Written & researched by Dr. Shahin Siami