##### Nested for Loops
 Nested for Loops sammay Unladen Swallow Posts: 1 Threads: 1 Joined: Jan 2021 Reputation: Jan-09-2021, 05:24 PM (This post was last modified: Jan-09-2021, 05:53 PM by buran.) Hey everybody, Hope everything goes well, As I am a newcomer in Python, I have difficulty in understanding the way Python calculate nested loops. Here is my exact question: I'm trying to print triangle with stars, line 1: for num in range (6): line 2: stars = ' ' line 3: for x in range(num): line 4: stars += '*' line 5: print(stars) Output: line 1: null line 2: * line 3: ** line 4: *** line 5: **** line 6: ***** -------------------------------------------------------------------- What I've found is that when I write line 1, the Python considers 6 row, and for line 3, Python considers 5 columns. when num=0 , x=null, and for num=1 , x=0, and so on. why for num=0 Python collect x=null? Thank you so much in advance for your consideration buran write Jan-09-2021, 05:53 PM:Please, use proper tags when post code, traceback, output, etc. See BBcode help for more info. Also, don't include line numbers with your code Reply deanhystad So-and-so of the Yard Posts: 2,693 Threads: 12 Joined: Feb 2020 Reputation: Jan-09-2021, 06:48 PM I think this is your program ```for num in range (6): stars = ' ' for x in range(num): stars += '*' print(stars)```When I run this program it does not print "null". The first line is blank (actually it has one space). Is that what you mean by null? The reason the first line is blank is because num = 0. Since num is the number of '*' appended to stars, appending zero '*' should give you a zero stars. Right? Is your confusion about the for loops starting at 0? This is the fault of the range function, not the for loop. range(6) will return 0, 1, 2, 3, 4, 5. If you want range to start with something other than 0 you need to specify the starting value in the call. To always print at least 1 star you can change your code to look like this: ```for num in range (1,7): stars = ' ' for x in range(num): stars += '*' print(stars)`````````Output: * ** *** **** ***** ******``````The reason I use range(1, 7) is because I want 1 to be the first number in my range and 7 to be the first number not in my range. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Break out of nested loops muzikman 11 993 Sep-18-2021, 12:59 PM Last Post: muzikman How to break out of nested loops pace 11 2,155 Mar-03-2021, 06:25 PM Last Post: pace How to make this function general to create binary numbers? (many nested for loops) dospina 4 1,530 Jun-24-2020, 04:05 AM Last Post: deanhystad Python beginner - nested while loops mikebarden 1 895 Jun-01-2020, 01:04 PM Last Post: DPaul best way out of nested loops? Skaperen 7 1,899 May-30-2020, 05:20 PM Last Post: Skaperen alternative to nested loops for large data set JonnyEnglish 2 1,173 Feb-19-2020, 11:26 PM Last Post: JonnyEnglish Nested for loops d79danny 8 2,138 Oct-05-2019, 02:12 AM Last Post: newbieAuggie2019 Printing out a triangle using nested for loops MrGoat 12 3,791 Jan-16-2019, 07:21 PM Last Post: ichabod801 Looping and nested loops albry 5 2,472 Dec-20-2018, 12:10 PM Last Post: buran nested for loops to recursion ashkea26 1 1,847 Nov-02-2018, 09:53 AM Last Post: Larz60+

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020