Show Function - Python Programming Exercise

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 program calculates the corresponding y value using the equation and then prints that number of asterisks. This allows the user to visually interpret the shape of a parabola by observing how the number of asterisks changes with each value of x. The program provides an excellent opportunity to practice loops and function plotting in Python. By iterating through the integer values of x, it demonstrates how mathematical functions can be represented graphically using text characters. This graphical representation of the function is a simple but effective way to understand the nature of quadratic equations and to see how the output values grow and shrink as x moves across the specified range.

 Category

Python Data Types

 Exercise

Show Function

 Objective

Develop a Python program to "draw" the graph of the function y = (x-4)^2 for integer values of x ranging from -1 to 8. It will display as many asterisks on the screen as the value obtained for "y", like this:

*************************
****************
*********
****
*

*
****
*********
****************

 Example Python Exercise

 Copy Python Code
# Loop through integer values of x from -1 to 8
for x in range(-1, 9):
    y = (x - 4) ** 2  # Calculate the value of y using the given function
    print('*' * y)  # Print y number of asterisks

 Output

***************
****************
***********
******
*
*
****
**********
*****************
***********************
*******************************

 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.

  •  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...

  •  Conditional Symbols

    This Python program prompts the user to enter a symbol and determines whether it is an uppercase vowel, a lowercase vowel, a digit, or any other ...