Python Forum
[split] help me make this code better please (basic) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: [split] help me make this code better please (basic) (/thread-27740.html)



[split] help me make this code better please (basic) - Rustam - Jun-19-2020

I'm just study Python and fall in very stupid situation
This code every time have different results, looks like if range() returns random result in range:
l = {str(i)+str(i-1) for i in range(20)}
print(l)
Output:
================= RESTART: D:\My Files\Documents\Python\new.py ================= {'10', '1817', '32', '109', '43', '76', '1413', '1110', '21', '1514', '65', '1716', '1918', '87', '1211', '54', '1312', '98', '0-1', '1615'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'65', '98', '1817', '1716', '1211', '10', '109', '1312', '76', '21', '87', '1110', '1918', '1413', '54', '1514', '43', '32', '0-1', '1615'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'1312', '76', '0-1', '98', '65', '1918', '1413', '21', '1817', '32', '87', '1110', '43', '1615', '1716', '109', '1211', '1514', '54', '10'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'1312', '87', '54', '21', '1211', '1716', '1918', '1514', '76', '43', '109', '1413', '0-1', '65', '1110', '1817', '32', '98', '1615', '10'} >>>



RE: [split] help me make this code better please (basic) - buran - Jun-19-2020

what you create is set (i.e. that is because you use curly braces { and }). sets are unordered. That's why the elements are the same, but order in which they are print is different.
use brackets [ and ] to create list or ( and ) to create tuple.

Also don't use names like l, o - they are discouraged because it's easy to confuse with 1 and 0


RE: [split] help me make this code better please (basic) - Rustam - Jun-19-2020

(Jun-19-2020, 12:00 PM)buran Wrote: what you create is set (i.e. that is because you use curly braces { and }). sets are unordered. That's why the elements are the same, but order in which they are print is different.
use brackets [ and ] to create list or ( and ) to create tuple.

Also don't use names like l, o - they are discouraged because it's easy to confuse with 1 and 0

Thanks, I will consider your comments for the future