print[*range[1, int[input[]]+1], sep='']
This was the code I found in a discussion on hacker rank. But I did not understand it. Can anyone please explain this?
asked Nov 4, 2021 at 6:58
So, from what I can tell, this is how it works:
int[input[]]
This takes input from the user. Next;
range[1,...+1]
This creates a range from 1 to our number we inputted earlier. The +1 means that it will include the max number. And then:
print[*...,sep='']
The *
sign, from what I can tell, just effectively returns each value in our range one to be printed. The sep=''
just means each value is separated by ''
or nothing.
Hope this is useful to you.
[EDIT]
More on star and double star expressions in this post: What does the star and doublestar operator mean in a function call?
answered Nov 4, 2021 at 7:05
2
Ok so we have print
function. Inside we have this strange *range[1, int[input[]]+1]
this is range function which return value from 1 to n [n is typed in input
] in a form of a range object. *
unpack this object to form like: 1 2 3 4 ...
with spaces, so we have this sep='', keyword argument that makes separation from space to '' [no separate between].
Also you can do it like that:
n = input["Type integer value: "]
try:
[print[x+1,end=""] for x in range[int[n]]]
except ValueError:
exit["Typed string not number"]
answered Nov 4, 2021 at 7:11
1
Python Basic: Exercise-50 with Solution
Write a Python program to print without newline or space.
Sample Solution-1:
Python Code:
for i in range[0, 10]:
print['*', end=""]
print["\n"]
Sample Output:
**********
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
import functools
printf = functools.partial[print, end=""]
for i in range[0, 10]:
printf['*']
i = 0
Sample Output:
**********
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-3:
Python Code:
i=0;
while i>> import numpy as np
>>> res = list[np.cumsum[range[0,10,2]]]
>>> res
[ 0, 2, 6, 12, 20]