> For the complete documentation index, see [llms.txt](https://calnotes.gitbook.io/cs61a-guidebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnotes.gitbook.io/cs61a-guidebook/building-blocks/control-flow/what-are-logical-operators/logical-operators-seemingly-illogical-behavior.md).

# Logical Operators, Seemingly Illogical Behavior

And now an example that contains both `and` and `or`:

```python
>>> 1 or 0 and 1/0 or 2
```

And this statement gets tricky to comprehend! Here's the core idea: **the `and` operator takes precedence over the `or` operator.**

In simpler English, what this means is that the statement looks something like this:

```python
>>> 1 or (0 and 1/0) or 2
```

This statement first evaluates `1`, finds it to be a truthy value, and immediately returns it. This is why the `1/0` doesn't throw an error.

Try this one yourself:

```python
>>> 0 or None and 1/0 or 2
```

Now that you've given this a shot, let's walk through it together!&#x20;

```python
>>> 0 or None and 1/0 or 2
>>> 0 or (None and 1/0) or 2
```

First, the `or` statement evaluates 0. Finding it to be a false-y value, it keeps going. It evaluates `(None and 1/0)` next. In this, the `and` finds `None` to be the first false-y value, and immediately returns it. The outer `or` statements sees `None` as the next value, and keeps going. It therefore returns `2`, the first truth-y value it finds!&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://calnotes.gitbook.io/cs61a-guidebook/building-blocks/control-flow/what-are-logical-operators/logical-operators-seemingly-illogical-behavior.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
