- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to print the pattern ‘G’
When it is required to print the pattern of the letter ‘G’ using ‘*’, a method can be defined, and nested loop can be used to iterate through the numbers and print ‘*” to form a ‘G’ pattern.
Below is a demonstration of the same −
def display_pattern(my_line): my_pattern="" for i in range(0,my_line): for j in range(0,my_line): if ((j == 1 and I != 0 and I != my_line-1) or ((I == 0 or I == my_line-1) and j > 1 and j < my_line-2) or (I == ((my_line-1)/2) and j > my_line-5 and j < my_line-1) or (j == my_line-2 and I != 0 and I != my_line-1 and I >=((my_line-1)/2))): my_pattern=my_pattern+"*" else: my_pattern=my_pattern+" " my_pattern=my_pattern+"\n" return my_pattern num_line = 8 print("The pattern G has been shown below :") print(display_pattern(num_line))
The pattern G has been shown below : **** * * * * * * * * * ****
A method named ‘display_pattern’ is defined that takes the line as parameter.
Nested loop is used to iterate through the numbers,
The ‘*’ is printed whenever a condition is met.
When the base condition is reached, the output is displayed on the console.
The control comes out of the loop.
- PHP program to print the number pattern
- Python Program to Print an Inverted Star Pattern
- Python program to print rangoli pattern using alphabets
- How to print pattern in Python?
- Java Program to Print the Left Triangle Star Pattern
- Program to print Interesting pattern in C++
- Program to print Kite Pattern in C++
- Program to print number pattern in C
- Program to print Diamond Pattern in C
- Program to print numeric pattern in C
- Program to print pyramid pattern in C
- Java Program to Print Pyramid Star Pattern
- Java Program to Print Diamond Star Pattern
- Java Program to Print Square Star Pattern
- Java Program to Print 8 Star Pattern