Hướng dẫn piat hollow triangle python

This python program generates hollow right angle triangle pattern made of stars up to n lines.

In this python example, we first read number of row in the hollow right angle triangle pattern from user using built-in function input[]. And then we generate hollow right angle triangle pattern using python's for loop.

Nội dung chính

  • Python Source Code: Hollow Right Angle Triangle Pattern
  • Description
  • Explanation
  • Python Code To Print Hollow Right Angled Triangle Pattern
  • How do you print a hollow right
  • How do you print a right
  • How do you draw a hollow triangle in Python?

Python Source Code: Hollow Right Angle Triangle Pattern


# Hollow Right Angle Triangle Pattern Using Stars

row = int[input['Enter number of rows required: ']]

for i in range[row]:
    for j in range[i+1]:
        if j==0 or j==i or i==row-1:
            print['*',end=" "]
        else:
            print[" ", end=" "]
    print[]

In this program print[] only is used to bring control to new lines.

Output

Enter number of rows required: 8

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

In this shot, we will discuss how to generate a hollow right-angled triangle with stars in Python.

We can print a plethora of patterns with Python. The basic and only prerequisite is a good understanding of how loops work in Python.

Here, we will use simple for loops to generate a hollow right-angled triangle with stars.

Description

A triangle is said to be right-angled if it has an angle equal to 90 degrees on its left side.

We will use 2 for loops:

  • Outer loop: To handle the number of rows.
  • Inner loop: To handle the number of columns.
# Number of rows
n = 5
# Loop through rows
for i in range[1, n+1]:
    # Loop through columns
    for j in range[1, n+1]:
        # Printing Pattern
        if [i == j] or [j == 1] or [i == n]:
            print["*", end=" "]
        else:
            print[" ", end=" "]
    print[]  

Explanation

  • In line 2, we take the input for the number of rows [i.e. the length of the triangle].

  • In line 4, we create a for loop to iterate through the number of rows.

  • In line 6, we create an inner nested for loop to iterate through the number of columns.

  • From lines 8 to 11, we use conditional statements to create the pattern. The end statement helps to stay on the same line.

  • In line 12, we use the print[] statement to move to the next line.

Want to know how to print hollow right angled triangle pattern in python, then you are at the right place today in this tutorial. I will show you the python program to print hollow right angled triangle pattern, so follow this tutorial till the end. 

Printing patterns in python is fun and very interesting. You must have created python programs to print pyramids and other shapes but today I will show you how to print a hollow right angled triangle pattern in python.

We will use for loop for creating pattern, no external python library is needed, just follow this tutorial till the end.

Python Code To Print Hollow Right Angled Triangle Pattern


row = 10

for i in range[row]:
    for j in range[i+1]:
        if j==0 or j==i or i==row-1:
            print['*',end=" "]
        else:
            print[" ", end=" "]
    print[]

Above is the python program to print hollow right angled triangle pattern. Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.

To run this python program, follow the below steps:

  • Create a new folder for this python project.
  • Open it in a code editor of your choice.
  • Create a python file with an ending .py extension.
  • Copy the above code and paste it in your file.
  • Open a terminal or CMD at folder location
  • And type this command python filename.py [Name of your file].

After running this program you will see a hollow right angled triangle pattern printed like below output.


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

Don’t want to create all the files and folders then you can run this program now using this online python compiler it is fast and easy.

Summary

This was the python program to print hollow right angled triangle pattern. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.

Here are some more python drawing tutorials for you:

  • Python Program To Print Hollow Triangle Pattern.
  • Python Program To Convert Miles To Feet.
  • Python Program To Convert Rupee To Paise.

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

How do you print a hollow right

Description.

# Loop through rows. for i in range[1, n+1]:.

# Loop through columns. for j in range[1, n+1]:.

if [i == j] or [j == 1] or [i == n]: print["*", end=" "].

else: print[" ", end=" "].

How do you print a right

Pattern - 4: Printing Triangle Pyramid.

n = int[input["Enter the number of rows: "]].

m = [2 * n] - 2..

for i in range[0, n]:.

for j in range[0, m]:.

print[end=" "].

m = m - 1 # decrementing m after each loop..

for j in range[0, i + 1]:.

# printing full Triangle pyramid using stars..

How do you draw a hollow triangle in Python?

The main difference is that, in a filled triangle, there are two stages in each line: the part where you write some spaces and then a part where you write some stars. But in a hollow triangle you need four stages: write spaces, write one star, write zero or more spaces and write one star again.

Chủ Đề