Python | Program to print double sided stair case pattern

Python | Program to print double sided stair case pattern: we are going to print a double-sided staircase pattern. Here, n is an integer that is provided by the user.Below mentioned is the python program to print the double sided stair case pattern.

Examples:

Input : 10
Output :

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

Note : This code only works for even values of n.

Recommended Please try your approach on ide first, before moving on to the solution.

# Python3 Program to demonstrate # staircase pattern # function definition def pattern(n)

Algorithm of Program to print double sided stair case pattern

We have seen the Input and Output of the double-sided staircase pattern now let’s dive deep into the algorithm part followed by the code:

Create a function staircase_pattern()
Pass n as a parameter in the function
Create a for loop for the rows
Mark a conditional operator
Create for loop for printing spaces
According to value carry on with the looping operation
The result will be the desired pattern
Print the pattern

# Python3 Program to demonstrate
# staircase pattern

# function definition
def pattern(n):

	# for loop for rows
	for i in range(1,n+1):

		# conditional operator
		k =i + 1 if(i % 2 != 0) else i

		# for loop for printing spaces
		for g in range(k,n):
			if g>=k:
				print(end=" ")

		# according to value of k carry
		# out further operation
		for j in range(0,k):
			if j == k - 1:
				print(" * ")
			else:
				print(" * ", end = " ")


# Driver code
n = 10
pattern(n)

Output:


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

Leave a Reply