Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Understanding "de Morgan's laws"

While trying to understand logical 'or'/'and', I encountered another problem (I'm writing Python code here, but my question is about the logic, not about any given programming language). I have som...

1 answer  ·  posted 1d ago by Karl Knechtel‭  ·  last activity 1d ago by Karl Knechtel‭

#1: Initial revision by user avatar Karl Knechtel‭ · 2024-11-13T22:11:03Z (1 day ago)
Understanding "de Morgan's laws"
While trying to [understand logical 'or'/'and'](https://software.codidact.com/posts/292968), I encountered another problem (I'm writing Python code here, but my question is about the logic, not about any given programming language). I have some code like:

    if x or y:
        pass
    else:
        do_something()

(Of course, following the other Q&A, I made sure that the real `x` and `y` make sense independently as conditions.)

I wanted to focus on the `else` case, because it's actually interesting. I figured out that I could *negate* the condition to flip the cases, and then the `else: pass` part would be unnecessary:
```python
if not(x or y):
    do_something()
```
However, *other obvious attempts did not work*:
```python
if not x or y:
    do_something()

if not x or not y:
    do_something()

if (not x) or (not y): # not even with more parentheses!
    do_something()
```
I asked about this elsewhere and was told it had something to do with "de Morgan's laws". What does this mean? Can I use these "laws" to understand how to write the code correctly? How exactly do they apply?