Print range of positive integers ascending and reverse order - Python
up vote
0
down vote
favorite
I'd like to print the output as a palindromic triangle of positive numbers (without 0's), either through user input int(input()):
for i in range(1,int(input())+1):
print(*range(1, i+1),(*range(i-1, -1, -1)))
or as fixed range like this:
for n in range(1,5):
print(*range(1, n+1),(*range(n-1, -1, -1))
I can get the latter to work as far as this, but I'd like it without the 0's. Two additional issues: 1) Will user input of 5 be the same as hard coded 5 in the range? 2) How to get the 5 included in output? Any idea how this can be done? Thanks in advance.
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
python range reverse
add a comment |
up vote
0
down vote
favorite
I'd like to print the output as a palindromic triangle of positive numbers (without 0's), either through user input int(input()):
for i in range(1,int(input())+1):
print(*range(1, i+1),(*range(i-1, -1, -1)))
or as fixed range like this:
for n in range(1,5):
print(*range(1, n+1),(*range(n-1, -1, -1))
I can get the latter to work as far as this, but I'd like it without the 0's. Two additional issues: 1) Will user input of 5 be the same as hard coded 5 in the range? 2) How to get the 5 included in output? Any idea how this can be done? Thanks in advance.
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
python range reverse
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'd like to print the output as a palindromic triangle of positive numbers (without 0's), either through user input int(input()):
for i in range(1,int(input())+1):
print(*range(1, i+1),(*range(i-1, -1, -1)))
or as fixed range like this:
for n in range(1,5):
print(*range(1, n+1),(*range(n-1, -1, -1))
I can get the latter to work as far as this, but I'd like it without the 0's. Two additional issues: 1) Will user input of 5 be the same as hard coded 5 in the range? 2) How to get the 5 included in output? Any idea how this can be done? Thanks in advance.
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
python range reverse
I'd like to print the output as a palindromic triangle of positive numbers (without 0's), either through user input int(input()):
for i in range(1,int(input())+1):
print(*range(1, i+1),(*range(i-1, -1, -1)))
or as fixed range like this:
for n in range(1,5):
print(*range(1, n+1),(*range(n-1, -1, -1))
I can get the latter to work as far as this, but I'd like it without the 0's. Two additional issues: 1) Will user input of 5 be the same as hard coded 5 in the range? 2) How to get the 5 included in output? Any idea how this can be done? Thanks in advance.
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
python range reverse
python range reverse
asked Nov 11 at 1:44
gurlinthewurld
33
33
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42
add a comment |
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Your indexing is just ever so slightly off:
user_range = int(input("Desired limit : ")) + 1
for n in range(1, user_range):
print(*range(1, n+1),(*range(n-1, 0, -1)))
output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you haverange(x, y, z)orrange(x, y), the ending pointyof the iterator is not inclusive. If set to-1the iteration will stop at 0, and if set to0, the iteration will stop at1:)
– LeKhan9
Nov 11 at 2:49
add a comment |
up vote
0
down vote
Is this what you want? Rather than hardcoding 5 we are asking the input from the user. And a slight modification to the indexing as well.
for i in range(1,int(input("Enter number : "))+1):
print(*range(1,i+1), *range(i-1,-1,-1))
Ouput:
Enter number : 7
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1 0
1 2 3 4 5 6 5 4 3 2 1 0
1 2 3 4 5 6 7 6 5 4 3 2 1 0
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
add a comment |
up vote
0
down vote
Nice fancy work with those ranges and splats. I don't see anything wrong with slicing.
for n in range(1,5):
print(*range(1, n+1), *range(n-1, -1, -1)[:-1])
Somebody else said this and it seems to work the same:
for n in range(1,5):
print(*range(1, n+1), *range(n-1, 0, -1))
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:*range(n-1, -1, -1)[:-1]simply cuts off everything after the-1nth (the last) position in the range.
– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Your indexing is just ever so slightly off:
user_range = int(input("Desired limit : ")) + 1
for n in range(1, user_range):
print(*range(1, n+1),(*range(n-1, 0, -1)))
output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you haverange(x, y, z)orrange(x, y), the ending pointyof the iterator is not inclusive. If set to-1the iteration will stop at 0, and if set to0, the iteration will stop at1:)
– LeKhan9
Nov 11 at 2:49
add a comment |
up vote
4
down vote
accepted
Your indexing is just ever so slightly off:
user_range = int(input("Desired limit : ")) + 1
for n in range(1, user_range):
print(*range(1, n+1),(*range(n-1, 0, -1)))
output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you haverange(x, y, z)orrange(x, y), the ending pointyof the iterator is not inclusive. If set to-1the iteration will stop at 0, and if set to0, the iteration will stop at1:)
– LeKhan9
Nov 11 at 2:49
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Your indexing is just ever so slightly off:
user_range = int(input("Desired limit : ")) + 1
for n in range(1, user_range):
print(*range(1, n+1),(*range(n-1, 0, -1)))
output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Your indexing is just ever so slightly off:
user_range = int(input("Desired limit : ")) + 1
for n in range(1, user_range):
print(*range(1, n+1),(*range(n-1, 0, -1)))
output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
answered Nov 11 at 1:52
LeKhan9
913111
913111
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you haverange(x, y, z)orrange(x, y), the ending pointyof the iterator is not inclusive. If set to-1the iteration will stop at 0, and if set to0, the iteration will stop at1:)
– LeKhan9
Nov 11 at 2:49
add a comment |
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you haverange(x, y, z)orrange(x, y), the ending pointyof the iterator is not inclusive. If set to-1the iteration will stop at 0, and if set to0, the iteration will stop at1:)
– LeKhan9
Nov 11 at 2:49
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
Very cool, thank you. Can you elaborate on the arguments passed in the print's second range: it seems that zero made the difference.
– gurlinthewurld
Nov 11 at 2:33
np! In Python when you have
range(x, y, z) or range(x, y), the ending point y of the iterator is not inclusive. If set to -1 the iteration will stop at 0, and if set to 0, the iteration will stop at 1 :)– LeKhan9
Nov 11 at 2:49
np! In Python when you have
range(x, y, z) or range(x, y), the ending point y of the iterator is not inclusive. If set to -1 the iteration will stop at 0, and if set to 0, the iteration will stop at 1 :)– LeKhan9
Nov 11 at 2:49
add a comment |
up vote
0
down vote
Is this what you want? Rather than hardcoding 5 we are asking the input from the user. And a slight modification to the indexing as well.
for i in range(1,int(input("Enter number : "))+1):
print(*range(1,i+1), *range(i-1,-1,-1))
Ouput:
Enter number : 7
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1 0
1 2 3 4 5 6 5 4 3 2 1 0
1 2 3 4 5 6 7 6 5 4 3 2 1 0
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
add a comment |
up vote
0
down vote
Is this what you want? Rather than hardcoding 5 we are asking the input from the user. And a slight modification to the indexing as well.
for i in range(1,int(input("Enter number : "))+1):
print(*range(1,i+1), *range(i-1,-1,-1))
Ouput:
Enter number : 7
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1 0
1 2 3 4 5 6 5 4 3 2 1 0
1 2 3 4 5 6 7 6 5 4 3 2 1 0
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
add a comment |
up vote
0
down vote
up vote
0
down vote
Is this what you want? Rather than hardcoding 5 we are asking the input from the user. And a slight modification to the indexing as well.
for i in range(1,int(input("Enter number : "))+1):
print(*range(1,i+1), *range(i-1,-1,-1))
Ouput:
Enter number : 7
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1 0
1 2 3 4 5 6 5 4 3 2 1 0
1 2 3 4 5 6 7 6 5 4 3 2 1 0
Is this what you want? Rather than hardcoding 5 we are asking the input from the user. And a slight modification to the indexing as well.
for i in range(1,int(input("Enter number : "))+1):
print(*range(1,i+1), *range(i-1,-1,-1))
Ouput:
Enter number : 7
1 0
1 2 1 0
1 2 3 2 1 0
1 2 3 4 3 2 1 0
1 2 3 4 5 4 3 2 1 0
1 2 3 4 5 6 5 4 3 2 1 0
1 2 3 4 5 6 7 6 5 4 3 2 1 0
answered Nov 11 at 1:52
Sanchit Kumar
3107
3107
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
add a comment |
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
– gurlinthewurld
Nov 11 at 2:36
add a comment |
up vote
0
down vote
Nice fancy work with those ranges and splats. I don't see anything wrong with slicing.
for n in range(1,5):
print(*range(1, n+1), *range(n-1, -1, -1)[:-1])
Somebody else said this and it seems to work the same:
for n in range(1,5):
print(*range(1, n+1), *range(n-1, 0, -1))
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:*range(n-1, -1, -1)[:-1]simply cuts off everything after the-1nth (the last) position in the range.
– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
add a comment |
up vote
0
down vote
Nice fancy work with those ranges and splats. I don't see anything wrong with slicing.
for n in range(1,5):
print(*range(1, n+1), *range(n-1, -1, -1)[:-1])
Somebody else said this and it seems to work the same:
for n in range(1,5):
print(*range(1, n+1), *range(n-1, 0, -1))
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:*range(n-1, -1, -1)[:-1]simply cuts off everything after the-1nth (the last) position in the range.
– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
add a comment |
up vote
0
down vote
up vote
0
down vote
Nice fancy work with those ranges and splats. I don't see anything wrong with slicing.
for n in range(1,5):
print(*range(1, n+1), *range(n-1, -1, -1)[:-1])
Somebody else said this and it seems to work the same:
for n in range(1,5):
print(*range(1, n+1), *range(n-1, 0, -1))
Nice fancy work with those ranges and splats. I don't see anything wrong with slicing.
for n in range(1,5):
print(*range(1, n+1), *range(n-1, -1, -1)[:-1])
Somebody else said this and it seems to work the same:
for n in range(1,5):
print(*range(1, n+1), *range(n-1, 0, -1))
answered Nov 11 at 1:53
Charles Landau
1,0951211
1,0951211
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:*range(n-1, -1, -1)[:-1]simply cuts off everything after the-1nth (the last) position in the range.
– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
add a comment |
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:*range(n-1, -1, -1)[:-1]simply cuts off everything after the-1nth (the last) position in the range.
– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
Thanks, I missed that slicing part, if you could elaborate.
– gurlinthewurld
Nov 11 at 2:35
In my example:
*range(n-1, -1, -1)[:-1] simply cuts off everything after the -1nth (the last) position in the range.– Charles Landau
Nov 11 at 2:36
In my example:
*range(n-1, -1, -1)[:-1] simply cuts off everything after the -1nth (the last) position in the range.– Charles Landau
Nov 11 at 2:36
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
Thanks. I see that the results only get to 4 even though the *range(1,n+1) should make it all inclusive, and so the user input is the ideal way to go.
– gurlinthewurld
Nov 11 at 2:49
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245139%2fprint-range-of-positive-integers-ascending-and-reverse-order-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In case anyone is wondering about the syntax and the purpose of the asterisk, the Python docs explain: "If the syntax *expression appears in the function call, expression must evaluate to an iterable." docs.python.org/3/reference/expressions.html#calls
– gurlinthewurld
Nov 11 at 14:42