Inverted star pattern in Python
In this post, you will learn how to print an inverted star pattern using the Python programming language. Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful to improve your logical programming skills and is helpful in game development and other logical application development.
Inverted star pattern using for loop
The following Python program prints an inverted star pattern on the specified n lines. In this Python program, we take a row number in input from the user. Here the row demonstrates the number of lines that will be printed in the inverted star pattern shape.
# Get range of the star pattern
n = int(input("Enter the range: \t "))
# outer loop over the range
for rows in range(n, 0, -1):
# Inner loop to create gap
for columns in range(0, n-rows):
print(end=" ")
# Inner loop to print star
for columns in range(0, rows):
print("*", end=" ")
print()
Output of the above code:
Enter the range: 8
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Inverted star pattern using while loop
In the given program, we have used the while loop to print the inverted star pattern.
num = 10
temp = num
while(num > 0):
print((temp-num) * ' ' + num * '*')
num = num-1
Output of the above code:
**********
*********
********
*******
******
*****
****
***
**
*
Inverted Right Triangle Star Pattern
In the given Python program, we have used the while loop to print the inverted right triangle star pattern.
# Python Program to Print Inverted Right Triangle Star Pattern
rows = int(input("Please Enter the total Number of Rows : "))
print("Inverted Right Angle Triangle of Stars")
i = rows
while(i > 0):
j = i
while(j > 0):
print('* ', end = ' ')
j = j - 1
i = i - 1
print()
Output of the above code:
Please Enter the total Number of Rows : 6
Inverted Right Angle Triangle of Stars
* * * * * *
* * * * *
* * * *
* * *
* *
*
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary