Evaluate Function Outputs - Python Programming Exercise

This Python program displays the values of the function y = x² - 2x + 1 for integer values of x ranging from -10 to +10. The function is a quadratic equation, and the program iterates through each integer value of x within the specified range, calculating the corresponding y value for each x. By plugging each x into the equation, the program demonstrates how to compute the output of a mathematical function for a series of inputs. This example program is a great way to understand how to use loops in Python to perform repetitive calculations. It allows the user to observe the behavior of a quadratic function, exploring how the value of y changes as x moves through a given range. Using Python's simple syntax, this program makes it easy to visualize the relationship between x and y, offering an intuitive introduction to both mathematical functions and Python programming.

 Category

Python Data Types

 Exercise

Evaluate Function Outputs

 Objective

Develop a Python program to display certain values of the function y = x^2 - 2x + 1 (using integer numbers for x, ranging from -10 to +10).

 Example Python Exercise

 Copy Python Code
# Loop through integer values of x from -10 to 10
for x in range(-10, 11):
    y = x**2 - 2*x + 1  # Calculate the value of y using the given function
    print(f"For x = {x}, y = {y}")

 Output

For x = -10, y = 121
For x = -9, y = 91
For x = -8, y = 73
For x = -7, y = 57
For x = -6, y = 43
For x = -5, y = 31
For x = -4, y = 21
For x = -3, y = 13
For x = -2, y = 7
For x = -1, y = 3
For x = 0, y = 1
For x = 1, y = 1
For x = 2, y = 3
For x = 3, y = 7
For x = 4, y = 13
For x = 5, y = 21
For x = 6, y = 31
For x = 7, y = 43
For x = 8, y = 57
For x = 9, y = 73
For x = 10, y = 91

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Show Function

    This Python program "draws" the graph of the function y = (x-4)² by displaying a series of asterisks on the screen. For each x value ranging from -1 to 8, the ...

  •  Floating Point, Speed Units

    This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units:...

  •  Floating Point Sphere

    This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and v...

  •  Vowel Check - Conditional

    This Python program prompts the user to input a symbol and categorizes it as a vowel (if it’s a lowercase vowel), a digit, or any other symbol. T...

  •  Northeast Triangle

    This Python program prompts the user to enter a width and then displays a triangle pattern where the number of stars decreases with each row, and underscores (...

  •  Prime Divisors

    This Python program prompts the user to enter a number and then displays that number as a product of its prime factors. The program uses a factorization approa...