Part of the series

Several example codes

~2 min read · Updated Oct 7, 2025

Program Overview

This Python program first reads a value x, then reads n data points as decimal pairs (xᵢ, yᵢ).
It calculates the slope m and intercept b using linear regression formulas, and displays the final equation y = mx + b.


Formulas Used:

  • x̄ = (∑xᵢ) / n → Mean of x values
  • ȳ = (∑yᵢ) / n → Mean of y values
  • ∑xᵢyᵢ = x₁y₁ + x₂y₂ + ... + xₙyₙ
  • ∑xᵢ² = x₁² + x₂² + ... + xₙ²
  • m = (∑xᵢyᵢ - ȳ ∑xᵢ) / (∑xᵢ² - x̄ ∑xᵢ)
  • b = ȳ - m x̄

Python Code:


n = int(input("Enter number of points: "))

x_vals = []
y_vals = []

for i in range(n):
    x_i = float(input(f"x[{i+1}] = "))
    y_i = float(input(f"y[{i+1}] = "))
    x_vals.append(x_i)
    y_vals.append(y_i)

sum_x = sum(x_vals)
sum_y = sum(y_vals)
sum_xy = sum(x * y for x, y in zip(x_vals, y_vals))
sum_x2 = sum(x ** 2 for x in x_vals)

x_mean = sum_x / n
y_mean = sum_y / n

m = (sum_xy - y_mean * sum_x) / (sum_x2 - x_mean * sum_x)
b = y_mean - m * x_mean

print(f"Linear equation: y = {round(m, 4)}x + {round(b, 4)}")

Sample Output:


Enter number of points: 3  
x[1] = 1  
y[1] = 2  
x[2] = 2  
y[2] = 3  
x[3] = 3  
y[3] = 5  

Linear equation: y = 1.5x + 0.0

Step-by-Step Explanation:

- The program collects x and y values into lists
- It computes the required sums and means
- Then it applies the regression formulas to calculate m and b
- Finally, it prints the linear equation rounded to 4 decimal places


Written & researched by Dr. Shahin Siami

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue