
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Inverted Star Pattern in Python
In this article, let's try to print an inverted star pattern. This pattern involves publishing a series of stars(*) in each line arranged in a descending order. For example, if the N=5 then the output should be ?
***** **** *** ** *
For printing star (*) patterns frequently, we use for loops. Let's discuss the for loop in Python.
Python for loop
The for loop in Python provides the ability to loop over the items of any sequence, such as a list, tuple, or string. It performs the same action on each item of the sequence. This loop starts with the for keyword, followed by a variable that represents the current item in the sequence. The in keyword links the variable to the sequence you want to iterate over. A colon (:) is used at the end of the loop header, and the indented block of code beneath it is executed once for each item in the sequence.
Following is the syntax of the for loop in Python ?
for iterating_var in sequence: statement(s)
Inverted Star Pattern
In this pattern, the outer loop is used to print the rows, and the inner loop is used to print the columns.
Algorithm
Following are the steps to print an inverted start pattern ?
- Initialize the number of rows to a variable
- The outer loop starts from rows and decrements to 1
- The Inner for loop runs from 0 to i-1, printing the stars for each row and prints the stars by spacing and on the same line
- Move the cursor to the next line after printing all stars in the current row.
Example
Following is an example to print an inverted start pattern using nested for loop ?
#Number of rows N = 6 for i in range(N , 0, -1): for j in range(0,i): print("*", end=' ') print(" ")
Following is the output of the above code ?
* * * * * * * * * * * * * * * * * * * * *
In the above example, we have used two loops. Let's try to print the inverted star using a single for loop.
Example
In the following example, we have to print the inverted star using single for loop ?
n = 6 for i in range (n, 0, -1): print((n-i) * '' + i * '*',)
Following is the output of the above code ?
****** ***** **** *** ** *
Inverted Star Pyramid Pattern
In this pattern, the outer loop is used to print the rows, and the inner loop is used to print the columns. In this program, we are generating an inverted pyramid using stars.
Algorithm
Following is an algorithm to print an inverted star pyramid pattern ?
- Initialize the number of rows, N
- The outer Loop starts from N to 1 (inclusive) in decrementing order. This loop controls the number of rows.
- The Inner loop 1 is used for spaces. loop starts from j = 0 to N-i-1 (inclusive). This loop prints the leading spaces for each row.
- Inner loop 2 is used to print stars on each row, loop starts from j = 0 to 2*i-1 (inclusive).
- After printing spaces and stars for a row, print a newline character to move to the next row.
Example
In the following example we have printed an inverted star pyramid pattern using for loop ?
N = 5 for i in range(N,0,-1): for j in range(N-i): print(' ', end='') for j in range(2*i-1): print('*',end='') print()
Following is the output of the above code ?
********* ******* ***** *** *