Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for Loops
#1
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
#2
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
  reduce nested for-loops Phaze90 11 1,757 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,638 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,239 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,263 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 4,331 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 1,838 Jun-01-2020, 01:04 PM
Last Post: DPaul
  best way out of nested loops? Skaperen 7 3,705 May-30-2020, 05:20 PM
Last Post: Skaperen
  alternative to nested loops for large data set JonnyEnglish 2 2,526 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish

Forum Jump:

User Panel Messages

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