This code doesn't make sense for me

How True and True … etc?
img

Hi @Maho,

Here and is one of the so-called logical operators (like we have arithmetic operators: +, -, *, /). The usage of this operator and is to return True if both statements are true, and False otherwise (if at least one of the statements is false). For example, if you write

print(1<3 and 5>2)

since both of the statements are true, the result will be True. In another case

print(1<3 and 5>10)

since at least one expression (the second one) is not true, the whole result will be False

In your examples (print(True and True), etc.), you don’t have any expression to estimate, but just directly True and False values in different combinations, to compare between them. If we look at the usage of the and operator (in bold at the beginning of my message), we’ll see that only in case print(True and True) we’ll receive the result True, since both of the “expressions” (well, ultra-short in this case) are true.

1 Like

I understand the and operator but these expressions like ( True and False ) ( 7 and 7 [ I have tried it and the output was 7 I don’t know how!] ) don’t have any meaning for me, unlike this expression ( 5==5 and 5!=7 ) I understand this

When you’re using non-boolean values (like real numbers) with the and operator, the logic is the following: the and returns the first “falsy” value if there are any, otherwise it returns the last value in the whole expression. And among the numbers the only “falsy” value is 0. There are other “falsy” values (for example, None, empty list [], empty string ""), but they are not numbers.

For example:

print(7 and 10 and 3 and 56 and 8)

will return 8 (since there are no “falsy” values, it returns just the last value of the whole expression). In your example with print(7 and 7) it was the second 7 which was printed. If you try instead:

print(7 and 0 and 3 and False and 8)

the output will be 0, since it’s the first “falsy” value (even the real False will be ignored in this case!).

This is how this and works in a broader context. But of course, the major (and the most reasonable) use of it is related to working with boolean expressions. And yes, using it with the expressions like print(True and True) or print(9 and 7) doesn’t obviously have any practical meaning. In that mission screen, such examples were introduced only as very simplified examples of how the and operator works, without getting into the broader context of its usage.

1 Like

Thanks a lot.
Does Your clarification also work on the other logical operators ( or / not )?

With or, in a broader context (like in my last message for and), it works just the opposite: it returns the first “truthy” value (“truthy” values are all the values that are not “falsy”, i.e. the great majority of values). If all the values in the expression are “falsy”, then it returns the last value (in this sense, it’s analogous to and). For example:

print(0 or 10 or 0 or 56 or 8)

will return 10, since it’s the first “truthy” value. But in this exampe:

print(0 or [] or False or None)

it will return None, since there are no “truthy” values at all, so it returns just the last value of the whole expression.

In more “normal” context, when we use or with boolean expressions, with which it is actually mostly used, the or operator returns True if at least one of the expressions is True. Well, strictly speaking, this case is anyway a particular case of a broader context, described above.

1 Like

Ah, and about not operator, it always renders the value to its opposite boolean. I mean, if the value (or the expression) is True or “truthy”, it returns False, and if it is False or “falsy”, it returns True.
For example:

print(not 0)

will return True, since 0 is a “falsy” value. Another example:

print(not 2<3)

will return False, since the expression after not is actually True.

Of course, having all that in mind, you can use this not in any combinations with the and and or operators, taking into account also their own (and and or) logics.

1 Like

Thanks a lot for the clarification.

1 Like

I am very sorry about annoying you, but what about using the comparison operators with non-numbers like ( ‘good’ != ‘bad’ ) or with numbers and strings like ( ‘hello’ > 5 )?

1 Like

As for numbers and strings, in Python 3 you cannot compare them directly, even it these values are, for example, “10” and 10. If one of the values to be compared represents a number in a form of string (like our “10”, in this case), you have first to render it in int and only then compare:

int("10") == 10

This expression will return True, since we rendered the string value “10” into int type. Otherwise, if we try your expression 'hello' > 5, we’ll just receive TypeError, since the types of the values to be compared are different.

As for comparing two values of the string type, here another principle works, probably a bit more complicated: the strings are compared from left to right character by character, and every time the so-called Unicode values of both characters are compared (let’s say, each character has its own code in that Unicode system, and all those codes have their hierarchy). For example:

'cat' > 'car'

will return True since the unicode value of ‘t’ is higher than that of ‘r’ (the previous characters ‘c’ and ‘a’ are the same for both words, and go in the same order, hence they are equal). And this comparison:

'car' > 'care'

will return False, since the second string value is longer.

1 Like

I am very thankful to you.

1 Like

While I am practicing, I have found this question.
I think - as I have understood from you - it is supposed to appear an error message because you have told me ( " Otherwise, if we try your expression 'hello' > 5, we’ll just receive TypeError, since the types of the values to be compared are different. " )

This also doesn’t make sense for me ( how if True? ).

And this also ( why ‘D’ wouldn’t be printed? ) I think this weird condition ( False and False ) is True.

  1. Yes, “B” will be printed here, because ‘10’ is not equal to 10. In Python 3, TypeError is thrown when you use exactly the comparison operators with > or < as a part of them (I mean <, >, >=, and <=). The == operator is, let’s say, an exception here, but even without throwing this error, these two values ‘10’ and 10, being of different types, are not equal in any case. So be careful and never use values of string and int types in the same comparison (including the equality ==), to avoid confusion. Probably, in the future versions of Python also using the == operator will throw this error.
  2. This if True: is another very simplified and a little bit crazy example. Well, True is obviously always true, so of course ‘A’ will be always printed in this loop.
  3. No, the and operator returns True only when all (here - both) booleans (we are not talking here about a broader context of using non-boolean values, as in one of my previous messages, for not complicating too much the situation) are true. Hence, from all the combinations of True / False, only True and True will return True, and no other combination will do so.
1 Like

Really, thanks a lot.

1 Like

No problem, you are welcome @Maho

It’s a simple AND Logic Gate that’s all.

Sorry, I don’t understand.