Diamond Pattern in Python Using For Loop

Diamond pattern in python. The python programming language is used to build a diamond pattern program. The diamond pattern will be printed in the production of this program. In Python, a diamond pattern program with an asterisk and a lines range is used to print the number of lines output.

lines = 5
k = 2 * lines - 2
for i in range(0, lines):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print("* ", end="")
print("")
k = lines - 2
for i in range(lines, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print("* ", end="")
print("")

 

Output:
     *

    ***

   *****

  *******

 *********

  *******

    *****

     ***

       *

Python range() in For Loop

The Python range() function constructs an immutable sequence of numbers beginning with the specified start integer and ending with the given finish integer. It’s a built-in function that returns a range object made up of a series of integer integers that we can run over with a for loop. To make writing a for loop easier, we can utilise a range(). The range() must have a set stop value, but we may also change the start ing value and the step between integers.

For Loop Statement in Python

Python for loops are commonly used in data science work to loop through an iterable object and perform the same operation on each entry. A for loop, for example, would allow us to cycle through a list, doing the same operation on each item. In Python, the for loop is used to iterate over a sequence, which can be a list, tuple, array, or string. The init step is the first to be conducted, and it is only done once. You can declare and initialise any loop control variables in this stage.