Skip to content

Understanding nested loops #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Nested-loops
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#Program to understand working of loops and nested loops in python by a simple pattern formation.
#suppose if user enters 4 then the following should be printed
#output -
# *

# * *

# * * *

# * * * *
# ignore all the '#' above in the output

a=int(input("enter ")) #we take an input from the users of how many lines the pattern should run.
for i in range(a): #executing a loop, here i's value will start from 0 till a-1.
t=((a-1)-i) #carefuly observing the pattern we get to understand that we need loop for spaces and one for * so first we define t that will contain values from a-1 to 0
for b in range(t): #creating a Nested loop for spaces
print(" ",end=' ')
k=i+1 #by observing the pattern above we can see that we need a variable that can print the number of * , and by logic the line number = number of * in that line
for c in range(k): # making a loop to print *
print("*"," ",end=' ')
print("\n")

# Hence loops work in a defined order to produce a specific result.
# hence in all , all the logical patterns are solved by nested loop concept and to be a master in that getting the right logic and sufficient number of nested loops is important.
# If possible try running this python code and try putting different input and see the output