Nested Loops:
Nested loop means loop inside a loop. In most languages we use nested loops to print patterns like triangles but python made our work easier. In python we use a concept of multiplying strings with integers and as a result we get a another string that is n-time of the number which is multiplied to it so first let's code this new concept and then we dive into some basic and advanced patterns..
Now use this concept with loops to print a triangle
We can reverse this triangle by just change the values in program let’s see.
So what we have done in this program is that we change the value and we said that i will start from 5 and decrease till 0. We changed the condition and told the program to terminate when ‘i’ is greater than or equal to zero and at every step decrease ‘i’ by 1.
Now let’s print a square and a rectangle without wasting any time. Let's jump into code. The only difference between their code is of values
Here we have used a nested loop here ‘i’ selects the size of square and ‘j’ selects the number of ‘*’ which needs to be printed. As you can see we have used something new here that is end=”” we use it to override the newline and print all the ‘*’ on a single line. If you change the values of the code and both ‘i’ and ‘j’ have different values then this will be a rectangle. You can also use input function to let user select the size of you triangle or square let’s code it
That’s it for now. In the next thread we will cover some advanced patterns so that you can get a better understanding of nested loop’s logic. keep good care of your health
Nested loop means loop inside a loop. In most languages we use nested loops to print patterns like triangles but python made our work easier. In python we use a concept of multiplying strings with integers and as a result we get a another string that is n-time of the number which is multiplied to it so first let's code this new concept and then we dive into some basic and advanced patterns..
Code:
# Program to print 5 asterisk
symbol = '*'
print(5 * symbol)
Now use this concept with loops to print a triangle
Code:
# Program to print Triangle
i = 1
while i <= 5:
print(i * "*")
i += 1
We can reverse this triangle by just change the values in program let’s see.
Code:
# Program to print Triangle
i = 5
while i >= 0:
print(i * "*")
i -= 1
So what we have done in this program is that we change the value and we said that i will start from 5 and decrease till 0. We changed the condition and told the program to terminate when ‘i’ is greater than or equal to zero and at every step decrease ‘i’ by 1.
Now let’s print a square and a rectangle without wasting any time. Let's jump into code. The only difference between their code is of values
Code:
# Program to print Square
# and demonstrate
#nested loops
for i in range(4):
for j in range(4):
print("*", end="")
print()
Here we have used a nested loop here ‘i’ selects the size of square and ‘j’ selects the number of ‘*’ which needs to be printed. As you can see we have used something new here that is end=”” we use it to override the newline and print all the ‘*’ on a single line. If you change the values of the code and both ‘i’ and ‘j’ have different values then this will be a rectangle. You can also use input function to let user select the size of you triangle or square let’s code it
Code:
# Program to print
#Square/Rectangle using
#input function
n = int(input("Enter the size of square: "))
for i in range(n):
for j in range(4):
print("*", end="")
print()
Code:
# Program to print Triangle
n = int(input("Enter the size of Triangle: "))
i = 1
while i <= n:
print(i * "*")
i += 1
That’s it for now. In the next thread we will cover some advanced patterns so that you can get a better understanding of nested loop’s logic. keep good care of your health
"Allah Hafiz" and "Good Bye" Till the next post
Keep Learning and Keep exploring"
Love me like you do