Print range of positive integers ascending and reverse order - Python











up vote
0
down vote

favorite
1












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









share|improve this question






















  • 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















up vote
0
down vote

favorite
1












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









share|improve this question






















  • 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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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





share|improve this answer





















  • 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




















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





share|improve this answer





















  • Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
    – gurlinthewurld
    Nov 11 at 2:36


















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))





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer





















  • 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

















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





share|improve this answer





















  • 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















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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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 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




















  • 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


















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














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





share|improve this answer





















  • Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
    – gurlinthewurld
    Nov 11 at 2:36















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





share|improve this answer





















  • Thank you, yes ideally prefer to have user input, but keep the range in positive numbers.
    – gurlinthewurld
    Nov 11 at 2:36













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










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))





share|improve this answer





















  • 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















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))





share|improve this answer





















  • 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













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))





share|improve this answer












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))






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly