Python 'print' function 'sep' parameter is not working as expected [duplicate] - python

This question already has answers here:
Printing an int list in a single line python3
(11 answers)
Closed 3 years ago.
I am using Python version 3.7.4 on Windows 10 Enterprise.
I am facing a weird issue with Python's print function and especially sep parameter.
In Python REPL, when I use code print(1, 2, 3, 4, 5, sep='\t'), I get proper output as 1 2 3 4 5
However when code tries to iterate over a collection as shown below, instead of showing number separated by a tab, it always displays individual value on a new line.
numbers = {1, 2, 3, 4, 5}
for n in numbers:
print(n, sep='\t')
Can someone please help me to understand why its displaying number value on a separate line?
I have attached screenshot for the reference.
Thanks.

It is because you are iterating over the set numbers. The loop runs and the print() function automatically inserts a \n at the end of each item it prints out for display. Hence each item is being displayed on its own line.
If you wanted to loop through an iterable like in your example and have it separated by a tab, then you can do the following:
for n in numbers:
print(n, end='\t')
By default, the end argument is set to \n.

Related

Overwriting printed statements in the IDLE in Python [duplicate]

This question already has answers here:
How to output to the same line overwriting the previous line?
(4 answers)
Closed 3 years ago.
I am looking for a method so that when something is printed continuously, it overwrite what the previous printed statement says. for example, if I am making a counter, normally the outcome in the IDLE would be: 1 2 3 4...., but however I'm looking to rewrite/overwrite what the previous printed statement says so it say "1" for a second then "2" appears but we can no longer see "1". Any suggestions? Sorry about how wordy this question is, I was having a hard time trying to write this where another person understands.
import time
arr = [1,2,3,4]
for element in arr:
print(element, end='\r')
time.sleep(1)
end='\r' in the print statement does the trick

How to delete the last character in a string in python 3 [duplicate]

This question already has answers here:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.

How to make a list of multiples and use a for loop to print in Python3 [duplicate]

This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
The question in my book I am working through (I am new to programming) is:
Threes: Make a list of the multiples of 3 from 3 to 30. Use a for loop to print the numbers in your list.
I have tried a few different things to do this, but in my book Python Crash Course, it doesn't explain the syntax or show me examples on how to do multiples. Only exponents. I have reread the chapter several times over and still am not able to find the tutorial on how to do this. And also, being that I am new to programming, I don't exactly know the keywords or phrases I should be searching for.
It would be of great help to me (I've been confused by this for over an hour) if someone could explain this to me and give me an example.
It's easy, you can use the range function to iterate over a sequence of numbers and then create the list by examining the result of value % 3 (modulus 3). If it is zero, you got a multiple, if not, you don't:
# Create an empty list
l = []
# 31 because the end of the range is exclusive
for i in range(3, 31):
# if equal to zero it is a multiple of 3
if i % 3 == 0:
# add it to the list
l.append(i)
This can be mushed into a single line called a comprehension:
l = [i for i in range(3, 31) if i % 3 == 0]
As for printing, you can tackle it, you use a similar for loop through the list l created and then use the print function!
Since you're new to the language, go over to the Python homepage and read the official tutorial on the language, it is nicely written and will help you much more than any answer can.

Print all elements of a dictionary? (It gets cut off at 1000 in my jupyter notebook) [duplicate]

This question already has answers here:
IPython Notebook output cell is truncating contents of my list
(9 answers)
Closed 6 years ago.
I have a dictionary with around 1200 key-valuepairs. I want to be able to go through all of them to check the values, but it cuts off at 1000 (and ends on ...}).
I expect there some setting somewhere to print all of them? I'm doing this in jupyter notebook using python 2.7.
I'm doing Flux Balance Analysis on a model, and the goal is to identify fluxes that does not work - but I find that the value of these fluxes are not necessarily 0, but can simply be an extremely low number.
EDIT: I've just been typing the dict directly into the command line. The function that I used to generate the dict also returned it by default, and that got cut off as well.
Using print str(dict) works... But then it becomes unreadable because it's all on one line instead of displaying one key-value pair per line.
Iterating over it with a 'for i in dict, print i, dict[i]' worked though, thanks!
I think the answer you're looking for is available in this old question here: IPython Notebook output cell is truncating contents of my list

Does the Python range generator generate all values or yields them progressively? [duplicate]

This question already has answers here:
Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?
(12 answers)
Closed 7 years ago.
When you do something like this:
for i in range(5):
print i
What does Python do? Does it first generate an array with [0,1,2,3,4] and then goes over each item printing it?
Similar to:
for i in [0,1,2,3,4]:
print i
Or does it print each number as it generates them? Something like:
Generate 0 assign 0 to i print i
Generate 1 -> assign 1 to i -> print i
Generate 2 -> assign 2 to i -> print i
Generate 3 -> assign 3 to i -> print i
Generate 4 -> assign 4 to i -> print i
Update
I have added the tag for Python 2.7. I didn't think my question was version specific, but it seems it is!
Right now I am working with Python 2.7 so my question refers to that. But I find very valuable the information comparing Python 2 range against Python 3 range.
In Python 2, range() returns the whole list object then that list
object is iterated by for.
In Python 3, it returns a memory efficient iterable, which is an
object of its own with dedicated logic and methods, not a list. Now
for will get each value as it is generated by that iterable.
In Python 2 there is xrange() to get something like what Python 3's
range() do. Python 2's xrange is kind of halfway between Python 2's
range and Python 3's range. It's better than the former but not as
good as the latter.
Finally, in Python 3, if you want the whole list do:
>>> list(range(...))
In Python 2, it returns a list of the values. Try this to see
print range(4)
In Python 3, it returns an iterable object. You can convert it to list using this:
print(list(range(4))

Categories