Change print item to:
print item,in Python 2.7print(item, end=" ")in Python 3
If you want to print the data dynamically use following syntax:
print(item, sep=' ', end='', flush=True)in Python 3
Change print item to:
print item,in Python 2.7print(item, end=" ")in Python 3
If you want to print the data dynamically use following syntax:
print(item, sep=' ', end='', flush=True)in Python 3
By the way...... How to refresh it every time so it print mi in one place just change the number.
In general, the way to do that is with terminal control codes. This is a particularly simple case, for which you only need one special character: U+000D CARRIAGE RETURN, which is written '\r' in Python (and many other languages). Here's a complete example based on your code:
from sys import stdout
from time import sleep
for i in range(1,20):
stdout.write("\r%d" % i)
stdout.flush()
sleep(1)
stdout.write("\n") # move the cursor to the next line
Some things about this that may be surprising:
- The
\rgoes at the beginning of the string so that, while the program is running, the cursor will always be after the number. This isn't just cosmetic: some terminal emulators get very confused if you do it the other way around. - If you don't include the last line, then after the program terminates, your shell will print its prompt on top of the number.
- The
stdout.flushis necessary on some systems, or you won't get any output. Other systems may not require it, but it doesn't do any harm.
If you find that this doesn't work, the first thing you should suspect is that your terminal emulator is buggy. The vttest program can help you test it.
You could replace the stdout.write with a print statement but I prefer not to mix print with direct use of file objects.
Python: for loop - print on the same line - Stack Overflow
python - Use one line of code to print a for loop - Stack Overflow
python - Print and for loop in one line - Stack Overflow
Changing output to only one line
Videos
I am new to coding in python and I have created a while loop. How do I make it so that the outputs of each repetition print on the same line like n1,n2,n3,n4... so on so forth. instead of going
n1
n2
n3
print(“n”, end=“,”)
Alternatively...
Instead of printing in the loop, append what you want to print to a list.
Then AFTER the loop, print out the entire list. Something like join() is great for inserting commas without needing to have a special case for "except after the last one".
result = []
for idx in range(10):
result.append("n{}".format(idx))
print(",".join(result))
Use end parameter in the print function
print(new_item, end=" ")
There is another way to do this, using comprehension and join.
print (" ".join([function(word) for word in split]))
The simplest solution is using a comma in your print statement:
>>> for i in range(5):
... print i,
...
0 1 2 3 4
Note that there's no trailing newline; print without arguments after the loop would add it.
As Jon and Patrick mention, that's not an error, it's what happens when you print the __repr__ of a generator expression.
So you just need to "splat" that generator. :)
print(*(i**2 for i in range(int(input()))), sep='\n')
demo output
10
0
1
4
9
16
25
36
49
64
81
In that demo I entered 10 at the input prompt.
In the comments I wondered how the speed of i * i compares to i ** 2. On my old 2GHz 32 bit single core machine, running Python 3.6.0, i * i is around 3 or 4 times faster than i ** 2. Here's some timeit code.
from timeit import Timer
commands = {'mul' : 'num * num', 'pow' : 'num ** 2'}
def time_test(num, loops, reps):
timings = []
setup = 'num = {}'.format(num)
for name, cmd in commands.items():
result = Timer(cmd, setup).repeat(reps, loops)
result.sort()
timings.append((result, name))
timings.sort()
for result, name in timings:
print(name, result)
loops, reps = 100000, 3
num = 1
for _ in range(10):
print('num =', num)
time_test(num, loops, reps)
num <<= 1
output
num = 1
mul [0.02114695899945218, 0.02127135100090527, 0.02303983199817594]
pow [0.08504067399917403, 0.08687452600133838, 0.12349813100081519]
num = 2
mul [0.02089159800016205, 0.021789606998936506, 0.02889108999806922]
pow [0.08612996800002293, 0.09132789800059982, 0.09559987299871864]
num = 4
mul [0.021155500999157084, 0.02333696799905738, 0.028521009000542108]
pow [0.08492234799996368, 0.08499632499660947, 0.08537705599883338]
num = 8
mul [0.02173021600174252, 0.021955170999717666, 0.02823427400289802]
pow [0.08423048700205982, 0.08541251700080466, 0.08654486299928976]
num = 16
mul [0.02176373900147155, 0.02222509399871342, 0.02816650199747528]
pow [0.08528696699795546, 0.09080051600176375, 0.0968476650014054]
num = 32
mul [0.03118283900039387, 0.03388790600001812, 0.03745272100059083]
pow [0.0943321790000482, 0.09484523300125147, 0.09691544299857924]
num = 64
mul [0.030481540998152923, 0.03292956899895216, 0.03887743200175464]
pow [0.09454960600123741, 0.09569520199875114, 0.09926063899911242]
num = 128
mul [0.030935312999645248, 0.031198748001770582, 0.03733277300125337]
pow [0.09531564099961543, 0.09669112700066762, 0.09679062199938926]
num = 256
mul [0.03280377900227904, 0.03324341500047012, 0.04479783699935069]
pow [0.09439349899912486, 0.09439918999851216, 0.09548852000079933]
num = 512
mul [0.03275527599907946, 0.03428718699797173, 0.038492286003020126]
pow [0.10492119499758701, 0.10698100599984173, 0.13057717199990293]
You should wrap the expression into [] in order to have a list comprehension.
print([i**2 for i in range(int(input()))])
If you want to print the results line by line just use extended iterable unpacking operator.
print(*[i**2 for i in range(int(input()))], sep = '\n')