Re: [Tutor] Count for loops

2017-04-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
A lot of confusion is caused by the print function converting an integer or
float
to a string before printing to console. thus both '1234 and '1234' are
shown as
1234 on the console. Similarly '15.4' and 15.4 are displayed as 15.4. There
is no way
to tell which is a string, which is an int and which is a float by looking
at the display on
console. In order to find which is which one has to type the variables.


>>> s = '1234'

>>> print(s)
1234

>>> s = 1234

>>> print(s)
1234

>>> s = '15.4'

>>> print(s)
15.4

>>> s = 15.4

>>> print(s)
15.4

regards,
Sarma.

On Wed, Apr 12, 2017 at 11:11 AM, eryk sun  wrote:

> On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp  wrote:
> >
> > I have not used the decimal module (until tonight).  I just now played
> > around with it some, but cannot get it to do an exact conversion of
> > the number under discussion to a string using str().
>
> Pass a string to the constructor:
>
> >>> d = decimal.Decimal('3.141592653589793238462643383279
> 50288419716939')
> >>> str(d)
> '3.14159265358979323846264338327950288419716939'
>
> When formatting for printing, note that classic string interpolation
> has to first convert the Decimal to a float, which only has 15 digits
> of precision (15.95 rounded down).
>
> >>> '%.44f' % d
> '3.14159265358979311599796346854418516159057617'
> >>> '%.44f' % float(d)
> '3.14159265358979311599796346854418516159057617'
>
> The result is more accurate using Python's newer string formatting
> system, which allows types to define a custom __format__ method.
>
> >>> '{:.44f}'.format(d)
> '3.14159265358979323846264338327950288419716939'
> >>> format(d, '.44f')
> '3.14159265358979323846264338327950288419716939'
> ___
> 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] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp  wrote:
>
> I have not used the decimal module (until tonight).  I just now played
> around with it some, but cannot get it to do an exact conversion of
> the number under discussion to a string using str().

Pass a string to the constructor:

>>> d = decimal.Decimal('3.14159265358979323846264338327950288419716939')
>>> str(d)
'3.14159265358979323846264338327950288419716939'

When formatting for printing, note that classic string interpolation
has to first convert the Decimal to a float, which only has 15 digits
of precision (15.95 rounded down).

>>> '%.44f' % d
'3.14159265358979311599796346854418516159057617'
>>> '%.44f' % float(d)
'3.14159265358979311599796346854418516159057617'

The result is more accurate using Python's newer string formatting
system, which allows types to define a custom __format__ method.

>>> '{:.44f}'.format(d)
'3.14159265358979323846264338327950288419716939'
>>> format(d, '.44f')
'3.14159265358979323846264338327950288419716939'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 3:40 AM, boB Stepp  wrote:
>
> I have to say I am surprised by this as well as the OP.  I knew that
> str() in general makes a nice printable representation

The single-argument str() constructor calls the object's __str__
method (or __repr__ if __str__ isn't defined). In Python 3,
float.__str__ and float.__repr__ behave the same. They use as many
digits as is required to round trip back to the float value exactly.
That's up to 17 digits.

>>> str(1.2345678901234567)
'1.2345678901234567'
>>> str(1.)
'1.0'

In Python 2, float.__str__ uses up to 12 digits:

>>> str(1.2345678901234567)
'1.23456789012'

> realize that using str() on an arbitrarily typed in "float-like" value
> would convert the typed in value to a normal float's max precision.

For a literal floating-point value in source code, the compiler
(component of the interpreter) first parses a NUMBER node:

>>> e = parser.expr('3.14159265358979323846264338327950288419716939')
>>> p = parser.st2list(e)
>>> while p[0] != token.NUMBER:
... p = p[1]
...
>>> p
[2, '3.14159265358979323846264338327950288419716939']

Next it transforms this concrete syntax tree into a abstract syntax tree:

>>> a = ast.parse('3.14159265358979323846264338327950288419716939',
...   mode='eval')
>>> ast.dump(a)
'Expression(body=Num(n=3.141592653589793))'

>>> a.body.n
3.141592653589793
>>> type(a.body.n)


You see above that the AST transforms the NUMBER node into a Num node
that references a float object. The value of the float is the closest
possible approximation of the source code literal as a
double-precision binary float.

If the compiler instead used Decimal objects, then it could retain all
of the literal's precision. Double-precision binary floats are fast
and efficient by virtue of hardware support, but that's not a
compelling reason in Python. Maybe some future version of Python will
switch to using Decimals for floating-point literals.

The final step is to compile this AST into bytecode and create a code
object. The float value is referenced in the code object's co_consts
attribute:

>>> c = compile(a, '', 'eval')
>>> c.co_consts
(3.141592653589793,)

The code in this case is simple; just load and return the constant value:

>>> dis.dis(c)
  1   0 LOAD_CONST   0 (3.141592653589793)
  3 RETURN_VALUE

>>> eval(c)
3.141592653589793
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 3:43 PM, Alan Gauld via Tutor  wrote:
> On 11/04/17 19:44, Mats Wichmann wrote:
>
>> import decimal
>>
>> Pi_Number =
>> str(decimal.Decimal(3.14159265358979323846264338327950288419716939))
>>
>
> Unfortunately that doesn't work either:
>
 "   " + str(decimal.Decimal(
> ... 3.14159265358979323846264338327950288419716939))
> '   3.141592653589793115997963468544185161590576171875'

>
> Notice the output is both longer and has completely different
> numbers in the last half of the result.

I have not used the decimal module (until tonight).  I just now played
around with it some, but cannot get it to do an exact conversion of
the number under discussion to a string using str().  I notice that
the module has the methods to_eng_string() and to_sci_string(), but I
see no substitute for str() listed.  Surely there is some way to use
the decimal module to get the desired conversion to a string?  I have
to retire for the evening and will try to figure this out tomorrow.

Cheers!

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


Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 2:18 PM, Marc Tompkins  wrote:
> On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth 
> wrote:
>
>> I tested this approach, and I noticed one weird thing:
>>
>> Pi_Number = str(3.14159265358979323846264338327950288419716939)
>> Pi_Number = "3" + Pi_Number[2:]

Minor note:  If the OP's original effort was to reproduce his pi
approximation with a decimal point, then his slice indexing should
have been Pi_Number[1:]

>> print(Pi_Number)
>>
>> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python
>> Code\PPC_56.py ==
>> 3141592653589793
>> >>>
>>
>> How come that not the entire string is being printed, but only the
>> first 16 digits?
>>
>
> That's due to how str() works; it's simply making (its own conception) of a
> pretty representation of what's fed to it, which in the case of
> floating-point numbers means that they're represented to 16 digits
> precision.
> str(3.14159265358979323846264338327950288419716939) is _not_ the same as
> "3.14159265358979323846264338327950288419716939"
>
> From the docs: "Return a string containing a nicely printable
> representation of an object. For strings, this returns the string itself.
> The difference with repr(object) is that str(object) does not always
> attempt to return a string that is acceptable to eval()
> ; its goal is to
> return a printable string."

I have to say I am surprised by this as well as the OP.  I knew that
str() in general makes a nice printable representation, but I did not
realize that using str() on an arbitrarily typed in "float-like" value
would convert the typed in value to a normal float's max precision.
So I guess the only way to get a string representation of such a
number is to do something like:

py3: str_pi = '3.14159265358979323846264338327950288419716939'
py3: len(str_pi)
46
py3: str_pi
'3.14159265358979323846264338327950288419716939'

or perhaps:

py3: new_pi = '3' + '.' + '14159265358979323846264338327950288419716939'
py3: len(new_pi)
46
py3: new_pi
'3.14159265358979323846264338327950288419716939'

or even with str():

py3: new_pi = '3' + '.' + str(14159265358979323846264338327950288419716939)
py3: len(new_pi)
46
py3: new_pi
'3.14159265358979323846264338327950288419716939'

since in Python 3 integers are unlimited in precision (within RAM constraints).

I guess this has to be this way or the conversions of actual Python
numerical literals to strings and back again would otherwise be
inconsistent.

Still learning!

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


Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 19:44, Mats Wichmann wrote:

> import decimal
> 
> Pi_Number =
> str(decimal.Decimal(3.14159265358979323846264338327950288419716939))
> 

Unfortunately that doesn't work either:

>>> "   " + str(decimal.Decimal(
... 3.14159265358979323846264338327950288419716939))
'   3.141592653589793115997963468544185161590576171875'
>>>

Notice the output is both longer and has completely different
numbers in the last half of the result.


> topic. The decimal module documentation contains this pithy comment near
> the top:
> 
> Decimal “is based on a floating-point model which was designed with
> people in mind, and necessarily has a paramount guiding principle –
> computers must provide an arithmetic that works in the same way as the
> arithmetic that people learn at school.”

But sadly they haven't beat the problem of storing high precision
decimal numbers in binary storage.

-- 
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] Count for loops

