Z Pattern in Python
Python
Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming language for newcomers and seasoned C++ and Java programmers. Python is a well-known high-level, general-purpose language used for various tasks, including web development, scraping, and GUI creation. Although many Python tutorials are many Python tutorials that go into great detail, it might not be enough for you to understand this language fully.
Features of Python Programming Language:
The most popular high-level, multipurpose programming language is Python. Python is a more extensible language. Python supports procedural paradigms. Like java, programmers only have to type a small amount of text, and the language’s indentation rule ensures that their code is always readable. Nearly every major tech company uses Python, including Google and YouTube.
Advantages of python:
Probably one of Python's biggest benefits is this. Python increases productivity by allowing developers to focus on finding solutions rather than learning the syntax and semantics of the language. They can accomplish a great deal with very little code.
Disadvantages of Python:
Python has some restrictions as well. The main drawbacks of Python are its execution speed, struggle in switching to other languages, weakness in developing mobile applications, large memory consumption, and lower level of acceptance in the entrepreneurship development space.
Z pattern in python defines a z-shaped number series or z-shaped design with numbers, symbols, etc.
The below program defines the code for the Z Alphabet pattern with numbers using functions.
CODE:
# Z Pattern using a function
def zpattern(N):
# Declaring the values of Right, Left, and Diagonal values as Top=1, Bottom=1, and Diagonal=N-1
Top=1
Bottom=1
Diagonal=N-1
# using for loop printing the first row(Top)
for index in range(N):
print(Top, end = ' ')
Top += 1
print()
# Main Loop for the rows from (2 to n-1)
for index in range(1, N - 1):
for side_index in range(2 * (N - index - 1)):
print(' ', end = '')
# Printing diagonal values(Diagonal)
print(Diagonal, end = '')
Diagonal -= 1
print()
# for loop for printing last row(Bottom)
for index in range(N):
print(Bottom, end = ' ')
Bottom += 1
#N defines the Number of rows.
N = 7
zpattern(N)
CODE EXPLANATION:
For the above code to get a Z pattern output, we define a function named zpattern; then we declare the values of Right, Left, and Diagonal values as Top, Bottom, and Diagonal to the values of Diagonal=N-1. The N includes user input to what extent the pattern is to be concluded, whereas the Bottom is assigned to 1, and the Top is assigned to 1. We take a loop inside the function, which includes for loop from range 0 to N. We increment the Top to Top++ when the top is in the range. The main loop from the rows part starts from 2 and ends from N-1. Then we move on to Spaces for diagonals to give spaces for diagonals. We take another for loop, which starts from 1 to N-1. If the diagonal is out of range, we decrement the diagonal to diagonal— which is also returned as diagonal=diagonal-1. At Last, we take another for loop for the bottom to increment.
OUTPUT:
1 2 3 4 5 6 7
6
5
4
3
2
1 2 3 4 5 6 7
Let us now write a code for user-defined Input, which includes the same code where the N is assigned to N=int(input(“Enter N value”))
CODE:
# Z Pattern using a function
def zpattern(N):
# Declaring the values of Right, Left, and Diagonal values as Top=1, Bottom=1, and Diagonal=N-1
Top=1
Bottom=1
Diagonal=N-1
# using for loop printing the first row(Top)
for index in range(N):
print(Top, end = ' ')
Top += 1
print()
# Main Loop for the rows from (2 to n-1)
for index in range(1, N - 1):
for side_index in range(2 * (N - index - 1)):
print(' ', end = '')
# Printing diagonal values(Diagonal)
print(Diagonal, end = '')
Diagonal -= 1
print()
# for loop for printing last row(Bottom)
for index in range(N):
print(Bottom, end = ' ')
Bottom += 1
#N defines the Number of rows.
N = int(input("Enter N value "))
zpattern(N)
OUTPUT:
Enter N value 5
1 2 3 4 5
4
3
2
1 2 3 4 5
Similarly, we try the code for the Z alphabet pattern using stars with user-defined input.
CODE:
result_str=""
n=int(input("Enter n value: "))
for row in range(0,n):
for column in range(0,n):
if (((row == 0 or row == n-1) and column >= 0 and column <= n-1) or row+column==n-1):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
CODE EXPLANATION:
The way of code was generated before, and this code might give the difference in considering only two loops for which the code starts and another for the end. The loop increments the row by adding a string * and breaks when the condition fails. The condition for the row is either row is equal to 0 or equal to n-1, whereas the column should be greater than or equal to 0 and less than or equal to the given input-1, then the summation of the row and column should be equal to input-1. Here the input is n.
OUTPUT:
Enter n value: 8
********
*
*
*
*
*
*
********