Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program reads two binary numbers (0 or 1) from the user and computes the results of the following logical operations:
XOR, OR, AND, NOR, NAND


Python Code:


def logical_operations(a: int, b: int) -> dict:
    return {
        "AND": a & b,
        "OR": a | b,
        "XOR": a ^ b,
        "NAND": int(not (a & b)),
        "NOR": int(not (a | b))
    }

# Read inputs from user
a = int(input("Enter first binary number (0 or 1): "))
b = int(input("Enter second binary number (0 or 1): "))

# Validate input
if a not in [0, 1] or b not in [0, 1]:
    print("Only 0 or 1 are allowed.")
else:
    results = logical_operations(a, b)
    for op, val in results.items():
        print(f"{op}: {val}")

Sample Output (input: a = 1, b = 0):


AND: 0  
OR: 1  
XOR: 1  
NAND: 1  
NOR: 0

Written & researched by Dr. Shahin Siami