How to check logical value of an expression in the interactive shell? - python

Sometimes I want to check the logical value of an expression, so I type it in Python (or IPython) and get the result:
>>> a==3
True
But in other cases it doesn't work that way (here string is None):
>>> string
>>>
So I check the logical value like this:
>>> if string: print('True')
...
>>>
Is there a shorter way for checking the logical value of an expression?
Any function that returns True or False the same way it will be evaluated in an if-condition?

All expressions except those that produce None are echoed.
If you execute an expression that doesn't result in data being echoed, then you probably have a None result. You can test for None explicitly:
>>> string = None
>>> string is None
True
Using is None produces a non-None result.
If you just wanted to test for the boolean value of an expression, use the bool() function; it'll return a result following the standard truth value testing rules:
>>> string = None
>>> bool(string)
False
Again, this is guaranteed to not be None and echoed. You can't distinguish between None and other false-y values this way, however. An empty string '' will also result in False, for example.
Another alternative is to explicitly print all expression results, using the repr() function:
>>> print(repr(string))
None
This then produces the exact same output as echoing does, with the single exception that None is printed too.

Is there a shorter way for checking the logical value of an
expression?
Yes, it's the bool() function:
>>> string = None
>>> bool(string)
False

Yes, create a boolean directly from the expression, which will always be True or False:
>>> bool(string)
False

string is None or string is not None will return the boolean you need.
>>> string = None
>>> string is None
>>> True

Related

Boolean evaluation of a string without conditionals

I'm trying to figure out the solution to this particular challenge and so far I'm stumped.
Basically, what I'm looking to do is:
Check if substring false exists in string s
If false exists in s, return the boolean True
However, I am not allowed to use any conditional statements at all.
Maybe there is a way to do this with objects?
There is always str.__contains__ if it's needed as a function somewhere:
In [69]: str.__contains__('**foo**', 'foo')
Out[69]: True
This could be used for things like filter or map:
sorted(some_list, key=partial(str.__contains__,'**foo**'))
The much more common usecase is to assign a truth value for each element in a list using a comprehension. Then we can make use of the in keyword in Python:
In[70]: ['foo' in x for x in ['**foo**','abc']]
Out[70]: [True, False]
The latter should always be preferred. There are edge cases where only a function might be possible.
But even then you could pass a lambda and use the statement:
sorted(some_list, key=lambda x: 'foo' in x)
Evaluating condition without using if statement:
True:
>>> s = 'abcdefalse'
>>> 'false' in s
True
False:
>>> s = 'abcdefals'
>>> 'false' in s
False
Return blank if False:
>>> s = 'abcdefals'
>>> 'false' in s or ''
''

'if' not followed by conditional statement

