Python: Negation
Along with the logical operators AND and OR, there is also an operation called “negation” It changes the logical meaning to the opposite. In programming, negation corresponds to the unary operator not:
not True # False
not False # TrueFor example, if there's a function that checks if a number is even, then you can use negation to check if a number is odd:
def is_even(number):
return number % 2 == 0
print(is_even(10)) # => True
print(not is_even(10)) # => FalseIn the example above, we added not to the left of the function call and got the opposite action.
Negation is a tool with which you can express intended rules in code without writing new functions.
If you write not is_even(10), the code will still work:
print(not not is_even(10)) # => TrueIn logic, double negation means positive:
not not True # True
not not False # False
print(not not is_even(10)) # => True
print(not not is_even(11)) # => FalseNow you know what the operators AND, OR and not mean. They allow you to specify compound conditions with two or more logical expressions.
Instructions
-
Implement a function called
is_palindrome()that determines whether a word is a palindrome or not. A palindrome is a word that reads the same way in both directions. Words may be passed to the function in any case, so you must first convert the word to lowercase:word.lower().is_palindrome('hut') # true is_palindrome('hexlet') # false is_palindrome('Argument') # true is_palindrome('Function') # false -
Implement a function called
is_not_palindrome()which checks if a word is NOT a palindrome:is_not_palindrome('шалаш') # false is_not_palindrome('Ага') # false is_not_palindrome('хекслет') # trueTo do this, call
is_palindrome()insideis_not_palindrome()and apply negation.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import solution
def test1():
assert not solution.is_not_palindrome("wow")
assert solution.is_not_palindrome("hexlet")
assert not solution.is_not_palindrome("asdffdsa")
assert not solution.is_not_palindrome("Wow")
assert solution.is_not_palindrome("CodeBasics")Teacher's solution will be available in:
20:00
