If not... followed by function - python

I am stuck with this question. I am not too concerned about what each function does, but more importantly how does the IF statement work with functions. From my understanding, the IF.... or statements usually work with a condition, but for this scenario it only involves two functions without any conditions?
def disk_check_usage(disk):
du = shutil.disk_usage(disk)
free = du.free/du.total * 100
return free > 20
def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage < 75
if not disk_check_usage("/") or not check_cpu_usage():
print("ERROR!")
else:
print("Everything is OK")
I want it to give an 'Error!' message when both conditions (free > 20 and usage < 75) are not True/Satisfied.
Edit: When I run the code, 'free' = 17 which gives 'False' and Usage < 75 which gives 'True'. So my IF statement would mean 'If not False or not True:'. What does that mean and how does the system whether to run 'if' or 'else' statement?
Any help will be appreciated!

They are some operations you can do with booleans.
In particular, (not a) or (not b) is equivalent to not(a and b); at least one of the two have to be False
You just have to inverse your tow statements: if everything is ok, print ok else print error
if disk_check_usage("/") and check_cpu_usage():
print("Everything is OK")
else:
print("Error")

From my understanding, the IF.... or statements usually work with a
condition, but for this scenario it only involves two functions
without any conditions?
There's always a condition. The code you posted is no exception.
What you call a "condition" is really just a boolean expression in a specific context. If the entire expression evaluates to True, you enter the body of the if-statement. Otherwise, if it evaluates to False, you don't.
Example
The following code iterates over the characters in a string, and only prints characters which are both alphabetic and uppercase:
string = "Hell2O]WoR3(Ld"
for char in string:
if char.isalpha() and char.isupper():
print(char)
Output:
H
O
W
R
L
>>>
str.isalpha returns a boolean - True if the string (in this case a string consisting of a single character) contains only alphabetic characters, False otherwise.
str.isupper returns a boolean - True if the string (again, just a single character in this case) contains only uppercase characters, False otherwise.
I picked these two string methods to simulate the two functions you have in your code, since they also return booleans.
Let's take the first character, "H":
>>> char = "H"
>>> char.isalpha()
True
>>> char.isupper()
True
>>> char.isalpha() and char.isupper()
True
>>>
You see, the entire boolean expression char.isalpha() and char.isupper() evaluates to True. In order for the entire expression to be evaluated, both functions need to be called - you can think of their return values effectively "replacing" their respective function calls. When char is "H", after calling the functions, the expression really looks like this:
True and True
Which evaluates to True, so we enter the body of the if-statement.
In your case, you used or instead of and. What I said about boolean expressions collapsing down into a single True or False value still applies, the only difference would be in short-circuiting, which isn't that relevant to your question I think.
Example
Here's something closer to what you have. Imagine a scenario where you want to monitor the temperature and radiation of something. If either the temperature OR the radiation levels are not nominal, we trigger an alarm.
def is_temperature_nominal(temp):
return 70 <= temp <= 100
def is_radiation_nominal(rad):
return rad < 200
if not is_temperature_nominal(250) or not is_radiation_nominal(50):
print("Alarm triggered!")
else:
print("Everything is good.")
Output:
Alarm triggered!
>>>
I picked the nominal ranges arbitrarily, and I picked the arguments 250 and 50 arbitrarily as well. With these two hardcoded values, the boolean expression would look like this after calling the two functions:
not False or not True
Which is another way of saying:
True or False
With an or, only one of the operands has to be True in order for the entire expression to evaluate to True - therefore the entire expression evaluates to True, and we enter the body of the if-statement, triggering the alarm (because only one of the values needs to be not nominal in order for the alarm to trigger. If both values are not nominal it would also trigger the alarm. The only way we enter the body of the else is if both values are nominal).

From my understanding, the IF.... or statements usually work with a
condition, but for this scenario it only involves two functions
without any conditions
if (as well as while) needs an expression that evaluates to a logical (type bool) value (either True or False), or can be typecast to a logical value (for example None is typecast to False).
In your case, the conditions (namely, comparisons) are done inside your functions (see the return statements), so that the functions already return logical values.

When calling a method, it's executed and return a value, which can be use in different manner : directly or stored in a variable, this code is exactly the same as the following, but just not saving in variables and inline the methods in the condition
disck_check = disk_check_usage("/") # True or False
cpu_check = check_cpu_usage() # True or False
if not disk_check or not cpu_check: # Boolean conbination
print("ERROR!")
else:
print("Everything is OK")

You have to use 'and' operator instead the 'or' used by you.
def disk_check_usage(disk):
du = shutil.disk_usage(disk)
free = du.free/du.total * 100
return free > 20
def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage < 75
if not disk_check_usage("/") and not check_cpu_usage():
print("ERROR!")
else:
print("Everything is OK")

