473,785 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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 1982
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******@nospa m.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_conditi ons)

e.g.:

pyimport operator
pylist_of_condi tions = [
.... 'big' < 'small',
.... 'all' 'half',
.... 'five' 'one',
.... 'six' < 'seven'
.... ]
pylist_of_condi tions
[True, False, False, False]
pyreduce(operat or.or_, list_of_conditi ons)
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
6073
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 containing norwegian special characters æ, ø and å. >>> liste =
99
4667
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 like him to consider in place of the @ syntax that is currently in 2.4a2. I think special thanks are due to: - Anthony Baxter for his continuing efforts in this regard - Steven Bethard for some of the clearest thinking and writing on this topic...
3
44534
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 to read (versus the format I have used for years). I have tested the queries below using the SQL Profiler, and both have identical costs. Is there any advantage of one format over the other?
177
7088
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 programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
170
6038
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 RealPlayer, MediaPlayer and all applications that need speed are written in c++ ? (except for
7
1284
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 deleting if the memory is deletable. I get an error if I try to delete this pointer when Literals are assigned. Thanks, Maruthi
27
5383
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
3606
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 group, and see what is the best way to handle it. Basically, the if statements are used to check several conditions of whether something is activated etc., and if it is, it outputs a small piece of text and redirects the user to a new page. For each...
4
2666
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 used Case statements instead. I've read several different examples of Case statements, but can't quite figure out the syntax, or if they can be used when you need to test for a pair of conditions, as I am doing below.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.