A Python Program to Print Diamond Using Star-Hyphen?
● Upper Half Construction:
The first for loop runs from 0 to n - 1 and constructs the top half of the diamond.
● Space Calculation (Upper):
spaces = n - i - 1 determines how many spaces to print before the characters, keeping the pattern centered.
● Character Selection (Upper):
char = '*' if i % 2 == 0 else '-' alternates characters — * on even rows and - on odd rows.
● Pattern Printing (Upper):
print(" " * spaces + char * (2 * i + 1)) prints each line with increasing characters centered by leading spaces.
● Lower Half Construction:
The second for loop runs in reverse from n - 2 down to 0, forming the bottom half of the diamond.
● Space Calculation (Lower):
Again, spaces = n - i - 1 ensures the pattern remains symmetrical and centered.
● Character Selection (Lower):
Just like in the upper part, it alternates characters on even and odd rows using the same logic.
● Final Diamond Output:
Together, the upper and lower halves form a complete diamond pattern with alternating * and - symbols in each row.
Program:
def symbol_diamond(n):
for i in range(n):
spaces = n - i - 1
char = '*' if i % 2 == 0 else '-'
print(" " * spaces + char * (2 * i + 1))
for i in range(n - 2, -1, -1):
spaces = n - i - 1
char = '*' if i % 2 == 0 else '-'
print(" " * spaces + char * (2 * i + 1))
symbol_diamond(5)
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |