Re: Planet Python

2020-12-14 Thread dn via Python-list

On 12/12/2020 07:22, dn via Python-list wrote:

Has something happened to the Planet Python feed?
- Last update: December 07, 2020 04:48 PM UTC



Fixed! (Thanks!)

Although, still reported as an 'open' issue
https://github.com/python/planet/issues/446
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


RE: To check if number is in range(x,y)

2020-12-14 Thread Schachner, Joseph
>>> r = range(10)
So r is a list containing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

  >>> 2 in r
  True
As expected.

  >>> 2.5 in r
  False
Also as expected.  If you did int(floor(2.5)) in 5 that would be true.

  >>> r = range(1, 10, 2)
  >>> 2 in r
  False
  >>> list(r)
  [1, 3, 5, 7, 9]
Well, yes, because you started the range at 1.  Start at 0 and you'd get 0, 2, 
4, 6, 8.

"It also doesn't automatically convert from the string inputs you're getting 
from the input() function:

  >>> s = "5"
  >>> s in r
  False
  >>> int(s) in r
  True"
You have just discovered that Python, although it is dynamically typed, is 
STRICTLY typed.  Another way to say this: you have discovered that Python isn't 
the same as BASIC.  Yes, you have to convert strings to int or float, Python 
does not assume you want to if you did not do it. Similarly, you have to do 
something to convert int or float to text.  Python makes it very simple, but 
you have to do it.


"Additionally, note that the endpoint of the range is exclusive so
  >>> r = range(1, 10)
  >>> 10 in r
  False"

I don't have to note that, I KNOW that (as I've demonstrated above), because I 
read a couple of books on Python.  Python range starts on the number you 
specify and does NOT include the end number.
So: range(0,10) is 0 to 9(note that this is 10 integers)
  range(10,20) is 10 to 19(also 10 integers)
  range(20,30) is 20 to 29   (another 10 integers)

Now suppose that the end integer was not excluded. Each range call would 
produce 11 integers.  10, 20, and 30 would occur twice.  Or you'd have to set 
the range limits differently.

I recommend you read Python 101 and when you've done that, read Python 201.   I 
think they are very good "learn Python" books.
If you're surprised that the end point is not included in range, you need to 
read Python 101.

--- Joseph S.



-Original Message-
From: Tim Chase 
Sent: Saturday, December 12, 2020 11:51 AM
To: Bischoop 
Cc: Bischoop ; python-list@python.org
Subject: Re: To check if number is in range(x,y)

On 2020-12-12 15:12, Bischoop wrote:
> I need to check if input number is 1-5. Whatever I try it's not
> working. Here are my aproaches to the problem: https://bpa.st/H62A
>
> What I'm doing wrong and how I should do it?

A range is similar to a list in that it contains just the numbers
listed:

  >>> r = range(10)
  >>> 2 in r
  True
  >>> 2.5 in r
  False
  >>> r = range(1, 10, 2)
  >>> 2 in r
  False
  >>> list(r)
  [1, 3, 5, 7, 9]

It also doesn't automatically convert from the string inputs you're getting 
from the input() function:

  >>> s = "5"
  >>> s in r
  False
  >>> int(s) in r
  True

Additionally, note that the endpoint of the range is exclusive so

  >>> r = range(1, 10)
  >>> 10 in r
  False
  >>> list(r)
  [1, 2, 3, 4, 5, 6, 7, 8, 9]

If you want numeric-range checks, Python provides the lovely double-comparison 
syntax:

  >>> x = 5
  >>> 2 < x < 10
  True
  >>> x = 5.5
  >>> 2 < x < 10
  True
  >>> s = "5"
  >>> 2 < s < 10
  Traceback…
  >>> 2 < int(s) < 10
  True

Hopefully this gives you the hints that you need to troubleshoot.

-tkc






-- 
https://mail.python.org/mailman/listinfo/python-list


Re: To check if number is in range(x,y)

2020-12-14 Thread 2QdxY4RzWzUUiLuE
On 2020-12-14 at 21:21:43 +,
"Schachner, Joseph"  wrote:

> >>> r = range(10)
> So r is a list containing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

In a number of ways, r behaves as if it were that list, but r is
definitely not that list:

>>> r = range(10)
>>> type(r)

>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(l)

>>> r == l
False

> You have just discovered that Python, although it is dynamically
> typed, is STRICTLY typed.  Another way to say this: you have
> discovered that Python isn't the same as BASIC ...

Citation needed?  I can't speak for every version of BASIC ever, but the
ones I used had separate namespaces for numeric variables and string
variables:  A was a number, A$ was a string, and never the twain shall
meet.  That's strict typing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: To check if number is in range(x,y)

2020-12-14 Thread Dan Stromberg
On Mon, Dec 14, 2020 at 1:23 PM Schachner, Joseph <
joseph.schach...@teledyne.com> wrote:

> >>> r = range(10)
> So r is a list containing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
>
To get a list of consecutive int's, you can use, for EG:
r = list(range(10))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: To check if number is in range(x,y)

2020-12-14 Thread Dan Stromberg
On Mon, Dec 14, 2020 at 3:07 PM Dan Stromberg  wrote:

>
> On Mon, Dec 14, 2020 at 1:23 PM Schachner, Joseph <
> joseph.schach...@teledyne.com> wrote:
>
>> >>> r = range(10)
>> So r is a list containing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
>>
> To get a list of consecutive int's, you can use, for EG:
> r = list(range(10))
>

Oh, and range() returning a (lazy) range is a new thing.  In Python 2.x,
range returned a list and you had to use xrange to get laziness.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: To check if number is in range(x,y)

2020-12-14 Thread Tim Chase
On 2020-12-14 21:21, Schachner, Joseph wrote:
> >>> r = range(10)  
> So r is a list containing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

In Python 3.x, r is *not* a list.  It is a custom object/class.

>   >>> 2 in r  
>   True
> As expected.

I'm not sure what your replies are suggesting here.  I demonstrated
the OP's edge-cases, especially cases that one might experience coming
from other languages.

>   >>> r = range(1, 10, 2)
>   >>> 2 in r  
>   False
>   >>> list(r)  
>   [1, 3, 5, 7, 9]
> Well, yes, because you started the range at 1.  Start at 0 and
> you'd get 0, 2, 4, 6, 8.

Had I done this, for pedagogical value I would have checked for 3
then:

  >>> r = range(0, 10, 2)
  >>> 3 in r
  False

The goal was to demonstrate that the resulting range object, when
given a step-size of something than the default 1, will have holes in
it, and as such, testing for membership in one of those holes would
fail.  Showing successful membership wouldn't add any value.

> "It also doesn't automatically convert from the string inputs
> you're getting from the input() function:
> 
>   >>> s = "5"
>   >>> s in r  
>   False
>   >>> int(s) in r  
>   True"
> You have just discovered that Python, although it is dynamically
> typed, is STRICTLY typed. 

No, not just now discovered something I've long known.  The goal was
to provide an example for the OP of this exact case since their
original code attempted to use the string returned from input() and
used it as-is (without converting to int) for this exact sort of
comparison.

> Another way to say this: you have discovered that Python isn't the
> same as BASIC.

Additionally, (many? all? some?) BASICs have similarly strict typing.
For example, reaching for the BASIC that I used in the 80s:

  ] S$ = "HELLO"
  ] I = 42
  ] PRINT S$ + I
  ?TYPE MISMATCH ERROR

> "Additionally, note that the endpoint of the range is exclusive so
>   >>> r = range(1, 10)
>   >>> 10 in r  
>   False"
> 
> I don't have to note that

My comment was directed at the OP.  Unless you are Bischoop, that's
not you.

> Now suppose that the end integer was not excluded. Each range call
> would produce 11 integers. 

The goal was to show the OP that while some languages (such as the
aforementioned BASIC) have *inclusive* ranges:
  
  ] FOR I = 1 to 3 : PRINT I : NEXT
  1
  2
  3

Python's ranges are exclusive.  Because a language could have either,
the example demonstrated Python's choice.

> I recommend you read Python 101 and when you've done that, read
> Python 201.   I think they are very good "learn Python" books. If
> you're surprised that the end point is not included in range, you
> need to read Python 101.

Your condescending replies bark up the wrong tree.

-tkc






-- 
https://mail.python.org/mailman/listinfo/python-list