Re: [Tutor] List of ints

2015-03-03 Thread Alan Gauld

On 03/03/15 06:50, Phil wrote:


I'd like to set up a two dimensional list of counters as follows;

count = [ [0], [0], [0] ]

And then increment the first counter as follows;

count [0] += 1


Are you trying to increment the zero to make it 1?
Or are you trying to add a new value, 1, to the first sublist?
ie Do you want the output to be:

count = [ [1], [0], [0] ]

OR

count = [ [0,1], [0], [0] ]

To do the first you need to increment the *value* of
the first sublist not the sublist:

count[0][0] += 1

To do the second you must use append():

count[0].append(1)


The array module looks like the answer because it seems to function in
the same way as an array under C.


The array module is pretty specialized, in most cases a simple
list is a better solution.


Is there a way to add a value to a list of ints?


Yes, see above.

--
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] List of ints

2015-03-03 Thread Danny Yoo
On Mon, Mar 2, 2015 at 10:50 PM, Phil  wrote:
> Thank you for reading this.
> Python 3 under Linux.
>
> I'd like to set up a two dimensional list of counters as follows;
>
> count = [
> [0],
> [0],
> [0]
> ]
>


Can you explain why the list is two-dimensional?  It's not quite clear
why.  Do you have a particular use case in mind?



> Is there a way to add a value to a list of ints?

Can you give an example of what you'd like to see?  Unfortunately, the
word "add" is too ambiguous to know what the expectations are.  If you
can disambiguate with concrete examples, that may help us.


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


[Tutor] (no subject)

2015-03-03 Thread chelseysp

don't understand why these execute different things…




 total=total+10
>>> total=total+25
>>> total=total+25
>>> average=total/3
>>> total
110
>>> average
36.664




and 







total=0
>>> total=total+10
>>> total=total+25
>>> total=total+25
>>> average=total/3
>>> average
20.0






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


Re: [Tutor] How to test a class in python?

2015-03-03 Thread Marcos Almeida Azevedo
On Mon, Mar 2, 2015 at 12:41 PM, Danny Yoo  wrote:

> On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld 
> wrote:
> > On 01/03/15 16:19, Fatimah Taghdi wrote:
> >>
> >> Hello I was wondering how to test a class in python is it the same way
> as
> >> testing a function ?
>
>
> Depending on the design of the class, there might be a little bit more
> set-up involved.  But the ideas are similar.
>
>
> In a test case for a function, we have a few things in hand:
>
>1.  The function being tested.
>2.  The parameters we're going to pass as inputs to that function.
>3.  The expected output we want to see if the function is behaving
> properly.
>
>
> In a test case for a class, we have a few more things in hand.
>
>1.  The class being tested.
>2.  The parameters we use to create the class instance.
>3.  The particular method of the class that we're testing.
>4.  The parameters we're going to pass as inputs to that method.
>5.  The expected output we want to see if the method is behaving
> properly.
>
> So you can see that the core concept for testing is the same: we
> express the things that are going to change, and we also say what the
> expected result is supposed to be.  It just so happens that classes
> can have a lot more state, and this can contribute to a few extra
> steps to do the test set up..
>

I suggest PyUnit for easier testing.
Here is a very good guide: http://pyunit.sourceforge.net/pyunit.html



>
>
> Do you have a particular class in mind?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Marcos | I love PHP, Linux, and Java

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


Re: [Tutor] (no subject)

2015-03-03 Thread Danny Yoo
On Mon, Mar 2, 2015 at 10:22 PM,   wrote:
>
> don't understand why these execute different things…
>
>
>
>
>  total=total+10
 total=total+25
 total=total+25
 average=total/3
 total
> 110
 average
> 36.664


In your first case, what's the value of 'total' *before* all the
things you've typed in?

Can you say more why you expected to see the same result here as in
the next program?  They do look textually different, so I'd expect
them to likely have a different meaning, so I'm confused as to what
your expectations are.  Say more about why you expected them to be the
same, and maybe we can understand better.





> total=0
 total=total+10
 total=total+25
 total=total+25
 average=total/3
 average
> 20.0

Your second case makes sense.  10 + 25 + 25 is 60, and if we divide 60
by 3, the we'd expect the average to be twenty.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] codecs.open vs io.open

2015-03-03 Thread Albert-Jan Roskam
Hi,

Is there in a (use case) difference between codecs.open and io.open? What is 
the difference?
A small difference that I just discovered is that codecs.open(somefile).read() 
returns a bytestring if no encoding is specified, but a unicode string if an 
encoding is specified. io.open always returns a unicode string.

Thank you!

Regards,

Albert-Jan



~~

All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 

fresh water system, and public health, what have the Romans ever done for us?

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


Re: [Tutor] How to test a class in python?

2015-03-03 Thread Mark Lawrence

On 03/03/2015 06:25, Marcos Almeida Azevedo wrote:

On Mon, Mar 2, 2015 at 12:41 PM, Danny Yoo  wrote:


On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld 
wrote:

On 01/03/15 16:19, Fatimah Taghdi wrote:


Hello I was wondering how to test a class in python is it the same way

as

testing a function ?



Depending on the design of the class, there might be a little bit more
set-up involved.  But the ideas are similar.


In a test case for a function, we have a few things in hand:

1.  The function being tested.
2.  The parameters we're going to pass as inputs to that function.
3.  The expected output we want to see if the function is behaving
properly.


In a test case for a class, we have a few more things in hand.

1.  The class being tested.
2.  The parameters we use to create the class instance.
3.  The particular method of the class that we're testing.
4.  The parameters we're going to pass as inputs to that method.
5.  The expected output we want to see if the method is behaving
properly.

So you can see that the core concept for testing is the same: we
express the things that are going to change, and we also say what the
expected result is supposed to be.  It just so happens that classes
can have a lot more state, and this can contribute to a few extra
steps to do the test set up..



I suggest PyUnit for easier testing.
Here is a very good guide: http://pyunit.sourceforge.net/pyunit.html



Quoting from the link "PyUnit forms a part of the Python Standard 
Library as of Python version 2.1." so for the latest version see 
https://docs.python.org/3/library/unittest.html


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] codecs.open vs io.open

2015-03-03 Thread Mark Lawrence

On 03/03/2015 10:31, Albert-Jan Roskam wrote:

Hi,

Is there in a (use case) difference between codecs.open and io.open? What is 
the difference?
A small difference that I just discovered is that codecs.open(somefile).read() 
returns a bytestring if no encoding is specified, but a unicode string if an 
encoding is specified. io.open always returns a unicode string.

Thank you!

Regards,

Albert-Jan



You might get an answer here if you're lucky, but I believe this is more 
python-list territory.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] text file help

2015-03-03 Thread Alan Gauld

On 03/03/15 12:56, Tihomir Zjajic wrote:

kl_number = []
myfile = open("formular_doznake.txt")
for line in myfile:
   if line.startswith('1'):
   num = makeNumber(next[vrs_drv], next[prs_prec], next[teh_kl])
   kl_number.append(num)

def makeNumber(l1,l2,l3):
   nums = []
   for line in(vrs_drv,prs_prec,teh_kl):
   nums.append(line.rstrip().split()[-1])
   return ".join(nums)"



print(next(myfile) + next(myfile) + next(myfile))
print(kl_number)


Do you have any idea what these two lines do?
If not you need to go and read about next() and iterators.

It is obviously never going to work to try to read the next
line from a file that has already been processed.

I don't know what you think the lines will do but they
obviously throw an error. Its like trying to climb a ladder
when you ae already at the top.

I don't know what your logic is here, only you can answer that.

But you will get better responses asking the whole iist
not just me. I'm only one pair of eyes. The list has dozens.


Traceback (most recent call last):
 File "C:\Python31\pro.py", line 13, in 
   print(next(myfile) + next(myfile) + next(myfile))
StopIteration 


Alan G.

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


Re: [Tutor] (no subject)

2015-03-03 Thread Danny Yoo
On Mar 3, 2015 1:49 PM,  wrote:
>
> i expected them to be the same because 10 plus 25 plus 25 divided by 3 is
60 and they both say that but the one that that says total=0. im just not
sure how that changes the equation
>

Please use "reply to all" in your email client.

You might be getting confused because your mental model is of a formula and
mathematics.  In Python, unfortunately that's not what's happening.
Instead, in a python program, a computation is a sequence of actions that
changes the world.

For example,

total=total+10

In a Python program, this has the physical form of a mathematical equation
, but it's not!  It means the following sequence of steps: "1. Load the
current value of the 'total' variable, 2. add ten to that value, and 3.
store the result into the 'total' variable".

For any of that to work, 'total' must already have some prior numeric value
that you haven't shown us yet.  Math variables don't directly have a notion
of "prior" or change over time.  But Python variables do.

Note how different the meaning of the statement is from an equation of
algebra.  You have to be very careful because the notation used in certain
programming languages looks superficially like math.  Here, the notation is
borrowed, but the meanings are different: programmers have gotten used to
the different meaning of symbols like '='.

