Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101797 > unrolled thread

Using 'Or'

Started byKitten Corner <joshua.shapo@gmail.com>
First post2016-01-15 15:24 -0500
Last post2016-01-17 10:15 +1100
Articles 8 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Using 'Or' Kitten Corner <joshua.shapo@gmail.com> - 2016-01-15 15:24 -0500
    Re: Using 'Or' Christian Gollwitzer <auriocus@gmx.de> - 2016-01-16 12:03 +0100
      Re: Using 'Or' Marko Rauhamaa <marko@pacujo.net> - 2016-01-16 14:41 +0200
    Re: Using 'Or' Alister <alister.ware@ntlworld.com> - 2016-01-16 14:06 +0000
      Re: Using 'Or' Steven D'Aprano <steve@pearwood.info> - 2016-01-17 08:53 +1100
        Re: Using 'Or' Alister <alister.ware@ntlworld.com> - 2016-01-16 22:47 +0000
          Re: Using 'Or' Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-16 21:12 -0200
          Re: Using 'Or' Chris Angelico <rosuav@gmail.com> - 2016-01-17 10:15 +1100

#101797 — Using 'Or'

FromKitten Corner <joshua.shapo@gmail.com>
Date2016-01-15 15:24 -0500
SubjectUsing 'Or'
Message-ID<mailman.37.1452938481.15297.python-list@python.org>
Hi, I have python version 3.5.1 and I am working on a project, I'm trying
to make it by using the 'or' sequence, I'm trying to make it do 1 thing or
the other, here's an example: print('i like pie' or 'i like donuts'), it
only does the thing that's before the 'or', please help!

From,
Kitten Corner

[toc] | [next] | [standalone]


#101800

FromChristian Gollwitzer <auriocus@gmx.de>
Date2016-01-16 12:03 +0100
Message-ID<n7d7tm$ebu$1@dont-email.me>
In reply to#101797
Am 15.01.16 um 21:24 schrieb Kitten Corner:
> Hi, I have python version 3.5.1 and I am working on a project, I'm trying
> to make it by using the 'or' sequence, I'm trying to make it do 1 thing or
> the other, here's an example:

> print('i like pie' or 'i like donuts')

> it only does the thing that's before the 'or', please help!

I think you misunderstand what "or" does. It evaluates the first 
expression, and if this is false, it evaluates the second. The empty 
string is considered false in Python, so, if you modfiy your example:

	print('' or 'i like donuts')

it'll print the second thing 'i like donuts'.

'or' does not choose randomly between both sides - if you are looking 
for that, check

 >>> import random
 >>> random.choice(('donuts','apples'))
'donuts'
 >>> random.choice(('donuts','apples'))
'apples'
 >>> random.choice(('donuts','apples'))
'donuts'
 >>>


	Christian

[toc] | [prev] | [next] | [standalone]


#101803

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-01-16 14:41 +0200
Message-ID<87bn8lae8s.fsf@elektro.pacujo.net>
In reply to#101800
Christian Gollwitzer <auriocus@gmx.de>:

> Am 15.01.16 um 21:24 schrieb Kitten Corner:
>> print('i like pie' or 'i like donuts')
>
>> it only does the thing that's before the 'or', please help!
>
> I think you misunderstand what "or" does. It evaluates the first
> expression, and if this is false, it evaluates the second.

The fact that "or" doesn't return True but one of its arguments is a
great feature. Too bad "any" doesn't follow suit:

   >>> any(x for x in ('a', 'b'))
   True

>>>> import random
>>>> random.choice(('donuts','apples'))
> 'donuts'

Well, there's that. "Any" works more nicely with generators, though.


Marko

[toc] | [prev] | [next] | [standalone]


#101808

FromAlister <alister.ware@ntlworld.com>
Date2016-01-16 14:06 +0000
Message-ID<48smy.82436$Dn6.27537@fx46.am4>
In reply to#101797
On 15/01/16 20:24, Kitten Corner wrote:
> Hi, I have python version 3.5.1 and I am working on a project, I'm trying
> to make it by using the 'or' sequence, I'm trying to make it do 1 thing or
> the other, here's an example: print('i like pie' or 'i like donuts'), it
> only does the thing that's before the 'or', please help!
>
> From,
> Kitten Corner
>

Conditional operators (or and not == etc.) need to be used in a test

how else would you expect you print statement to be able to decided 
which to print?

see if you can work through the code below

food="input food ?"
if food =='pie' or food=='donuts':
	print ('I like %s'%food)
else:
	print ('I dont like %s'%food'

[toc] | [prev] | [next] | [standalone]


#101818

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-17 08:53 +1100
Message-ID<569abbf3$0$1603$c3e8da3$5496439d@news.astraweb.com>
In reply to#101808
On Sun, 17 Jan 2016 01:06 am, Alister wrote:

> Conditional operators (or and not == etc.) need to be used in a test

Technically, that is incorrect.

> how else would you expect you print statement to be able to decided 
> which to print?


default = "I like Brussels sprouts."
message = random.choice(["", "I like boiled cabbage."])
print( message or default )



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#101822

FromAlister <alister.ware@ntlworld.com>
Date2016-01-16 22:47 +0000
Message-ID<mMzmy.46103$Yh.17821@fx36.am4>
In reply to#101818
On 16/01/16 21:53, Steven D'Aprano wrote:
> On Sun, 17 Jan 2016 01:06 am, Alister wrote:
>
>> Conditional operators (or and not == etc.) need to be used in a test
>
> Technically, that is incorrect.
yes but the op is confused in his usage enough at present
>
>> how else would you expect you print statement to be able to decided
>> which to print?
>
>
> default = "I like Brussels sprouts."
> message = random.choice(["", "I like boiled cabbage."])
> print( message or default )
>
>
>
I hope I never see production code like that

[toc] | [prev] | [next] | [standalone]


#101824

FromBernardo Sulzbach <mafagafogigante@gmail.com>
Date2016-01-16 21:12 -0200
Message-ID<mailman.48.1452985992.15297.python-list@python.org>
In reply to#101822
On Sat, Jan 16, 2016 at 8:47 PM, Alister <alister.ware@ntlworld.com> wrote:
>>
>> default = "I like Brussels sprouts."
>> message = random.choice(["", "I like boiled cabbage."])
>> print( message or default )
>>
>>
>>
> I hope I never see production code like that
>

I agree. If you are going to use spaces after '(' and before ')' at
least be consistent.

-- 
Bernardo Sulzbach

[toc] | [prev] | [next] | [standalone]


#101825

FromChris Angelico <rosuav@gmail.com>
Date2016-01-17 10:15 +1100
Message-ID<mailman.49.1452986139.15297.python-list@python.org>
In reply to#101822
On Sun, Jan 17, 2016 at 9:47 AM, Alister <alister.ware@ntlworld.com> wrote:
> On 16/01/16 21:53, Steven D'Aprano wrote:
>>
>> On Sun, 17 Jan 2016 01:06 am, Alister wrote:
>>
>>> Conditional operators (or and not == etc.) need to be used in a test
>>
>>
>> Technically, that is incorrect.
>
> yes but the op is confused in his usage enough at present

Adding a falsehood sometimes helps reduce the confusion, but in this
case it just worsens things, I think.

>>> how else would you expect you print statement to be able to decided
>>> which to print?
>>
>>
>>
>> default = "I like Brussels sprouts."
>> message = random.choice(["", "I like boiled cabbage."])
>> print( message or default )
>>
>>
>>
> I hope I never see production code like that

Why? Okay, maybe not with a random.choice, but what about dict lookup?

specific_messages = {
     "foo": "You use a new foo.",
    "bar": "You sing a few bars of music.",
}
print(specific_messages.get(kwd) or "You {} vehemently.".format(kwd))

Seems fine to me.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web