2017-04-11 Thread Mats Wichmann
On 04/11/2017 10:48 AM, Rafael Knuth wrote:

> Thanks for the clarification.
> I tested this approach, and I noticed one weird thing:
> 
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
> 
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python Code\PPC_56.py ==
> 3141592653589793

> 
> How come that not the entire string is being printed, but only the
> first 16 digits?

Believe it or not, this is one of the Mysteries of Life (Monty Python
reference since that's what the language is named after... sorry).

To get what you're expecting, you could do this:

import decimal

Pi_Number =
str(decimal.Decimal(3.14159265358979323846264338327950288419716939))

in general, (binary) floats and all other things don't interact the way
we mere mortals might expect, and there's a ton of writing on that
topic. The decimal module documentation contains this pithy comment near
the top:

Decimal “is based on a floating-point model which was designed with
people in mind, and necessarily has a paramount guiding principle –
computers must provide an arithmetic that works in the same way as the
arithmetic that people learn at school.”

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


Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 17:48, Rafael Knuth wrote:

> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
> 3141592653589793

> 
> How come that not the entire string is being printed, but only the
> first 16 digits?

There are two problems here.
First str() truncates the length of the output to make it look nicer.
Second your version of PI has too many decimal places for a floating
point to hold. Flosting point numbers can hold a huge range of numbers
but not a huge precision.


-- 
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] Count for loops

2017-04-11 Thread Marc Tompkins
On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth 
wrote:

> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
>
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python
> Code\PPC_56.py ==
> 3141592653589793
> >>>
>
> How come that not the entire string is being printed, but only the
> first 16 digits?
>

That's due to how str() works; it's simply making (its own conception) of a
pretty representation of what's fed to it, which in the case of
floating-point numbers means that they're represented to 16 digits
precision.
str(3.14159265358979323846264338327950288419716939) is _not_ the same as
"3.14159265358979323846264338327950288419716939"

>From the docs: "Return a string containing a nicely printable
representation of an object. For strings, this returns the string itself.
The difference with repr(object) is that str(object) does not always
attempt to return a string that is acceptable to eval()
; its goal is to
return a printable string."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Rafael Knuth
>>> b = "3"+b[2:]  #Removing the decimal point so that there are digits only in
>>
>> my_number = 3.14159
>
> Here you assign a floating point number to mmy_number but
> the code Sama wrote was for working with strings read
> from a text file.
>
> You would need to convert it first:
>
> my_number = str(3.14159)
>
>> my_number = "3"+my_number[2:]
>> print(my_number)

Thanks for the clarification.
I tested this approach, and I noticed one weird thing:

Pi_Number = str(3.14159265358979323846264338327950288419716939)
Pi_Number = "3" + Pi_Number[2:]
print(Pi_Number)

== RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python Code\PPC_56.py ==
3141592653589793
>>>

How come that not the entire string is being printed, but only the
first 16 digits?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-08 Thread Alex Kleider

On 2017-04-08 05:49, Rafael Knuth wrote:

Dear Sama,

thank you so much for your explanation and sorry to bother you on the
same subject again.
I learn the most by taking code apart line by line, putting it
together, taking apart again, modifying it slightly ... which is
exactly what I did with your code.

On Tue, Apr 4, 2017 at 3:20 PM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
 wrote:
b = "3"+b[2:]  #Removing the decimal point so that there are digits 
only in


my_number = 3.14159
my_number = "3"+my_number[2:]
print(my_number)

This is the error code I got:

== RESTART: C:/Users/Rafael/Documents/01 - BIZ/CODING/Python 
Code/PPC_56.py ==

Traceback (most recent call last):
  File "C:/Users/Rafael/Documents/01 - BIZ/CODING/Python
Code/PPC_56.py", line 2, in 
my_number = "1"+my_number[2:]
TypeError: 'float' object is not subscriptable




I am really trying to understand how to modify strings, floats and
variables from different sources.
In case of a text file, your code works, but if I apply the same to a
float assigned to a variable, it does not work.
What am I doing wrong here? Thank you so much for your patience.


The error message is telling you exactly what is wrong:
"not subscriptable"
You've tried to use string functionality on a float.
Specifically: my_number is a float.
"""my_number[2:]""" would only make sense to the interpreter if 
it("my_number", not the interpretery:-) were a string (or some other 
sequence.)

try changing your code to:
  my_number_as_a_string = "3.14159"
  my_new_number_still_a_string = "3" + my_number_as_a_string[2:]  # 
spaces added for clarity

  print(my_new_number_still_a_string)
  my_float = float(my_new_number_still_a_string)
  print(my_float)
HTH
ps warning: not tested
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-08 Thread Alan Gauld via Tutor
On 08/04/17 13:49, Rafael Knuth wrote:

>> b = "3"+b[2:]  #Removing the decimal point so that there are digits only in
> 
> my_number = 3.14159

Here you assign a floating point number to mmy_number but
the code Sama wrote was for working with strings read
from a text file.

You would need to convert it first:

my_number = str(3.14159)

> my_number = "3"+my_number[2:]
> print(my_number)
> 
> This is the error code I got:
> 
> == RESTART: C:/Users/Rafael/Documents/01 - BIZ/CODING/Python Code/PPC_56.py ==
> Traceback (most recent call last):
>   File "C:/Users/Rafael/Documents/01 - BIZ/CODING/Python
> Code/PPC_56.py", line 2, in 
> my_number = "1"+my_number[2:]
> TypeError: 'float' object is not subscriptable

And that is what the error tells you, that you are trying
to index a float but it should be a string.

> In case of a text file, your code works, but if I apply the same to a
> float assigned to a variable, it does not work.

That's right, the set of operations applicable to numbers is
completely different to those applicable to strings, you cannot
mix 'n match. You need to convert between the types.

-- 
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] Count for loops

2017-04-08 Thread Rafael Knuth
Dear Sama,

thank you so much for your explanation and sorry to bother you on the
same subject again.
I learn the most by taking code apart line by line, putting it
together, taking apart again, modifying it slightly ... which is
exactly what I did with your code.

On Tue, Apr 4, 2017 at 3:20 PM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
 wrote:
> b = "3"+b[2:]  #Removing the decimal point so that there are digits only in

my_number = 3.14159
my_number = "3"+my_number[2:]
print(my_number)

This is the error code I got:

== RESTART: C:/Users/Rafael/Documents/01 - BIZ/CODING/Python Code/PPC_56.py ==
Traceback (most recent call last):
  File "C:/Users/Rafael/Documents/01 - BIZ/CODING/Python
Code/PPC_56.py", line 2, in 
my_number = "1"+my_number[2:]
TypeError: 'float' object is not subscriptable
>>>

I am really trying to understand how to modify strings, floats and
variables from different sources.
In case of a text file, your code works, but if I apply the same to a
float assigned to a variable, it does not work.
What am I doing wrong here? Thank you so much for your patience.



> the file
> n = len(b)
> for i in range(n-3):
> if b[i:i+4] == get_year:  # Taking 4 digit long chunks from successive
> positions in b and seeing whether they are equal to get_year
> count += 1  # If they are equal increase the count by 1
>
> regards,
> Sarma.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-05 Thread Neil Cerutti
On 2017-04-03, Mats Wichmann  wrote:
> If you're talking about 4-digit year numbers using a Western
> calendar in digits of PI, the overlap effect seems unlikely to
> matter - let's say the year is 1919, do we think PI contains
> the sequence 191919? count would report back one instead of two
> in that case. In other cases it might matter; count is written
> specifically to not care about overlaps: "Return the number of
> (non-overlapping) occurrences"  So that's worth keeping in mind
> when you think about what you need from substrings-in-strings
> cases.

Composing a FSA (finite state automata) by hand would be an
excellent exercise in addition to covering the overlapping cases.

Another fun exercise might be to find the bithdays using
arithmetic operations instead of string operations.

-- 
Neil Cerutti

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


Re: [Tutor] Count for loops

2017-04-04 Thread Alan Gauld via Tutor
On 04/04/17 12:04, Rafael Knuth wrote:
> Sarma: thank you so much, I checked your code, it works. However, can
> you enlighten me what it exactly does?

It just iterates over the PI string manually and compares
the birth date with the first 4 PI string characters.

It would probably be more pythonic and easier to read
to use startswith() instead

> count = 0
> b = "3"+b[2:]

I think this is to eliminate the period after the 3 of PI

> n = len(b)
> for i in range(n-3):
> if b[i:i+4] == get_year:
> count += 1

