0% found this document useful (0 votes)
6K views4 pages

Python Pattern Programs Using While Loop

This document discusses printing patterns in Python using while loops. It provides 3 examples - printing a right triangle pattern, an inverted right triangle pattern, and a number pattern. Each example shows the pattern, Python code to generate it using while loops, and sample output. The conclusion states that different types of patterns can be printed using while loops in Python.

Uploaded by

Easwar Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6K views4 pages

Python Pattern Programs Using While Loop

This document discusses printing patterns in Python using while loops. It provides 3 examples - printing a right triangle pattern, an inverted right triangle pattern, and a number pattern. Each example shows the pattern, Python code to generate it using while loops, and sample output. The conclusion states that different types of patterns can be printed using while loops in Python.

Uploaded by

Easwar Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Python Pattern Programs using While Loop

Python Pattern Programs using While Loop

In this tutorial, we will learn how to print patterns to console using Python While Loop.

* *
* * * *
* * * * * *
* * * * * * * *
1 1
2 3 2 2
4 5 6 3 3 3
7 8 9 10 4 4 4 4

Example 1 – Python Program to Print Right Triangle using While Loop


In this example, we will write a Python program to print the following start pattern to console. We shall read the
number of rows and print starts as shown below.

Pattern

For an input number of 4, following would be the pattern.

*
* *
* * *
* * * *

Python Program

n = int(input('Enter number of rows : '))

i = 1
while i <= n :
j = 1
while j <= i:
print("*", end = " ")
j += 1
print()
i += 1
Inner while loop prints a single row after its complete execution. Outer while loop helps to print n number of
rows.

In other words, outer while loop prints the rows, while inner while loop prints columns in each row.

Output

Enter number of rows : 6


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

Example 2 – Python Program to Print Inverted Right Triangle using While Loop
In this example, we will write a Python program to print the following start pattern to console.

Pattern

For an input number of 4, following would be the pattern.

* * * *
* * *
* *
*

Python Program

n = int(input('Enter number of rows : '))

i = 1
while i <= n :
j = n
while j >= i:
print("*", end = " ")
j -= 1
print()
i += 1

Output

Enter number of rows : 5


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

Example 3 – Python Program to Print Number Pattern using While Loop


In this example, we will write a Python program to print the following pattern to console. We shall read the
number of rows and print numbers as shown below.

Pattern

For an input number of 5, following would be the pattern.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Python Program

n = int(input('Enter number of rows : '))

k = 1
i = 1
while i <= n :
j = 1
while j <= i:
print("{:3d}".format(k), end = " ")
j += 1
k += 1
print()
i += 1

Output

Enter number of rows : 7


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28

Conclusion
In this Python Tutorial, we learned to write Python programs to print different types of patterns using While
Loop.
Python Programming

⊩ Python Tutorial

⊩ Install Python

⊩ Install Anaconda Python

⊩ Python HelloWorld Program

⊩ Python Variables

⊩ Python Variable Data Type Conversion

⊩ Python Comments

Control Statements

⊩ Python If

⊩ Python If Else

⊩ Python While Loop

⊩ Python For Loop

Python String

⊩ Python String Methods

⊩ Python String Length

⊩ Python String Replace

⊩ Python Split String

⊩ Python Count Occurrences of Sub-String

⊩ Python Sort List of Strings

Functions

⊩ Python Functions

Python Collections

⊩ Python List

⊩ Python Dictionary

Advanced

⊩ Python Multithreading

Useful Resources

⊩ Python Interview Questions

You might also like