How To Print Pattern in Python
How To Print Pattern in Python
In Python, for loop is used to print the various patterns. Printing the various patterns
are most common asked programming questions in the interview. The multiple for loops
are used to print the patterns where the first outer loop is used to print the number of
rows, and the inner loop is used to print the number of columns. Most of the patterns
use the following concepts.
Output:
*
* *
* * *
* * * *
* * * * *
Explanation:
In the above code, we have initialized the n variable to enter the number of rows for
the pattern. We entered n = 5, the range of outer for loop will be 0 to 4.
o The iteration of the inner for loop depends on the outer loop. The inner loop is
responsible to print the number of columns.
o The end argument prevents to jump into another line. It will printer the star until
the loop is valid.
o The last print statement is responsible for ending the line after each row.
1. # This is the example of print simple reversed right angle pyramid pattern
2. rows = int(input("Enter the number of rows:"))
3. k = 2 * rows - 2 # It is used for number of spaces
4. for i in range(0, rows):
5. for j in range(0, k):
6. print(end=" ")
7. k=k-2 # decrement k value after each iteration
8. for j in range(0, i + 1):
9. print("* ", end="") # printing star
10. print("")
Output:
*
* *
* * *
* * * *
* * * * *
Pattern - 3: Printing Downward half - Pyramid
Code -
Output:
Output:
Output:
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Output:
Output:
Output:
We have discussed the basic pyramid pattern using for loops. The concept of the
pattern depends on the logic and proper use of for loop.
Output:
Explanation -
In the above code, we have printed the numbers according to rows value. The first row
prints a single number. Next, two numbers, prints in the second row, and the next
three number prints on the third row and so on. In the
Output:
In the above code, we have printed the column value j in the inner for loop.
Output:
Explanation:
In the above code, we have used the reversed loop to print the downward inverted
pyramid where number reduced after each iteration.
Output:
Output:
1. current_Number = 1
2. stop = 2
3. rows = 3 # Number of rows to print numbers
4.
5. for i in range(rows):
6. for j in range(1, stop):
7. print(current_Number, end=' ')
8. current_Number += 1
9. print("")
10. stop += 2
Output:
1
2 3 4
5 6 7 8 9
Output:
Output:
Output:
Output:
In the above section, we have discussed all the basic number patterns. It will make a
strong command on Python for loop. We can any type of pattern using for loop.
Alphabets and Letters Pattern in Python
As we know that the, each alphabet has its own ASCII value, so define a character and
print it to the screen. Let's understand these patterns by following examples
Output:
Explanation -
In the above code, we have assigned the integer value 65 to asciiValue variable which
is an ASCII value of A. We defined for loop to print five rows. In the inner loop body, we
converted the ASCII value into the character using the char() function. It will print the
alphabets, increased the asciiValue after each iteration.
Output:
1. str1 = "JavaTpoint"
2. x = ""
3. for i in str1:
4. x += i
5. print(x)
Output:
J
Ja
Jav
Java
JavaT
JavaTp
JavaTpo
JavaTpoi
JavaTpoin
JavaTpoint
Output:
In this article, we have discussed all basic pattern programs. These patterns are
commonly asked during the interview and it is also helpful to understand the concept of
Python for loop. Once we get the logic of the program, we can print the any pattern
using the Python loops