This is the core of it.
It starts with i = 0 and goes up to i = n-4
If b[i] to b[i+3] equals the birthdate you count
a match

You could rewrite it as

for i in range(n-3):
if b.startswith(get_year, i):
   count += 1

HTH
-- 
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] Count for loops

2017-04-04 Thread Rafael Knuth
Sarma: thank you so much, I checked your code, it works. However, can
you enlighten me what it exactly does?
I do not understand it (yet). Thank you in advance.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"

with open (file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
b = "3"+b[2:]
n = len(b)
for i in range(n-3):
if b[i:i+4] == get_year:
count += 1
print("Your birth date occurs %s times in PI!" % (count))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Small correction.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
b= '3'+b[2:]
n = len(b)
for i in range(n-3):
if b[i:i+4] == get_year:
count += 1
print("Your birth date occurs %s times in PI!" % (count))


regards,
Sarma.

On Tue, Apr 4, 2017 at 5:07 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ <
dvnsa...@gmail.com> wrote:

> I will go for this modification of the original code.
>
> file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
> with open(file_path) as a:
> b = a.read()
>
> get_year = input("What year were you born? ")
>
> count = 0
> b= '3'+b[2:]
> n = len(b)
> for i in range(n-4):
> if b[i:i+4] == get_year:
> count += 1
> print("Your birth date occurs %s times in PI!" % (count))
>
> regards,
> Sarma.
>
> On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann  wrote:
>
>> On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote:
>> > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> >> Sorry. That was  stupid of me. The loop does nothing.
>> >
>> > Let me rewrite the code with some different variable names...
>> >
>>  with open(file_path) as a:
>>  b = a.read()
>> >
>> > with open (file_path) as PI_text:
>> >  PI_as_a_long_string = PI_text.read()
>> >
>>  for year in b:
>> >
>> > for each_char in PI_as_a_long_string:
>> >
>>  if get_year in b:
>>  count += 1
>> >
>> >   if get_year in PI_as_a_long_string:
>> >   count += 1
>> >
>>  print("Your birth date occurs %s times in PI!" % (count))
>> >
>> > if count:
>> >print("Your birth date occurs in PI, I checked, count, "times!")
>> >
>> >
>> > Aren't meaningful variable names great? We should all do
>> > them more often.
>>
>>
>> So the takeaways here are:
>>
>> in the first ("non-counting") sample, there's no need to use a loop,
>> because you're going to quit after the outcome "in" or "not in" right
>> away - there's no loop behavior at all.
>>
>> for year in b:
>> if get_year in b:
>> print("Your year of birth occurs in PI!")
>> break
>> else:
>> print("Your year of birth does not occur in PI.")
>> break
>>
>> for the counting version you could do something that searches for the
>> year repeatedly, avoiding starting over from the beginning so you're
>> actually finding fresh instances, not the same one (hint, hint). There
>> are several ways to do this, including slicing, indexing, etc.  Alan's
>> suggestion looks as good as any.
>>
>> Or, if you don't care about overlapping cases, the count method of a
>> string will do just fine:
>>
>> PI_as_a_long_string.count(year)
>>
>> If you're talking about 4-digit year numbers using a Western calendar in
>> digits of PI, the overlap effect seems unlikely to matter - let's say
>> the year is 1919, do we think PI contains the sequence 191919? count
>> would report back one instead of two in that case. In other cases it
>> might matter; count is written specifically to not care about overlaps:
>> "Return the number of (non-overlapping) occurrences"  So that's worth
>> keeping in mind when you think about what you need from
>> substrings-in-strings cases.
>>
>>
>>
>> ___
>> 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] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 04/04/17 00:37, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> I will go for this modification of the original code.

> count = 0
> b= '3'+b[2:]
> n = len(b)
> for i in range(n-4):
> if b[i:i+4] == get_year:
> count += 1

While I think this works OK, I would probably suggest
that this is one of the rare valid cases for using
a regex(*) with a simple string. The findall() method
with a suitable pattern and flags should catch
overlaps. And is probably faster being written in C.

(*)Assuming you want to include overlapping cases,
otherwise just use b.count()...

-- 
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] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I will go for this modification of the original code.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
b= '3'+b[2:]
n = len(b)
for i in range(n-4):
if b[i:i+4] == get_year:
count += 1
print("Your birth date occurs %s times in PI!" % (count))

regards,
Sarma.

On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann  wrote:

> On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote:
> > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> >> Sorry. That was  stupid of me. The loop does nothing.
> >
> > Let me rewrite the code with some different variable names...
> >
>  with open(file_path) as a:
>  b = a.read()
> >
> > with open (file_path) as PI_text:
> >  PI_as_a_long_string = PI_text.read()
> >
>  for year in b:
> >
> > for each_char in PI_as_a_long_string:
> >
>  if get_year in b:
>  count += 1
> >
> >   if get_year in PI_as_a_long_string:
> >   count += 1
> >
>  print("Your birth date occurs %s times in PI!" % (count))
> >
> > if count:
> >print("Your birth date occurs in PI, I checked, count, "times!")
> >
> >
> > Aren't meaningful variable names great? We should all do
> > them more often.
>
>
> So the takeaways here are:
>
> in the first ("non-counting") sample, there's no need to use a loop,
> because you're going to quit after the outcome "in" or "not in" right
> away - there's no loop behavior at all.
>
> for year in b:
> if get_year in b:
> print("Your year of birth occurs in PI!")
> break
> else:
> print("Your year of birth does not occur in PI.")
> break
>
> for the counting version you could do something that searches for the
> year repeatedly, avoiding starting over from the beginning so you're
> actually finding fresh instances, not the same one (hint, hint). There
> are several ways to do this, including slicing, indexing, etc.  Alan's
> suggestion looks as good as any.
>
> Or, if you don't care about overlapping cases, the count method of a
> string will do just fine:
>
> PI_as_a_long_string.count(year)
>
> If you're talking about 4-digit year numbers using a Western calendar in
> digits of PI, the overlap effect seems unlikely to matter - let's say
> the year is 1919, do we think PI contains the sequence 191919? count
> would report back one instead of two in that case. In other cases it
> might matter; count is written specifically to not care about overlaps:
> "Return the number of (non-overlapping) occurrences"  So that's worth
> keeping in mind when you think about what you need from
> substrings-in-strings cases.
>
>
>
> ___
> 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] Count for loops

2017-04-03 Thread Mats Wichmann
On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote:
> On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> Sorry. That was  stupid of me. The loop does nothing.
> 
> Let me rewrite the code with some different variable names...
> 
 with open(file_path) as a:
 b = a.read()
> 
> with open (file_path) as PI_text:
>  PI_as_a_long_string = PI_text.read()
> 
 for year in b:
> 
> for each_char in PI_as_a_long_string:
> 
 if get_year in b:
 count += 1
> 
>   if get_year in PI_as_a_long_string:
>   count += 1
> 
 print("Your birth date occurs %s times in PI!" % (count))
> 
> if count:
>print("Your birth date occurs in PI, I checked, count, "times!")
> 
> 
> Aren't meaningful variable names great? We should all do
> them more often.


So the takeaways here are:

in the first ("non-counting") sample, there's no need to use a loop,
because you're going to quit after the outcome "in" or "not in" right
away - there's no loop behavior at all.

for year in b:
if get_year in b:
print("Your year of birth occurs in PI!")
break
else:
print("Your year of birth does not occur in PI.")
break

for the counting version you could do something that searches for the
year repeatedly, avoiding starting over from the beginning so you're
actually finding fresh instances, not the same one (hint, hint). There
are several ways to do this, including slicing, indexing, etc.  Alan's
suggestion looks as good as any.

Or, if you don't care about overlapping cases, the count method of a
string will do just fine:

PI_as_a_long_string.count(year)

If you're talking about 4-digit year numbers using a Western calendar in
digits of PI, the overlap effect seems unlikely to matter - let's say
the year is 1919, do we think PI contains the sequence 191919? count
would report back one instead of two in that case. In other cases it
might matter; count is written specifically to not care about overlaps:
"Return the number of (non-overlapping) occurrences"  So that's worth
keeping in mind when you think about what you need from
substrings-in-strings cases.



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


Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> Sorry. That was  stupid of me. The loop does nothing.

Let me rewrite the code with some different variable names...

>>> with open(file_path) as a:
>>> b = a.read()

with open (file_path) as PI_text:
 PI_as_a_long_string = PI_text.read()

>>> for year in b:

for each_char in PI_as_a_long_string:

>>> if get_year in b:
>>> count += 1

  if get_year in PI_as_a_long_string:
  count += 1

>>> print("Your birth date occurs %s times in PI!" % (count))

if count:
   print("Your birth date occurs in PI, I checked, count, "times!")


Aren't meaningful variable names great? We should all do
them more often.

-- 
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] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> Sorry. That was  stupid of me. The loop does nothing.
> 

On the contrary, the loop does an awful lot, just
not what the OP was expecting.

>>> with open(file_path) as a:
>>> b = a.read()

>>> for year in b:
>>> if get_year in b:
>>> count += 1

-- 
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] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Sorry. That was  stupid of me. The loop does nothing.




regards,
Sarma.

On Mon, Apr 3, 2017 at 8:44 PM, Alan Gauld via Tutor 
wrote:

> On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> > Modifying the code as shown below may work.
>
> I doubt it.
>
> > with open(file_path) as a:
> > b = a.read()
> >
> > get_year = input("What year were you born? ")
> >
> > count = 0
> > for year in b:
>
> Once more I ask, what does this loop do?
>
> > if get_year in b:
> > count += 1
> > print("Your birth date occurs %s times in PI!" % (count))
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> Modifying the code as shown below may work.

I doubt it.

> with open(file_path) as a:
> b = a.read()
> 
> get_year = input("What year were you born? ")
> 
> count = 0
> for year in b:

Once more I ask, what does this loop do?

> if get_year in b:
> count += 1
> print("Your birth date occurs %s times in PI!" % (count))

-- 
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] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Modifying the code as shown below may work.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
for year in b:
if get_year in b:
count += 1
print("Your birth date occurs %s times in PI!" % (count))

regards,
Sarma.

On Mon, Apr 3, 2017 at 8:22 PM, Alan Gauld via Tutor 
wrote:

> On 03/04/17 13:22, Rafael Knuth wrote:
>
> > with open (file_path) as a:
> > b = a.read()
> >
> > get_year = input("What year were you born? ")
> >
> > for year in b:
>
> Can you explain what you think this loop line is doing?
> I'm pretty sure it's not doing what you expect.
>
> > if get_year in b:
> > print("Your year of birth occurs in PI!")
> > break
> > else:
> > print("Your year of birth does not occur in PI.")
> > break
> >
> > As a next challenge, I wanted to check how often a person's birth year
> > occurs in PI. Unfortunately, I wasn't able to figure out how to use
> > the loop count properly.
>
> What loop count?
> There is none, its a for loop, no counter needed.
> (OK I just spotted your code below...)
>
> But there is a count() method on a string object that should help.
>
>
> > count = 0
> > for year in b:
> > if get_year in b:
> > count += 1
> > else:
> > print("Your birth date does not occur in PI.")
> > break
>
> > sum_count = sum(count)
>
> sum() sums a sequence, but count is an integer. You have been
> incrementing it as you go, the final value is already there.
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 13:22, Rafael Knuth wrote:

> with open (file_path) as a:
> b = a.read()
> 
> get_year = input("What year were you born? ")
> 
> for year in b:

Can you explain what you think this loop line is doing?
I'm pretty sure it's not doing what you expect.

> if get_year in b:
> print("Your year of birth occurs in PI!")
> break
> else:
> print("Your year of birth does not occur in PI.")
> break
> 
> As a next challenge, I wanted to check how often a person's birth year
> occurs in PI. Unfortunately, I wasn't able to figure out how to use
> the loop count properly. 

What loop count?
There is none, its a for loop, no counter needed.
(OK I just spotted your code below...)

But there is a count() method on a string object that should help.


> count = 0
> for year in b:
> if get_year in b:
> count += 1
> else:
> print("Your birth date does not occur in PI.")
> break

> sum_count = sum(count)

sum() sums a sequence, but count is an integer. You have been
incrementing it as you go, the final value is already there.

-- 
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


[Tutor] Count for loops

2017-04-03 Thread Rafael Knuth
I wrote a program which checks if PI (first one million digits)
contains a person's birth year.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"

with open (file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

for year in b:
if get_year in b:
print("Your year of birth occurs in PI!")
break
else:
print("Your year of birth does not occur in PI.")
break

As a next challenge, I wanted to check how often a person's birth year
occurs in PI. Unfortunately, I wasn't able to figure out how to use
the loop count properly. Can anyone help? Thanks!

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
for year in b:
if get_year in b:
count += 1
else:
print("Your birth date does not occur in PI.")
break
sum_count = sum(count)
print("Your birth date occurs %s times in PI!" % (sum_count))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor