[Tutor] Need help with generators....

2014-02-15 Thread Bunny Sehgal
I am doing "Building Skills in Python". In the Generator Function example
at [url]
http://www.itmaybeahack.com/homepage/books/python/html/p02/p02c08_generators.html#generator-function-example[/url]i
can't understand, how it is working properly.
what i understood is as follows:>
1)First it makes count = 0
2)then it goes straight in for loop
3)Since the first color is red, it goes in else block and increment count
to 1 and stop execution.
4)Now its next method is called using list comprehension, and it resumes
its execution and yield its first value i.e. 1.Note that the yield
statement is outside the for loop.
5)Now there is no further yield statement which can stop execution of
countreds() function.So it raises StopExecution exception

But this is not what is happening. why?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with generators....

2014-02-15 Thread Alan Gauld

On 15/02/14 13:35, Bunny Sehgal wrote:

I am doing "Building Skills in Python". In the Generator Function
example at


I assume you are talking about this one:

spins = [('red', '18'), ('black', '13'), ('red', '7'),
('red', '5'), ('black', '13'), ('red', '25'),
('red', '9'), ('black', '26'), ('black', '15'),
('black', '20'), ('black', '31'), ('red', '3')]

def countReds( aList ):
count= 0
for color,number in aList:
if color == 'black':
yield count
count= 0
else:
count += 1
yield count

gaps= [ gap for gap in countReds(spins) ]
print gaps



what i understood is as follows:>
1)First it makes count = 0
2)then it goes straight in for loop
3)Since the first color is red, it goes in else block and increment
count to 1 and stop execution.


What makes you think it stops?
It carries on to the next iteration which is black and so enters the if 
block which yields the count which will be 1.



4)Now its next method is called using list comprehension, and it resumes
its execution and yield its first value i.e. 1.Note that the yield
statement is outside the for loop.


Not quite,
Next time round it carries on from the yield so sets count back
to zero and fetches the next two tuples before hitting another
black. So this time it yields 2.

Once the tuples are exhausted it yields the final count,
which should be 1


5)Now there is no further yield statement which can stop execution of
countreds() function.So it raises StopExecution exception

But this is not what is happening. why?


Your understanding was flawed. Assuming I've pasted the correct code 
listing... But there is an explanation right under the listing on the 
page. Assuming you read it, which bit of that did you not understand?


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] thanks - Beginner - explaining 'Flip a coin' bug

2014-02-15 Thread Marc Eymard
Hi David,

Thanks for your input about the logic of this little script of mine.

I confess I omitted the edge possibility and assumed heads or taisl only.

As I progress further with getting the foundation knowledge of the language 
itself, it is really appreciated to be corrected on what good programming is 
about regardless of the language.

Please keep commenting/helping whenever necessary.

Cheers,
Marc

Date: Fri, 14 Feb 2014 22:49:43 -0500
Subject: Re: [Tutor] Beginner - explaining 'Flip a coin' bug
From: dwightdhu...@gmail.com
To: marc_eym...@hotmail.com
CC: tutor@python.org

Here is a problem I've come across, from empirical evidence, that also relates 
to your equation. We always assume
 that their are always two probabilities, that a coin can be either head or 
tails. 


However, there are dynamics within a third realm of the dimensionality of the 
coin...it's not a two dimensional plane. So the planar probabilities in 
relation to the 'surface are' hold another possibility...the 'edge'.


The algorithm of applying the edge are up to you, but I've seen the coin land 
on it's curved edge more than once, so this function you've designed, should 
have more than two possibilities, within a complete algorithm to the real world 
functionality of the coin in question.



On Wed, Feb 12, 2014 at 10:25 AM, Marc Eymard  wrote:




Hello there,

I want to emulate a coin flip and count how many heads and tails when flipping 
it a hundred times.

I first coded coinflip_WRONG.py with "count_flips += 1" statement within the 
if/else block.

When running it, either returned values are wrong or the script seems to enter 
in an infinite loop showing no return values at all.

coinflip.py is a corrected version I worked out myself. I moved "count_flips+= 
1" out of if/else block and inserted it before if/else.


However, I still don't understand the bug since, in my understanding, both 
files are incrementing variable count_flips each time until the loop becomes 
false.

Can somebody explain the reason of the bug.

Cheers,

Marc
  

___

Tutor maillist  -  Tutor@python.org

To unsubscribe or change subscription options:

https://mail.python.org/mailman/listinfo/tutor




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com


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


[Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Marc Eymard
Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The logic of using the variables as part of the function arguments is to manage 
to get a smaller range each time the function is called excluding the possible 
repeat of the return value of randint.

Here is what happens in my script:

>>> import random
>>> low_range = -1
>>> high_range = 101
>>> random.randint(low_range + 1, high_range - 1)
56
>>> low_range
-1
>>> high_range
101

I was rather expecting:

 >>> low_range
0
>>> high_range
100

Can somebody explain why both low_range and high_range are still returning 
their initial values ?

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


Re: [Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Jay Lozier


On 02/15/2014 11:25 AM, Marc Eymard wrote:

Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The logic of using the variables as part of the function arguments is 
to manage to get a smaller range each time the function is called 
_excluding_ the possible repeat of the return value of randint.


Here is what happens in my script:

>>> import random
>>> low_range = -1
>>> high_range = 101
>>> random.randint(low_range + 1, high_range - 1)
56
>>> low_range
-1
>>> high_range
101*
*


I was rather expecting:

 >>> low_range

0

>>> high_range

100


Can somebody explain why both low_range and high_range are still 
returning their initial values ?


Thanks,
Marc*
*


The variables low_range and high_range are not modified. The arguments 
to random.randint do not change the initial values of the variables, 
only the actual values passed.


--
Jay Lozier
jsloz...@gmail.com

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


Re: [Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Alan Gauld

On 15/02/14 16:25, Marc Eymard wrote:


Here is what happens in my script:

 >>> import random
 >>> low_range = -1
 >>> high_range = 101
 >>> random.randint(low_range + 1, high_range - 1)
56
 >>> low_range
-1
 >>> high_range
101*

I was rather expecting:

  >>> low_range
0
 >>> high_range
100


Really? Why?
You never change low_range or high_range so why would
their values change?

The help() for randint says:

randint(self, a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.

So the function doesn't change the values either it
just returns a random number between them.


Can somebody explain why both low_range and high_range are still
returning their initial values ?


Because you didn't change them.
When you called randint you passed in two expressions:

low_range+1 and high_range-1
which Python evaluated as 0 and 100.

But that did not change your variable values in any way, it just created 
new values that were passed into randint() as 'a' and 'b' respectively.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Beginner - understanding randint arguments

2014-02-15 Thread Steven D'Aprano
On Sat, Feb 15, 2014 at 04:25:34PM +, Marc Eymard wrote:

> Can somebody explain why both low_range and high_range are still 
> returning their initial values ?

Because you haven't changed either of them. Imagine the chaos if every 
time you did arithmetic on a variable, Python changed the variable:

x = 1
y = x + 100

What is the value of y? 101, correct?

What is the value of x? It should still be 1, not 101.

The same applies when you get rid of the "y =" and just pass it to a 
function:

x = 1
some_function(x + 100)

What's the value of x? It needs to be 1, because you haven't changed it.

The only way to change x is to explicitly change it:

x = x + 100


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


Re: [Tutor] Regular expressions

2014-02-15 Thread Santosh Kumar
Thank you all.


On Thu, Feb 13, 2014 at 10:47 PM, Walter Prins  wrote:

> Hi,
>
> On 13 February 2014 06:44, Santosh Kumar  wrote:
> > I am using ipython.
> >
> > 1 ) Defined a string.
> >
> > In [88]: print string
> > foo foobar
> >
> > 2) compiled the string to grab the "foo" word.
> >
> > In [89]: reg = re.compile("foo",re.IGNORECASE)
> >
> > 3) Now i am trying to match .
> >
> > In [90]: match = reg.match(string)
> >
> > 4) Now i print it.
> >
> > In [93]: print match.group()
> > foo
> >
> > Correct me if i am wrong, i am expecting both "foo" and "foobar", why is
> it
> > giving
> > just "foo"
>
> A small addition to Peter's already comprehensive reply: Your regular
> expression is not including what follows "foo", it is defined as
> *only* the string literal "foo", so it can only ever match and return
> the literal string "foo".
>
> Try specifying "foo.*" as the regular expression.  Example session:
>
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
> Type "copyright", "credits" or "license" for more information.
>
> IPython 1.0.0 -- An enhanced Interactive Python.
> ? -> Introduction and overview of IPython's features.
> %quickref -> Quick reference.
> help  -> Python's own help system.
> object?   -> Details about 'object', use 'object??' for extra details.
>
> [C:/Src]|1> s='foo foobar'
>
> [C:/Src]|2> import re
>
> [C:/Src]|3> reg=re.compile('foo.*', re.IGNORECASE)
>
> [C:/Src]|4> match=reg.match(s)
>
> [C:/Src]|5> print match.group()
> foo foobar
>
>
>
> Walter
>



-- 
D. Santosh Kumar
RHCE | SCSA
+91-9703206361


Every task has a unpleasant side .. But you must focus on the end result
you are producing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor