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


Messages In This Thread
Nested for Loops - by sammay - Jan-09-2021, 05:24 PM
RE: Nested for Loops - by deanhystad - Jan-09-2021, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,906 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,722 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,595 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,225 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,355 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,377 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,420 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 1,874 Jun-01-2020, 01:04 PM
Last Post: DPaul
  best way out of nested loops? Skaperen 7 3,804 May-30-2020, 05:20 PM
Last Post: Skaperen
  alternative to nested loops for large data set JonnyEnglish 2 2,577 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