Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

how do you write these as boolean expressions: Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as "true." >>> x = 5 >>> x and 1 1 >>> y = 0 >>> y and 1 0

OpenStudy (anonymous):

"In general, this sort of thing is not considered good style. If you want to compare a value to zero, you should do it explicitly. "

OpenStudy (anonymous):

you mean the bitwise operations? | for bitwise OR (inclusive) ^ for bitwise XOR & for bitwise AND

OpenStudy (anonymous):

oh and ~ for bitwise negation

OpenStudy (anonymous):

"There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10. n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is divisible by 2 or 3. Finally, the not operator negates a boolean expression, so not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y. Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as "true." "

OpenStudy (anonymous):

"text between the quotes is from the readings"

OpenStudy (anonymous):

that paragraph is right. In Python, when numbers are used within boolean expressions or tests, they evaluate to True if they are not equal to zero (or an empty list etc.. you should check them out).

OpenStudy (anonymous):

""""Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as "true."""" How do i do this here he wants the operands of the logical operators to be boolean expressions

OpenStudy (anonymous):

A valid boolean expression evaluates to True or False.

OpenStudy (anonymous):

so put in anything that evaluates to True or False, whether it be a boolean object, a number, or another expression :-D

OpenStudy (anonymous):

ya im dumb

OpenStudy (anonymous):

x = 5 x == 7 or x == 1

OpenStudy (anonymous):

x = 8 x == 8 and x == 6 0

OpenStudy (anonymous):

The operators "and" and "or" are boolean operators, just like "+" and "*" are numerical operators. However, python has decided to also provide definitions for the "and" and "or" numerical operators. Here are some numerical operations and their results: 5 + 1 = 6 5 - 1 = 4 5 * 1 = 5 5 / 1 = 5 5 and 1 = 1 5 or 1 = 5 5 and 0 = 0 5 or 0 = 5 Here are some boolean operations and their results: True and False = False True or False = True (4 == 4) = True (4 == 0) = False (0 == 0) = True Asking to rewrite "5 and 1" as a boolean expression makes as much sense as asking to rewrite "5 + 1" as a boolean expression. Neither expression ever involves any boolean values, nor do they produce boolean values. The other thing python does to cloud the issue is to interpret non-boolean values as boolean values. Hence 0, None, and empty sequences are *equivalent* to False (they are not False) when used in a boolean context. All other values are *equivalent* to True. Note that python does not *convert* the values to True or False, it just *interprets* them as True or False. Yet another confusing thing is that python will let you mix operand types, so you can use the "and" and "or" operators with one boolean operand and one numerical operand. True or 5 = True True and 5 = 5 True and 0 = 0 False or 8 = 8 False and 8 = False True or [] = True True and [] = [] You can think of the "and" operator like this: if the first operand is equivalent to False: return the first operand return the second operand You can think of the "or" operator like this: if the first operand is equivalent to True: return the first operand return the second operand The reason this is so confusing is *because it's so confusing*. That's why the author, and the people on this forum, keep saying to not ever do this. Code is written to be read by humans. This kind of syntax is unreadable by humans. Here are some statements. Try to figure out what they'll return before running them in the interpreter. Maybe even just try to figure out the *type* of the return value. I couldn't do it. >>> x = 4 >>> x == 5 and 6 >>> x == 5 or 6 >>> x = 5 >>> x == 5 and 6 >>> x == 5 or 6 >>> x = 6 >>> x == 5 and 6 >>> x == 5 or 6

OpenStudy (anonymous):

i understand how all the boolean expressions works but what are some examples of when you should be using them as boolean expressions. I need some visual representations of what i should be doing when writing my programs 9pulling it out of this section on boolean values)

OpenStudy (anonymous):

"Boolean" implies "true or false." So if you need to know if something is true or false, use a boolean variable or a boolean expression. Don't use a non-boolean value or expression. "If" conditions and "while" conditions are inherently boolean. Though python allows you to use non-boolean values in conditional statements, be more explicit and turn it in to a boolean expression. Here's a quiz. Give the type of the variable x in these examples: 1. if (x): print x 2. if (x): print x 3. if (x): print x Here's another quiz. Same instructions: 1. if (x != 0): print x 2. if (len(x) != 0): print x 3. if (x): print x It goes back to making code look like English. "x" is not an English sentence, so don't use it like one. Imagine if your math teacher said "The fraction a/b is defined only if b." Only if b is what?! Seems like part of that sentence is missing. Don't make me have to parse that sentence when reading your code. Writing code for a compiler and for a human are two different things. Write code to be read by humans first, and compilers second.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!