I'm going through Zed's "Learn Python The Hard Way" and I'm on ex49. I'm quite confused by the following code he gives:
def peek(word_list):
if word_list: # this gives me trouble
word = word_list[0]
return word[0]
else:
return None
The condition of the if statement is giving me trouble, as commented. I'm not sure what this means as word_list is an object, not a conditional statement. How can word_list, just by itself, follow if?
The if statement applies the built-in bool() function to the expression which follows. In your case, the code-block inside the if statement only runs if bool(word_list) is True.
Different objects in Python evaluate to either True or False in a Boolean context. These objects are considered to be 'Truthy' or 'Falsy'. For example:
In [180]: bool('abc')
Out[180]: True
In [181]: bool('')
Out[181]: False
In [182]: bool([1, 2, 4])
Out[182]: True
In [183]: bool([])
Out[183]: False
In [184]: bool(None)
Out[184]: False
The above are examples of the fact that:
strings of length >= 1 are Truthy.
empty strings are Falsy.
lists of length >= 1 are Truthy.
empty lists are Falsy.
None is Falsy.
So: if word_list will evaluate to True if it is a non-empty list. However, if it is an empty list or None it will evaluate to False.
He is checking if word_list is empty or not. If a list is empty and it is used in a conditional statement, it is evaluated to False. Otherwise, it is evaluated to True.
word_list = ['some value']
if word_list:
# list is not empty do some stuff
print "I WILL PRINT"
word_list = []
if word_list:
# list is empty
print "I WILL NOT PRINT"
In the above code, only the first snippet will print.
See the following reference: https://docs.python.org/2/library/stdtypes.html#truth-value-testing
word_list is a list and when you use it for an if statement condition you check word_list is empty or not :
word_list = []
bool(word_list) # False
if word_list :
print "I'm not empty" # would not printed
word_list = ['a']
bool(word_list) # True
if word_list :
print word_list[0] # 'a'
as Mad Physicist said even None elements in a list means that it's not empty:
word_list = [None]
bool(word_list) # True
What is required for an if block is just something that can be evaluated either to True or to False. A conditional evaluates directly to one of those, but there are other objects that can be converted. To see what any given object is, you can use bool:
>>> mylist = []
>>> bool(mylist)
False
>>> mylist = [4, 3, 6]
>>> bool(mylist)
True
You see, a list is False if it is empty, but True otherwise. Therefore, the if word_list: block will be evaluated if word_list is nonempty. Strings also are False if they are empty, but True otherwise. Same thing with tuples, dictionaries, sets. With numbers, 0 and 0.0 are False, but any other number is True. A fairly common argument to give to indicate to the function to come up with its own value is None which evaluates to False, so the if not mylist: block will be executed if mylist is empty or if mylist is None. (It would also be executed if mylist is 0, (), {}, etc.; but it's unlikely that mylist would be given those)
Take a look at this docs page for Truth Value Testing in python. You should get clear idea about your situation after reading this. Here is the relevant part for easy access.
5.1. Truth Value Testing
Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below. The following
values are considered false:
None
False
zero of any numeric type, for example, 0, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a __bool__()
or __len__() method, when that method returns the integer zero or bool
value False.
All other values are considered true — so objects of many types are always true.
Read the first sentence (bolded) again, and note the bolded parts in the fourth rule. This relates to your question.
So, according to the 4th rule, if your word_list is empty, the condition evaluates to False, otherwise it evaluates to True.
I know you trust in the docs, but here is a code snippet to actually test the truth values for yourself. (I know it is needless to do something like this, but I am always tempted to see things with my own eyes)
def test_truth_value(arg):
# ANY object can be evaluated for truth or false in python
if arg: # or to be more verbose "if arg is True"
print("'{}' is True".format(arg))
else:
print("'{}' is False".format(arg))
class dummy_length_zero():
def __len__(self):
return 0
def __str__(self):
return 'instance of class: "dummy_length_zero"'
class dummy_bool_False():
def __bool__(self):
return False
def __str__(self):
return 'instance of class: "dummy_bool_False"'
obj_dummy_0 = dummy_length_zero()
obj_dummy_false = dummy_bool_False()
args = [None, False, 0, 0.0, 0j, '', (), [], {}, obj_dummy_0, obj_dummy_false]
for arg in args:
test_truth_value(arg)
And lastly, to test that last statement so objects of many types are always true, just remove the implementation of __len__() or __bool__() method from dummy_length_zero or dummy_bool_False class respectively, and check for truth.
In python, everything has an implicit boolean value. Putting any object in an if statement directly is equivalent (but more Pythonic than) doing if bool(word_list):. None, empty sequences, empty sets, empty dicts, 0, False, 0.0 all evaluate to False. Most other objects evaluate to True. That makes if word_list: the most Pythonic way of ensuring that the list is not None or empty before accessing the first element. The long way of expressing the same thing would be if word_list is not None and len(word_list) > 0:.
In Python, every expression can be evaluated to a boolean value (i.e. either True or False).
The following, basic, expressions evaluate to False
The keyword False (obviously!)
The keyword None
The number 0 (0, 0.0 ... )
empty sequence (tuple, list, string)
empty mapping (dictionary)
All other expressions evaluate to True.
So, what the if statement does is evaluating the expression that follows the if keyword to either True or False, then act accordingly.
So in your specific example, if word_list matches any of the above cases it will be considered False, otherwise, it will be considered True.
[#] reference

Write an expression whose value is true if and only if x is NOT a letter

Assume that x is a string variable that has been given a value. Write an expression whose value is true if and only if x is NOT a letter.
def isLetter(ch):
import string
return len(ch) == 1 and ch in string.ascii_letters
print(isLetter('A'))
True
If you want to check the type of the variable x, you can use the following:
if type(x) is str:
print 'is a string'
In python, String and Char will have the same type and same output unlike languages like java.
type(chr(65)) == str
type('A') == str
EDIT:
As #Kay suggested, you should use isinstance(foo, Bar) instead of type(foo) is bar since isinstance is checking for inheritance while type does not.
See this for more details about isinstance vs type
Using isinstance will also support unicode strings.
isinstance(u"A", basestring)
>>> true
# Here is an example of why isinstance is better than type
type(u"A") is str
>>> false
type(u"A") is basestring
>>> false
type(u"A") is unicode
>>> true
EDIT 2:
Using regex to validate ONLY ONE letter
import re
re.match("^[a-zA-Z]$", "a") is not None
>>> True
re.match("^[a-zA-Z]$", "0") is not None
>>> False
Turns out the answer to it is not((x>='A' and x<='Z') or (x>='a' and x<='z'))
Use regular expression. http://www.pythex.org is a good learning spot, official docs here: https://docs.python.org/2/library/re.html
something like this should work though:
if x != '[a-zA-Z]':

Python - Interesting boolean and string behavior [duplicate]

This question already has answers here:
Is short-circuiting in assignment statements considered good style?
(2 answers)
Closed 7 years ago.
I was reading through an API's documentation when I came across a curious little set of statements:
self.use_ssl = kwargs.get('use_ssl', True)
self.scheme = self.use_ssl and 'https://' or 'http://'
After doing some personal testing, I found that if self.use_ssl was set to True, self.scheme would be set to use HTTPS, and HTTP if self.use_ssl was False. Awesomely pythonic, and I'll definitely be stealing this.
Can somebody explain exactly how this works?
In python, an empty string is equivalent to False, a non empty string is equivalent to True
>>> bool('')
False
>>> bool('foo')
True
The behavior of a boolean expression is described in the python 2 documentation, and is the same for python 3.
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
That's why you get the string 'https://' or 'http://' depending on the value of 'self.use_ssl'
Some examples, from the python console:
>>> True or ''
True
>>> True or 'foo'
True
>>> False or ''
''
>>> False or 'foo'
'foo'
>>> '' or True
True
>>> '' or False
False
>>> 'bar' or True
'bar'
>>> 'bar' or False
'bar'
>>> True and ''
''
>>> True and 'foo'
'foo'
>>> False and ''
False
>>> False and 'foo'
False
>>> '' and True
''
>>> '' and False
''
>>> 'bar' and True
True
>>> 'bar' and False
False
You can always convert a boolean expression to a real boolean value using bool()
>>> 1 and 'bar'
'bar'
>>> bool(1 and 'bar')
True
This trick, a and b or c, only works when b itself is a "truthy" value. Consider True and "" or "foo". You might expect it to produce the empty string, but it will produce foo, because True and "" results in the empty string, which is considered False when evaluating "" or "foo". The correct way would be to wrap b and c in a list to guarantee that the second argument to and is truthy (and that the overall result is a list in either case), then extract the first value of that list:
(a and [b] or [c])[0]
To avoid the clunkiness (and inefficiency) of creating and indexing a temporary list, Python introduced the conditional expression:
b if a else c
which does not rely on b having any particular boolean value.
it works like this:
when python interapt this it first checks the self.use_ssl if it is True
it continues in the AND chain to see if the next statement is True.
since the next statement is a string (and not an empty one) it is True
so there is no need to continue to the or since this statement is definetly True already, so the last value is used witch was 'https'
if use_ssl was false then there is no need to evaluate the and part of the condition since the first part is already false so python "skip" it and continue to the or part to check if that is True, since it is again a non empty string it is true, and again the last value used is "returned"

How do boolean operations work with parentheses in python 2.7?

Found this little oddity while playing around.
>>> 'Hello' == ('Hello' or 'World')
True
>>> 'Hello' == ('World' or 'Hello')
False
>>> 'Hello' == ('Hello' and 'World')
False
>>> 'Hello' == ('World' and 'Hello')
True
Is there some trick to this logic that I'm not getting? Why is the order of the strings the determining factor of these queries? Should I not be using parentheses at all? Why does changing to "and" flip the outputs?
Thanks a buncharooni.
In Python, all objects may be considered "truthy" or "falsy". Python uses this fact to create a sneaky shortcut when evaluating boolean logic. If it encounters a value that would allow the logic to "short-circuit", such as a True at the beginning of an or, or a False at the start of an and, it simply returns the definitive value. This works because that value itself evaluates appropriately to be truthy or falsy, and therefore whatever boolean context it's being used in continues to function as expected. In fact, such operations always just return the first value they encounter that allows them to fully evaluate the expression (even if it's the last value).
# "short-circuit" behavior
>>> 2 or 0
2
>>> 0 and 2
0
# "normal" (fully-evaluated) behavior
>>> 'cat' and 'dog'
'dog'
>>> 0 or 2
2
x or y returns the first operand if its truthy, otherwise returns
the second operand.
x and y returns the first operand if its
falsey, otherwise returns the second operand.
For what it looks like you're trying to accomplish you may prefer this:
'Hello' in ['Hello', 'World']

Categories

Resources