There *are* good programming curricula where the programming language
behaves like algebra (see http://bootstrapworld.org for example).  But
unfortunately it's not what you'll be doing in Python.

> Sent from Windows Mail
>
> From: Danny Yoo
> Sent: ‎Tuesday‎, ‎March‎ ‎3‎, ‎2015 ‎3‎:‎45‎ ‎AM
> To: chelse...@yahoo.com.dmarc.invalid
> Cc: Tutor@python.org
>
> On Mon, Mar 2, 2015 at 10:22 PM,  
wrote:
> >
> > don't understand why these execute different things…
> >
> >
> >
> >
> >  total=total+10
>  total=total+25
>  total=total+25
>  average=total/3
>  total
> > 110
>  average
> > 36.664
>
>
> In your first case, what's the value of 'total' *before* all the
> things you've typed in?
>
> Can you say more why you expected to see the same result here as in
> the next program?  They do look textually different, so I'd expect
> them to likely have a different meaning, so I'm confused as to what
> your expectations are.  Say more about why you expected them to be the
> same, and maybe we can understand better.
>
>
>
>
>
> > total=0
>  total=total+10
>  total=total+25
>  total=total+25
>  average=total/3
>  average
> > 20.0
>
> Your second case makes sense.  10 + 25 + 25 is 60, and if we divide 60
> by 3, the we'd expect the average to be twenty.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List of ints

2015-03-03 Thread Phil

On 03/03/15 17:46, Mark Lawrence wrote:


You are trying to increment the first element of count which is itself a
list containing one element.  You actually need:-

count[0][0] +=1



Thank you Lawrence, Alan, and Danny,

The solution is embarrassingly obvious. It's been a long time since I've 
attempted any programming and I'd even forgotten that I needed a nested 
loop to access the cells in a two-dimensional array, or list. In this 
case I didn't need a two-dimensional array anyway.


I'd been away from home for five weeks and during a quiet period I 
installed QPython on my tablet with the aim of porting a programme that 
I'd written in C++ 15 years ago to Python. Cutting and pasting and even 
moving around the IDE turned out to be a truly frustrating exercise.


I wonder if it was just my clumsiness or if others have had the same 
experience?


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


Re: [Tutor] List of ints

2015-03-03 Thread Mark Lawrence

On 03/03/2015 23:09, Phil wrote:

On 03/03/15 17:46, Mark Lawrence wrote:


You are trying to increment the first element of count which is itself a
list containing one element.  You actually need:-

count[0][0] +=1



Thank you Lawrence, Alan, and Danny,

The solution is embarrassingly obvious. It's been a long time since I've
attempted any programming and I'd even forgotten that I needed a nested
loop to access the cells in a two-dimensional array, or list. In this
case I didn't need a two-dimensional array anyway.

I'd been away from home for five weeks and during a quiet period I
installed QPython on my tablet with the aim of porting a programme that
I'd written in C++ 15 years ago to Python. Cutting and pasting and even
moving around the IDE turned out to be a truly frustrating exercise.

I wonder if it was just my clumsiness or if others have had the same
experience?



Having never heard of QPython I've just looked it up, so for those who 
don't know from http://qpython.com/ it's "a script engine which runs 
Python programs on android devices".  I doubt if there is much 
experience on this list with it although you might get lucky.


I am aware though of http://bugs.python.org/issue23496 "Steps for 
Android Native Build of Python 3.4.2" which may be of interest.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] List of ints

2015-03-03 Thread Steven D'Aprano
On Wed, Mar 04, 2015 at 09:09:03AM +1000, Phil wrote:

> I'd been away from home for five weeks and during a quiet period I 
> installed QPython on my tablet with the aim of porting a programme that 
> I'd written in C++ 15 years ago to Python. Cutting and pasting and even 
> moving around the IDE turned out to be a truly frustrating exercise.

I don't actually know QPython, but in general, using a tablet or a smart 
phone is only acceptable for the most trivial actions. Fine if you're 
taping out a 15 character tweet with one finger, not so useful if you 
want to get real work done.



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


Re: [Tutor] List of ints

2015-03-03 Thread Steven D'Aprano
On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote:

> count [0] += 1
> 
> This fails with the following error;
> 
> TypeError: 'int' object is not iterable

I know that others have already solved the problem, but here is 
something which might help you solve similar problems in the future. 
The way to debug simple things like this is quite simple:

print count[0]

which will show you that count[0] is a list [0], not an int 0, and you 
are trying to add [0]+1 which doesn't work.

Never under-estimate the usefulness of a few print calls when debugging.


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