×

How to Print Pattern in Python

How to Print Pattern in Python.

Pattern programs are really important for beginners and they are always asked in all the technical interviews.

Multiple loops approach is used to print the different type of patterns. (de)

defined the function 'pypart' for simple pyramid.
def pypart(rows):
# to handle the printing of rows, this outer loop is used.
for i in range(0, rows):
# this inner loop is used to handle the printing of number of columns.
# Values change according to the outer loop.
for j in range(0, i+1):
# for printing stars and one space after that every star.
print("* ",end="")
# \r to end line after each row.
print("\r")
taking number of rows as input.
using 'int' because input takes ‘str’ type as input and we have to convert it into ‘int’ type.
rows= int(input("Enter the number of rows:"))
pypart(rows) 

Simple Pyramid Pattern

OUTPUT

How To Print Pattern In Python
Function to demonstrate printing pattern
def pypart(rows):
myList = []
for i in range(1,rows+1):
myList.append("* "*i)
print("\n".join(myList))
taking number of rows as input.
using 'int' because input takes ‘str’ type as input and we have to convert it into ‘int’ type.
rows= int(input("Enter the number of rows:"))
pypart(rows)

Simple Pyramid Using lists

OUTPUT

How To Print Pattern In Python

Simple Pyramid after 180 degree rotation

def pyramidpart2(rows):
# number of spaces n = 2*rows - 2 # this loop is used to handle the printing of rows for i in range(0, rows): # inner loop is used to handle number spaces # values are changed according to the requirement for j in range(0, n): print(end=" ") # n is decremented after each loop n = n - 2 # this inner loop is used to handle number of columns # values are changed according to the outer loop for j in range(0, i+1): # printing stars print("* ", end="") # \r to end line after each row. print("\r") 
taking number of rows as input.
using 'int' beacuse input takes str type as input and we have to convert it onto int type.
rows= int(input("Enter the number of rows:"))
pyramidpart2(rows)

OUTPUT

How To Print Pattern In Python

3.    Simple Triangle pattern

defined the function 'triangle' for simple triangle pattern.
def triangle(rows):
# no. of spaces
k = 2rows - 2 # To handle the printing of rows, this outer loop is used. for i in range(0, rows): # printing of spaces is handled by this inner loop. # values will change according to the requirement. for j in range(0, k): print(end=" ") # k is decremented after each loop k = k - 1 # this inner loop is used to handle the printing of columns. # Values will change according to the requirement. for j in range(0, i+1): # for printing the stars print(" ", end="")
# \r to end the line
print("\r")
taking number of rows as input.
using 'int' beacuse input takes ‘str’ type as input and we have to convert it onto ‘int’ type
rows= int(input("Enter the number of rows:"))
triangle(rows)

OUTPUT

How To Print Pattern In Python

4.    Inverse Triangle pattern.

defined the function 'triangle' for simple triangle pattern.
def triangle(rows):
# no. of spaces
k = 2rows - 2 # To handle the printing of rows, this outer loop is used. for i in range(rows, -1,-1): # printing of spaces is handled by this inner loop. # values will change according to the requirement. for j in range(k,0,-1): print(end=" ") # k is decremented after each loop k = k + 1 # this inner loop is used to handle the printing of columns. # values will change according to the requirement. for j in range(0, i+1): # for printing the stars print(" ", end="")
# \r to end the line
print("\r")
taking number of rows as input.
using 'int' beacuse input takes str type as input and we have to convert it onto int type
rows= int(input("Enter the number of rows:"))
triangle(rows)

OUTPUT

How To Print Pattern In Python

5.    Diamond pattern

 # taking number of rows as input.
# using 'int' beacuse input takes str type as input and we have to convert it onto int type
row = int(input("Enter the number of row: ")) 
# k is used to print the space 
k = 2 * row - 2 
# to print the number of rows this outer loop is used 
for i in range(0, row): 
    # to print the number of spaces this inner loop is used
    for j in range(0, k): 
        print(end=" ") 
    # k is decremented after each iteration 
    k = k - 1 
    # to print the stars this inner loop is used
    for j in range(0, i + 1): 
        print("* ", end="") 
    print("") 
# For printing downward triangle Pyramid 
# k is used to print space
k = row - 2 
# this loop is used to print downward triangle and handle the printing of rows
for i in range(row, -1, -1): 
    # to print the spaces this inner loop is used
    for j in range(k, 0, -1): 
        print(end=" ") 
    # k is incremented after each iteration 
    k = k + 1 
    # stars will be printed by this inner loop
    for j in range(0, i + 1): 
        print("* ", end="") 
    print("")  

OUTPUT

How To Print Pattern In Python

6.    Half Diamond Pattern

 # taking number of rows as input.
# using 'int' because input takes str type as input and we have to convert it onto int type
r = int(input("Enter the number of rows: "))
# this outer loop will be used to print the number of rows in the upper simple pyramid
for i in range(0, r): 
    # This inner loop will be used to print the stars 
    for j in range(0, i + 1): 
        print("*", end=' ') 
    print(" ") 
# To print the downward inverse simple pyramid
# this outer loop will print the number of rows of the downward inverse simple pyramid
for i in range(r + 1, 0, -1):
    # this inner loop is used to print the stars
    for j in range(0, i - 1): 
        print("*", end=' ') 
    print(" ")  

OUTPUT

How To Print Pattern In Python

7.    Hour-glass Pattern

 # hour_glass_pattern is defined
def hour_glass_pattern(rows):
    # this loop is used to print the upper half
    for i in range(1, rows + 1): 
        # for printing spaces at the beginning of each row
        for k in range(1, i):
            print(" ", end = "")
        # for printing 'i' to 'row' values at the end of each row
        for j in range(i, rows + 1): 
            print("*", end = " ") 
        print()
    # this loop is used for printing the lower half
    for i in range(rows - 1, 0, -1): 
        # for printing spaces at the beginning of each row
        for k in range(1, i):
            print(" ", end = "")
        # for printing 'i' to 'row' values at the end of each row
        for j in range(i, rows + 1):
            print("*", end = " ")
        print()
# taking number of rows as input.
# using 'int' beacuse input takes ‘str’ type as input and we have to convert it onto ‘int’ type
rows= int(input("Enter the number of rows:"))
hour_glass_pattern(rows)     

OUTPUT

How To Print Pattern In Python

Related Topics

Create First GUI Application using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

How to slice a list in python

When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to...

5 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python Pickle

The pickle is a module that enables serialization and de-serialization of the structure of the object in python. Pickling is the process that uses the protocols to convert the Python...

5 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

How to convert integer to float in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

5 minutes read.