Related

Why does the str.isdigit always output the opposite result

def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if (len(zip_code) == 5) and (str.isnumeric == True):
return True
else :
return False
First of all, it should be str.isnumeric() == True as that's calling the isnumeric function. Second of all you should be really using str.isdigit().
str.isnumeric()
In Python, decimal characters (like: 0, 1, 2..), digits (like: subscript, superscript), and characters having Unicode numeric value property (like: fraction, roman numerals, currency numerators) are all considered numeric characters. Therefore even japanese character for 1, 2 and 3 would pass this check.
str.isdigit()
On the other hand isdigit() will only return True if all characters in a string are digits. If not, it returns False.
source: https://www.programiz.com/python-programming/methods/string/isdigit
A few point to discuss. Regarding your condition:
str.isnumeric == True
That thing on the left side is the function itself, not a call to the function giving a result, the latter would be some_string.isnumeric().
The chances of the function object being equal to true are somewhere between zero and a very, very small number :-)
It's also redundant to compare boolean values against boolean constants since the result of the comparison is just another boolean value. Where do you stop in that case? For example:
(((some_bool_value == True) == True) == True) != False ...
Another point, the code form if cond then return true else return false can be replaced with the much less verbose return cond.
And also keep in mind that isnumeric() allows other things than raw digits, like ¾. If you just want the digits, you're probably better off with another method. You may be tempted to instead use isdigit(), but even that allows other things than just what most would consider "normal" digits, such as allowing "90²10" as a postal code, presumably the much trendier part of Beverly Hills :-).
If you only wanted the raw digits 0-9 (which is probably the case with US postal codes like you seem to be targeting), neither isnumeric() nor isdigit() is really suitable.
An implementation of the function, taking all that into account, could be as follows:
def is_valid_zip(zip_code):
if len(zip_code) != 5:
return False
return all([x in "1234567890" for x in zip_code])
it should be zip_code.isnumeric() not str.isnumeric
Also, why don't you use regex:
import re
RE_ZIP_CODE = re.compile(r'^[0-9]{5}$') # or r'^\d{5}$' for all digit characters
def is_valid_zip(zip_code):
return RE_ZIP_CODE.search(zip_code) is not None
This should work
def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if len(zip_code) == 5 and zip_code.isnumeric():
return True
else:
return False
print(is_valid_zip("98909"))

Python if statements and truth value testing for non compsci background

This aims to be a self answered question after a few hours of digging, I found this thought process may prove useful to others who also may not have come from a formal compsci background.
This all started from confusion over why one particular if statement was being entered.
>>>if (2 & 2):
... print("true")
true
Why was this if statement being entered?
My previous use of if statements were all quite straightforward, so I took it for granted and with the understanding that only a boolean True would result in the entering of an if statement. What constitutes a boolean True, was not so obvious to me after running into this if statement. This is surely obvious to those with a compsci background, but I never really dug into how if statements determined if the argument was a boolean True or not. For example, if (5 > 2) --> True just makes sense to anyone with at least an elementary-school math background, so I took it for granted. On the contrary, if (2) --> True, does not seem too obvious to a non compsci specialist.
For example
>>>2 & 2
2
>>> 2 == True
False
>>>if (2):
... print("true")
true
Why was the if statement entered despite 2 & 2 evaluating to an int value of 2? Why was the if statement entered if given the int value 2 even though 2 itself is not == True? This was the first I had seen this type of behavior and it lead me to understand from various other stack overflow questions that the if statement is not evaluating if the int value of 2 == True, but instead evaluating bool(2) == True, which is in fact True.
Again, if you come from a comp sci background, I'm sure this is all very obvious, but even the idea of bool(2) and "truth value testing" as a phrase was new to me, so adding this on to the binary logical operators caused me a fair bit of confusion at first. For example:
>>>2 & 4
0
>>>if (2 & 4):
... print("true")
>>>bin(2 & 4)
'0b0'
>>>if ('0b0'):
... print("true")
true
>>>if (0b0):
... print("true")
>>>bool(0)
False
>>>bool(0b0)
False
>>>bool(b'0')
True
>>>bool(bin(0))
True
At first, with the misunderstanding that the if statements were evaluating if the argument == True, the above examples seemed quite illogical, as I thought a binary 1 should result in True and binary 0 should result in False. I had no idea why integer values above 1, in either int or binary form should return True.
After reading through the python docs for Truth Value Testing I see a few examples of objects that are considered false. Here, and rather obviously, it makes sense why bool(0) and bool(0b0) returns False. The opposite though, for bool(b'0') and bool(bin(0)), where this seems to return True. Why is this? Because bin() returns a string representation of the number, as is b'0' (a string) not an actual binary value (like 0b0), and since these strings are not empty strings, but rather filled with the characters representing zero, it evaluates to True (See third bullet point of Truth Value Testing).
I gave a few examples of non-obvious (to me) truth evaluation tests, and why they do actually make logical sense. Hope this helps others who may be mystified by the seemingly (to me) lesser common uses of the if statement.

