473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

'IF' Syntax For Alternative Conditions

All my python books and references I find on the web have simplistic
examples of the IF conditional. A few also provide examples of multiple
conditions that are ANDed; e.g.,
if cond1:
if cond2:
do_something.

However, I cannot find, nor create by trial-and-error, the syntax for
alternative conditions that are ORed; e.g.,

if cond1 OR if cond2:
do_something.

I've tried using the C syntax for OR (||) but python complained. I'm sure
there's a way to do this rather than using if cond1: elif cond2: both with
the same code to execute.

Please pass me a pointer so I can learn how to correctly write this.

Rich
Feb 8 '07 #1
6 1974
rs******@nospam.appl-ecosys.com wrote:
However, I cannot find, nor create by trial-and-error, the syntax for
alternative conditions that are ORed; e.g.,

if cond1 OR if cond2:
do_something.
if cond1 or cond2:
do_something()
Feb 8 '07 #2
rs******@nospam.appl-ecosys.com writes:
if cond1:
if cond2:
do_something.
You can write:
if cond1 and cond2:
do_something
if cond1 OR if cond2:
do_something.
if cond1 or cond2:
do_something
I've tried using the C syntax for OR (||) but python complained. I'm sure
there's a way to do this rather than using if cond1: elif cond2: both with
the same code to execute.
Python uses the "and" and "or" keywords for && and ||.
Feb 8 '07 #3
En Thu, 08 Feb 2007 01:01:38 -0300, <rs******@nospam.appl-ecosys.com>
escribió:
However, I cannot find, nor create by trial-and-error, the syntax for
alternative conditions that are ORed; e.g.,

if cond1 OR if cond2:
do_something.

Please pass me a pointer so I can learn how to correctly write this.
See the Python tutorial:
http://docs.python.org/tut/node7.htm...00000000000000
Note that most (if not all) Python keywords are lowercase.

--
Gabriel Genellina

Feb 8 '07 #4
"Gabriel Genellina" <ga******@yahoo.com.arwrote:
Note that most (if not all) Python keywords are lowercase.
All keywords are lower case.

and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

'None' is not entirely lowercase, and you cannot assign to it, but
technically it isn't a keyword.
Feb 8 '07 #5
rs******@nospam.appl-ecosys.com wrote:
All my python books and references I find on the web have simplistic
examples of the IF conditional. A few also provide examples of multiple
conditions that are ANDed; e.g.,
if cond1:
if cond2:
do_something.

However, I cannot find, nor create by trial-and-error, the syntax for
alternative conditions that are ORed; e.g.,

if cond1 OR if cond2:
do_something.

I've tried using the C syntax for OR (||) but python complained. I'm sure
there's a way to do this rather than using if cond1: elif cond2: both with
the same code to execute.

Please pass me a pointer so I can learn how to correctly write this.

Rich
For lots of conditions:

import operator
reduce(operator.or_, list_of_conditions)

e.g.:

pyimport operator
pylist_of_conditions = [
.... 'big' < 'small',
.... 'all' 'half',
.... 'five' 'one',
.... 'six' < 'seven'
.... ]
pylist_of_conditions
[True, False, False, False]
pyreduce(operator.or_, list_of_conditions)
True

James
Feb 8 '07 #6
On 2007-02-08, Paul Rubin <httpwrote:
rs******@nospam.appl-ecosys.com writes:
> if cond1:
if cond2:
do_something.

You can write:
if cond1 and cond2:
do_something
> if cond1 OR if cond2:
do_something.

if cond1 or cond2:
do_something
> I've tried using the C syntax for OR (||) but python complained. I'm sure
there's a way to do this rather than using if cond1: elif cond2: both with
the same code to execute.

Python uses the "and" and "or" keywords for && and ||.
Allow me to thank all of you who responded with this one article. For
whatever reason, it did not occur to me to use the words 'and' and 'or.'
And, I did not see this in the tutorial or introduction ... which is my
fault.

So, I do thank all of you.

Rich
Feb 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
99
by: Paul McGuire | last post by:
There are a number of messages on the python-dev mail list that indicate that Guido is looking for some concensus to come from this list as to what *one* alternative syntax for decorators we would...
3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
170
by: 43 | last post by:
how come m$Office isn't written in .net? how come Open Office isn't written in j2ee? how come dbms systems aren't written in either? how come browsers aren't written in either? how come...
7
by: maruthir123 | last post by:
Hi, I have a wchar_t pointer. Based on some conditions I assign string literals to it and on some other conditions, I allocate memory and assign it to this. Is there a way to find out while...
27
by: rogz | last post by:
Hello, i have a question about "design" issues in C. In ACMqueue of february (article here: http://www.acmqueue.org/modules.php?name=Content&pa=showpage&pid=364 ), KV says:
3
by: =?Utf-8?B?WFNsZXI=?= | last post by:
I have some code that works fine, except for the fact that it has a lot of embedded if statements. I don't think that this would be practice code, but I wanted to tap into the knowledge of this...
4
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.