Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-30 Thread Steven D'Aprano
On Fri, Apr 29, 2016 at 07:05:27PM -0400, Ken G. wrote:

> Martin: I have been using Python2 for several years now and I have yet 
> been able to change over to Python3. I am not together sure if Python3 
> is now more stable to use and more commonly use. If so, I will gradually 
> change over but do realize there is a learning curve to learn. It will 
> be slowly done but sooner or later, I will be using Python3. I wonder if 
> there is a script to translate my Python2 programs to Python3 format. 

Python 3 is not only stable, it has been stable since version 3.1 about 
five years ago. (Unfortunately, version 3.0 turned out to be broken and 
should never be used.) We're now up to Python 3.5, with 3.6 about to 
come out soon.

Python 2 comes with a script called "2to3" which will take a Python 2 
script and update it to Python 3:

https://docs.python.org/2/library/2to3.html

Make sure you have backups!


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread meenu ravi
That's actually two single quotes:-) both single and double quotes should
work.

Thanks,
Meena
On Apr 29, 2016 5:58 PM, "Ken G."  wrote:

>
> On Fri, Apr 29, 2016 at 3:01 PM, Ken G.  wrote:
>
>> In entering five random number, how can I best sort
>> it into ascending order, such as 0511414453? Using
>> Linux 2.7.6 in Ubuntu 14.04.4. Thanks.
>>
>> number01 = "41"
>> number02 = "11"
>> number03 = "05"
>> number04 = "53"
>> number05 = "44"
>> line = number01 + number02 + number03 + number04 + number05
>> print
>> print line
>> line.sort()
>> print
>> print line
>>
>>
>> 4111055344
>>
>> Traceback (most recent call last):
>>   File "Mega_Millions_01_Tickets_Entry_TEST_TEST.py", line 11, in 
>> print line.sort()
>>
>> AttributeError: 'str' object has no attribute 'sort'
>>
>
> On 04/29/2016 04:58 PM, meenu ravi wrote:
>
> Hi Ken,
>
> As the message clearly says, the string object doesn't have an attribute
> "sort". You are trying to join the inputs as a single string,"line" and
> then you are trying to sort it. But, as you want the string in the sorted
> format, you should sort it first and then convert it into string, as
> follows.
>
> number01 = "41"
> number02 = "11"
> number03 = "05"
> number04 = "53"
> number05 = "44"
> list1 = [number01,number02,number03,number04,number05] # Creating a list
> with the inputs
>
> list1.sort() # sorting the input
> print ''.join(list1) #making it again as a single string
>
> Hope this helps. Happy coding.
>
> Thanks,
> Meena
>
>
> Thanks, Meena, that is great! I changed your last line to:
>
> print "".join(list1)  and it came out as below:
>
> 0511414453
>
> Another quotation mark was needed.
>
> Again, thanks.
>
> Ken
>
>
>
>
> I appreciate all of everybody help.
>
> Ken
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.

On 04/29/2016 07:40 PM, Alan Gauld via Tutor wrote:

On 29/04/16 23:58, Ken G. wrote:


print ''.join(list1) #making it again as a single string


Thanks, Meena, that is great! I changed your last line to:

  print "".join(list1)  and it came out as below:

  0511414453

Another quotation mark was needed.

No it wasn't. Meena used two single quotes, you used
two double quotes. Both will work. But the two single
quotes can look like a single double quote depending
on your font choices. So double quotes in this case
are probably less ambiguous.


Gosh, been using double quotes since the late eighties and thus, a hard 
habit

to break when encourage to use single quote mark in Python. Thanks for the
reminder of "it" being a possible double single quote when encountering a
double quote at various times.

Ken

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Alan Gauld via Tutor
On 30/04/16 00:05, Ken G. wrote:

> been able to change over to Python3. I am not together sure if Python3 
> is now more stable to use and more commonly use. 

It is definitely stable and most libraries are now converted (although a
few remain v2 only). Availability of libraries is now the only real
reason to stick with v2 IMHO.

> change over but do realize there is a learning curve to learn. 

The main learning is in the libraries rather than the core language.
Quite a lot of modules got changed/moved/renamed etc. Also the
return values of a lot of functions changed to iterators and the like.
Those are the things that tend to catch you out, not the core language.

> there is a script to translate my Python2 programs to Python3 format. 

Yes there is, although it's not foolproof, you often have to manually
tweak things. It comes with v3 in, I think, the scripts folder.

There is also something called six that you should investigate
if you have a significant amount of v2 code to manage alongside v3.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Alan Gauld via Tutor
On 29/04/16 23:58, Ken G. wrote:

>> print ''.join(list1) #making it again as a single string
>>
> 
> Thanks, Meena, that is great! I changed your last line to:
> 
>  print "".join(list1)  and it came out as below:
> 
>  0511414453
> 
> Another quotation mark was needed.

No it wasn't. Meena used two single quotes, you used
two double quotes. Both will work. But the two single
quotes can look like a single double quote depending
on your font choices. So double quotes in this case
are probably less ambiguous.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.



On 04/29/2016 05:10 PM, Martin A. Brown wrote:

Greetings Ken and welcome to Python,


Using Linux 2.7.6 in Ubuntu 14.04.4. Thanks.

Thank you for this information.  I have one tip for you:  While
Python 2.x will still be around for a while, if you are learning
Python today, I'd suggest Python 3.x. You can read more about the
differences online (or ask here), if you care.  The only syntax
difference that usually trips up beginners is the following:

   print line # Python 2.x
   print(line)# Python 3.x

