Can we make a simple true/false logical expression with Python?
It came out as a assignment, but it doesn't matter what language you use, so I'm going to code it with Python that I've used a few times.
I already know things like "and or"
I also need things like exist and all. Does Python have this function?
python circuit
I'm just going to give you an example
>>> 3 and 5
5
>>> 0 and 5
0
>>> False and 5
False
>>> False or 5
5
>>> 0 or 5
5
>>>
>>> def xor(a,b):
... ... return bool(a)!=bool(b)
...
>>> xor(True, True)
False
>>> xor(True, False)
True
>>>
any() returns True as soon as the True factor appears.
all() returns True only when all the factors are True
>>> def func(num):
... func.counter += 1 #Increases count by one each time called
... ... print("cnt:", func.counter)
... ... return num
...
>>> func.counter=0 #Initialization
>>>
>>> any(func(i) for i in [1,2,3,4])
('cnt:', 1)
True
>>>
>>> func.counter=0 #Initialization
>>> all(func(i) for i in [1,2,3,4])
('cnt:', 1)
('cnt:', 2)
('cnt:', 3)
('cnt:', 4)
True
>>>
© 2024 OneMinuteCode. All rights reserved.