Write a program to print following Python code:-
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
Answers
Answer:
111 is a program to print python code
Answer:
Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed.
Some of the Patterns are shown in this article.
Simple pyramid pattern
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
pypart(n)
Output:
*
* *
* * *
* * * *
* * * * *
Another Approach:
Using List in Python 3, this could be done in a simpler way
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
myList = []
for i in range(1,n+1):
myList.append("*"*i)
print("\n".join(myList))
# Driver Code
n = 5
pypart(n)
# Function to demonstrate printing pattern
def pypart2(n):
# number of spaces
k = 2*n - 2
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range(0, k):
print(end=" ")
# decrementing k after each loop
k = k - 2
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
pypart2(n)
Output:
*
* *
* * *
* * * *
* * * * *
Printing Triangle
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern triangle
def triangle(n):
# number of spaces
k = 2*n - 2
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range(0, k):
print(end=" ")
# decrementing