Though there are other differences, this is one of the most obvious
to all Python programmers.  With that said, I'll just answer your
question using Python 2.x, since everything else in your example
will work perfectly the same in either version.

I intend to suggest what you should read, and then what you should
try to run in order to address your question(s).

I will paste my entire interactive Python sessions below.  Did you
know that you can use the Python interpreter as a shell to test out
how things behave?  Very handy.  Try typing 'python' at the CLI and
you should see this:

   $ python
   Python 2.7.8 (default, Sep 30 2014, 15:34:38) [GCC] on linux2
   Type "help", "copyright", "credits" or "license" for more information.
   >>>

So, when you see ">>>" below, that's in the Python shell (2.7.8 in
my case, 2.7.6 in yours).


In entering five random number, how can I best sort it into
ascending order, such as 0511414453?

OK, so you have five random numbers, but you seem to want to print
them without any visual space between them.  A bit strange, but OK!

Fragment #1:


number01 = "41"
number02 = "11"
number03 = "05"
number04 = "53"
number05 = "44"

Observation:  You define a variable called 'number01', which
actually contains a string value.  You can call the variable
anything you want, but that will not change the type of the object
to which your variable name is bound.  For example:

   >>> number01 = "41"
   >>> type(number01)
   

Suggestion #1, try this instead:

   >>> number01 = 41
   >>> type(number01)
   

Ah-ha!  Now, we are dealing with integers (numbers).

Fragment #2:


line = number01 + number02 + number03 + number04 + number05

Observation:  Since all of the variables (named number01, number02,
number03, etc) contain strings, you are using string
concatenation to create a new string.  After the above command
executes, you have this:

   >>> line
   '4111055344'
   >>> type(line)
   

Comment:  You don't really want a string, do you?  Keep reading for
a moment, and I'll come back to this.

Also, I don't think you really want to add the integers.  That would
be 41 + 11 + 05 + 53 + 44 = 154.  But, you want to keep the numbers
and sort them.  We will need to keep them in a list.  And, Python
has a data structure for you...called, obviously enough list().

Fragment #3:


print
print line
line.sort()

Traceback (most recent call last):
  File "Mega_Millions_01_Tickets_Entry_TEST_TEST.py", line 11, in 
print line.sort()

AttributeError: 'str' object has no attribute 'sort'

Observation:  This message is called an Exception, in Python.
There are many different kinds of exceptions--this one is called
AttributeError, but the message of the exception is the important
part for us here.  It doesn't make sense to sort a string.  You can
sort two strings, of course.  But not a single string.

Anyway, the important thing about the Exception is it tries to give
you a good deal of information about which line of code did
something that was problematic, and what code called that code (this
is why it's called a "Traceback").

Now, I'll rewind to the beginning and try to go through the problem
again, changing a few pieces of your program to get you closer to
your solution.

First, let's use the right datatype, an int, for each number.
Second, let's use a list() to store the numbers (instead of five
   separate named variables; easy to mistype names).
Third, let's sort it.
Then, let's print it.

   >>> nums = [41, 11, 5, 53, 44]  # -- create/populate a list
   >>> nums
   [41, 11, 5, 53, 44] # -- still looks good
   >>> nums.sort() # -- sort, in place!
   >>> nums
   [5, 11, 41, 44, 53] # -- Ta-da!

I'm going to do the same thing a slightly different way, now, so you
can see how to add stuff to a list:

   >>> nums = list()
   >>> nums.append(41)
   >>> nums.append(11)
   >>> nums.append(5)
   >>> nums.append(53)
   >>> nums.append(44)
   >>> nums
   [41, 11, 5, 53, 44]

In each case the variable 'nums' still contains the same data.  We
just got there in a different series of steps.  You may decide which
makes more sense to you in any given situation.

So, suggestion, play with lists, and see how they work.

Now, we have to figure out how to print the contents of the list.
Here's one way, but it is not the output you appear to want.

   >>> print nums
   [5, 11, 41, 44, 53]

I'm guessing you want '05 11 41 44 53'.

There are many ways to convert fro

Re: [Tutor] Sorting a list in ascending order [RESOLVED]

2016-04-29 Thread Ken G.


On Fri, Apr 29, 2016 at 3:01 PM, Ken G. > wrote:


In entering five random number, how can I best sort
it into ascending order, such as 0511414453? Using
Linux 2.7.6 in Ubuntu 14.04.4. Thanks.

number01 = "41"
number02 = "11"
number03 = "05"
number04 = "53"
number05 = "44"
line = number01 + number02 + number03 + number04 + number05
print
print line
line.sort()
print
print line


4111055344

Traceback (most recent call last):
  File "Mega_Millions_01_Tickets_Entry_TEST_TEST.py", line 11, in

print line.sort()

AttributeError: 'str' object has no attribute 'sort'



On 04/29/2016 04:58 PM, meenu ravi wrote:

Hi Ken,

As the message clearly says, the string object doesn't have an 
attribute "sort". You are trying to join the inputs as a single 
string,"line" and then you are trying to sort it. But, as you want the 
string in the sorted format, you should sort it first and then convert 
it into string, as follows.


number01 = "41"
number02 = "11"
number03 = "05"
number04 = "53"
number05 = "44"
list1 = [number01,number02,number03,number04,number05] # Creating a 
list with the inputs


list1.sort() # sorting the input
print ''.join(list1) #making it again as a single string

Hope this helps. Happy coding.

Thanks,
Meena


Thanks, Meena, that is great! I changed your last line to:

print "".join(list1)  and it came out as below:

0511414453

Another quotation mark was needed.

Again, thanks.

Ken




I appreciate all of everybody help.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor