Can we make a simple true/false logical expression with Python?

Asked 2 years ago, Updated 2 years ago, 41 views

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

2022-09-21 19:23

1 Answers

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
>>> 


2022-09-21 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.