Saturday, 14 December 2024

Day 38: Python Program to Print Inverted Star Pattern

 


rows = int(input("Enter the number of rows: "))

for i in range(rows, 0, -1):

    print("*" * i)

#source code --> clcoding.com

Code Explanation:

1. Input: Number of Rows
rows = int(input("Enter the number of rows: "))
input(): Prompts the user to enter a value.
int(): Converts the input (string) into an integer.
rows: Represents the total number of rows in the reverse triangle.

Example:
If the user inputs 5, then:
rows = 5

2. Reverse Triangle Logic
for i in range(rows, 0, -1):
range(rows, 0, -1):
Starts at rows (e.g., 5).
Stops at 0 (not inclusive).
Decrements by -1 in each iteration, creating a countdown.
i: Represents the current row number. The value of i starts from rows and decreases to 1.

Example:
For rows = 5, the range(rows, 0, -1) produces:
5, 4, 3, 2, 1

3. Print Asterisk Pattern
print("*" * i)
"*" * i:
Repeats the asterisk (*) i times for the current row.
For example, if i = 5, "*" is repeated 5 times (*****).
print():
Outputs the repeated asterisks on a new line, creating one row of the triangle.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (41) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (225) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (925) Python Coding Challenge (343) Python Quiz (12) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses