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


Groups > comp.lang.python > #101800

Re: Using 'Or'

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.python
Subject Re: Using 'Or'
Date 2016-01-16 12:03 +0100
Organization A noiseless patient Spider
Message-ID <n7d7tm$ebu$1@dont-email.me> (permalink)
References <mailman.37.1452938481.15297.python-list@python.org>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web