TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Print Diamond Pattern Using Range()
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Print Diamond Pattern Using Range()

Last updated: Nov 06, 2023 10:20 pm
By Meenakshi Agarwal
Share
3 Min Read
Print Diamond pattern shape using Range
Print Diamond pattern shape using Range
SHARE

This Python program explains a step-by-step process to print Diamond patterns using the Python range function. It includes a working sample for help.

Contents
Diamond pattern program requirementTechnique-1Technique-2Techniques-3

Range() to Print Diamond Pattern

We have demonstrated multiple techniques to print diamond patterns in this post. Please read and evaluate them one by one.

Diamond pattern program requirement

Our objective for this exercise is to produce a diamond shape like the one given below. The pattern uses the star symbol and prints across nine lines.

Must Read: Print patterns in Python with examples

You need to develop a program that takes a number and prints, using stars, a full diamond of the given length. For Example, if the line size is 9, the code should print:

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

Also, you must make use of the Python range method for traversing through the loop.

Technique-1

Here, we’ve created a function and used inner loops with a range() function to print the pattern.

"""
Program desc:
Python sample code to print Diamond pattern
"""

""" Function to print Diamond pattern """
def diamond_pattern(lines): 
    star = 0
    for line in range(1, lines + 1): 
        # inner loop to print blank char 
        for space in range (1, (lines - line) + 1): 
            print(end = " ") 

        # inner loop to print star symbol
        while star != (2 * line - 1): 
            print("*", end = "") 
            star = star + 1
        star = 0

        # line break 
        print()

    star = 1
    ubound = 1
    for line in range(1, lines): 
        # loop to print spaces 
        for space in range (1, ubound + 1): 
            print(end = " ") 
        ubound = ubound + 1

        # loop to print star 
        while star <= (2 * (lines - line) - 1): 
            print("*", end = "") 
            star = star + 1
        star = 1
        print()

# Main Code 
# Enter the size of Diamond pattern
lines = 9
diamond_pattern(lines)

Technique-2

In this technique, we’ll use the Python string property to repeat itself by a number specified along with the multiplication symbol.

lines = 9

for iter in range(lines-1):
   print((lines-iter) * ' ' + (2*iter+1) * '*')

for iter in range(lines-1, -1, -1):
   print((lines-iter) * ' ' + (2*iter+1) * '*')

The above code produces the same diamond shape, as shown in the previous example.

Also Check: Alphabet Pattern “A” Using Python Range()

Techniques-3

Now, we’ll use the code that creates the diamond pattern with just one loop statement.

In this sample, we are using Python 3.6 f-string feature, and a combination of the reversed(), and range() functions.

lines = 9

for x in list(range(lines)) + list(reversed(range(lines-1))):
   print(f"{'': <{lines - x - 1}} {'':*<{x * 2 + 1}}")

Once you run the above code, it prints the diamond pattern as per our requirement.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python sorted function with examples Python Sorted() Function
Next Article Grooming in Agile Scrum Grooming in Agile Scrum

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x