Northeast Triangle - Python Programming Exercise

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 (_) are added at the beginning of each row to align the stars. The program uses nested loops to generate the desired pattern. The outer loop controls the number of rows, while the inner loop is responsible for adding the correct number of underscores and stars. The number of underscores increases with each row, and the number of stars decreases accordingly, creating the inverted triangle shape. This exercise demonstrates how looping structures can be used to create patterns in Python. It also serves as a great introduction to string manipulation and formatting in Python, showing how to combine characters like underscores and asterisks to form visually appealing outputs. The task is a practical way to learn about iteration and how to control the flow of a program to generate specific outputs based on user input.

 Category

Python Data Types

 Exercise

Northeast Triangle

 Objective

Develop a Python program that prompts the user for a width and displays a triangle like this one:

Enter the desired width: 5

*****
_****
__***
___**
____*

 Example Python Exercise

 Copy Python Code
# Prompt the user for the desired width of the triangle
width = int(input("Enter the desired width: "))

# Loop through each row from 0 to width-1
for i in range(width):
    # Print leading underscores
    print('_' * i + '*' * (width - i))

 Output

Case 1:
Enter the desired width: 5
*****
_****
__***
___**
____*

Case 2:
Enter the desired width: 3
***
__**
___*

 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.

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

  •  Character Loop

    This Python program uses a "for" loop to print the uppercase letters from "B" to "N". By utilizing Python's range() function and specifying the A...

  •  Double Precision Pi Approximation

    This Python program calculates an approximation for PI using the series expression: pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... The program allows t...

  •  Perimeter and Area

    This Python program calculates the perimeter, area, and diagonal of a rectangle based on its width and height. The program prompts ...

  •  Hex and Bin

    This Python program prompts the user to enter a number and then displays its equivalent values in both hexadecimal and binary formats. The program conti...