Python's Evaluation of If-Else Conditional Statements

I recently encountered an example of an if-else conditional statement and could not understand the rationale behind its output. The following are the statements:
if 0:
1
else:
2
Output: 2
I tried different integers in 0's place, and received 1 each time. Is this because the zero in the if condition represents False? But then why do integers other than 1 still satisfy the if condition?
Thanks!
Edit: Thank you for all your answers. I now understand that any integer except 0 in the 'if' statement will make the statement True by default, resulting in an output of 1, in this case.
Python will always attempt to determine the "truthiness" of a given value used in a boolean context. In Python any numerical value of 0 (or 0.0) is considered false, and string, dictionary, list, or other iterable (or other class that can report its length) is false if it's empty or has length of 0. Also, None and boolean False are considered false.
Other values are considered true.
More details: https://docs.python.org/2.4/lib/truth.html.
In Python, bool is a subtype of int. False has the value 0, while other non-zero integers have the subtype bool with the value True.
To see this for yourself try this: False == 0
And to see the subtypes of int try this: int.__subclasses__()
1 is considered True while 0 is False,just like in binary.
Any non-zero numeric value is evaluated as True in a conditional statement.
bool  type is just a subtype of int in Python, with 1 == True and 0 == False.

Why does using an integer value as a 'while' loop condition work in Python?

The following code will output infinite lines of "test".
foo = 5
while foo:
print("bar")
The other day I came across an answer here about digit sums. This was the code shown in the answer:
def digit_sum(t):
s = 0
while t:
s += t % 10
t //= 10
return s
The part I'm focusing on is the "while t:" part. How and why does this work?
The while condition tests for truth. Any non-zero numeric value is considered true. See the Truth Value Testing section in the Python documentation:
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, 0L, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a
__nonzero__() 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.
Bold emphasis mine.
In your sample while loop, t trends to 0 (integer division by 10), so eventually while t: ends because t is considered false.
You already got useful answers, but I just wanted to answer your question in a way that can be easily understood by someone who is a beginner in Python.
You could rewrite your code as:
def digit_sum(t):
s = 0
while t!=0:
s += t % 10
t = t//10
return s
'while t' is equivalent to 'while t!=0', meaning that the loop will end when t is equal to 0.
In your for loop, 't //= 10' is equivalent to 't = t // 10' ('//' is a floor division operator and returns an integer). So the value of t becomes smaller each time the loop is executed, until it eventually reaches the value of 0. At this point, the 'while t' condition is False and the loop ends.

Python- if and elif statements not working right [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 months ago.
User_Input = str(input())
if "1" or "2" in User_Input:
print("didn't work [1]")
if "3" in User_Input:
print("didn't work [2]")
elif '4' not in User_Input:
print('didnt work [3]')
elif '5' in User_Input:
print('it worked')
when I make User_Input equal to '5' it doesn't say 'it worked' it instead says
'didnt work [1]' and 'didnt work [3]' which is impossible because 5 doesn't equal 1 or 2 so it shouldn't even get too the line, print('didnt work [3]') but somehow it does. Please help explain this!
You're mixing up how the statements are evaluated, specifically your or.
Here is how it's being evaluated by the interpreter:
# Note: This is evaluated as two separate boolean checks
# as denoted by the parens, rather than one as you are
# intending.
if ("1") or ("2" in User_Input): # "1" is a "truthy" value, e.g. not an empty string, so this is True, and ("2" in User_Input) is not evaluated due to "short circuiting". Meaning no matter what input you get, this if is always True.
Instead what you are trying to see is if the User_Input string is in any of your values:
if User_Input in ["1", "2"]:
# Only happens when 1 or 2 is the user input
What is happening is called short circuiting. Essentially that means that if the result of a boolean operation can be satisfied by the first evaluation, the rest can be skipped.
if True or False: # Since the first is True, the False is never evaluated
if False and True: # Since this is an "and", and the first condition already failed, the True is never evaluated
Now extrapolate that to an expensive function call (not just built ins). It likely saves a lot of time in a larger function. To demonstrate:
import time
def my_long_func():
"""Waits 5 seconds and returns False"""
time.sleep(5)
return False
def my_true_func():
"""Immediately returns True"""
return True
if my_true_func() or my_long_func():
print("This happens immediately, because of short circuiting.")
If you run ^ in your interpreter, you will see that the print happens immediately and your program never waits on the 5s sleep because it's unnecessary (that function is never called). Essentially it's an easy programming optimization that every language I can think of does automatically for you :)

Categories

Resources