Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:38:33+00:00 2026-06-11T02:38:33+00:00

Why does this code: n = int(raw_input(How many rows? )) for row in xrange(n):

  • 0

Why does this code:

n = int(raw_input("How many rows? "))
for row in xrange(n):
    for col in xrange(row+1):
        print "*",
    print

Display this result (when n = 5)?

*
* *
* * *
* * * *
* * * * *

EDIT – I don’t understand how step by step it gets this form. Does col mean the number of columns? Because it doesn’t make sense as the number of columns is equal to the number of rows even though the input for columns is (rows+1).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:38:35+00:00Added an answer on June 11, 2026 at 2:38 am

    Well, let’s go line-by-line and think like we’re the computer.

    • n = int(raw_input("How many rows? ")) We get what the users inputs, convert it to an integer, and store the result in n.
    • for row in xrange(n): We’re looping over xrange(n), which is more or less the same as [0, 1, 2, 3, 4] (although xrange doesn’t store the full list at once, and so is more memory efficient). So now we know that whenever we see row in the future, we have to replace it with whichever one of those numbers we’re on.
    • for col in xrange(row+1): This line is similar to the last one; now col is one of the values in a list counting from 0 to the value stored in row. So if row is 2 on this iteration of the outer loop, the inner loop will run 3 times. The first time col will equal 0, the second 1, and the third time it will equal 2. (For each of those col values, row will stay 2, since row only changes when the outer loop goes through an iteration.)
    • print "*", This is where the *s actually get printed. The tricky bit here is the comma at the end of the line. A print statement without a comma just prints something and then moves to the next line. When you add the comma, it basically prints the line but adds a space after the line instead of a new line (\n). So, since this line is called for each col value, it will print a * followed by a space on one line for each col value.
    • print Notice that this is not inside the col loop. This statement just prints a newline. The idea here is to print a new line after each row has been printed so that the *s of the next rows are added to a new line, rather than to the previous line. If this print weren’t here, all of the stars would be printed on a single line.

    So, for each row value, this program prints row+1 number of *s on one row.

    In response to question edit:

    I could see how col could be a confusing name. col is just the current column number of the * being printed. To see this more clearly, try replacing print "*", with print col,. You should get the following:

    0
    0 1
    0 1 2
    0 1 2 3
    0 1 2 3 4
    

    (Remember that range(3) produces [0,1,2], while range(3+1) produces [0,1,2,3].)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Does this code cause a memory leak: int main(){ int * a = new
What does this line of code will do. pcm->card->number , pcm->device both are int
I have this code which does the trick: #include <stdio.h> int main() { int
What does this code mean: table = new JTable(){ public boolean isCellEditable(int arg0, int
Why does this code take not run? for(int i=0;i<11;i++){ for(int j=0;j<11;j++){ if(i>0){ if((staticWallLoc[i--][j]&4)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|1;} }
Why does this code: class A { public: explicit A(int x) {} }; class
Why does this code always produce x=2 ? unsigned int x = 0; x++
Why does this code: #include <stdio.h> int main(int argc, char** argv) { printf(%lld\n, 4294967296LL);
Does this code always evaluate to false? Both variables are two's complement signed ints.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.