Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 9: Nested loops and Basic Patterns

2 Replies, 905 Views

[Image: Python.jpg]
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..


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 Heart
Thanks for such amazing tutorial
Rs
* Thankful to Allah *
Kurdy
(04-21-2022, 09:35 PM)Mr.Kurd Wrote: Thanks for such amazing tutorial

Thank you, too. For your precious comment  Heart
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,179 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,536 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,570 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,776 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,773 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,738 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)