0% found this document useful (0 votes)
16 views8 pages

Python Programs

The document contains Python programs that demonstrate various set operations, relation properties, function operations, and combinatorial calculations. It includes examples for union, intersection, subset checks, reflexivity, symmetry, transitivity, function validation, domain and range identification, factorial computation, permutations, combinations, propositional logic evaluations, and truth tables. Each section provides code snippets and explanations for the respective concepts.

Uploaded by

Peli D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Python Programs

The document contains Python programs that demonstrate various set operations, relation properties, function operations, and combinatorial calculations. It includes examples for union, intersection, subset checks, reflexivity, symmetry, transitivity, function validation, domain and range identification, factorial computation, permutations, combinations, propositional logic evaluations, and truth tables. Each section provides code snippets and explanations for the respective concepts.

Uploaded by

Peli D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMS

1. Union, Intersection, Difference


A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print("A ∪ B =", [Link](B))


print("A ∩ B =", [Link](B))
print("A - B =", [Link](B))
print("B - A =", [Link](A))
print("A ^ B =", A.symmetric_difference(B))

2. Check Subset & Superset


A = {1, 2}
B = {1, 2, 3}

print("A is subset of B:", [Link](B))


print("B is superset of A:", [Link](A))

3. Check if Relation is Reflexive


A = {1, 2, 3}
R = {(1,1),(2,2),(3,3),(1,2)}
is_reflexive = all((a, a) in R for a in A)
print("Relation is Reflexive:", is_reflexive)

4. Check if Relation is Symmetric


R = {(1,2),(2,1),(2,3),(3,2)}

is_symmetric = all((b, a) in R for (a, b) in R)


print("Relation is Symmetric:", is_symmetric)

5. Check if Relation is Transitive


R = {(1,2),(2,3),(1,3)}

is_transitive = True
for (a,b) in R:
for (c,d) in R:
if b == c and (a,d) not in R:
is_transitive = False

print("Relation is Transitive:", is_transitive)

6. Check if a Relation is a Function. A relation is a function if no


two ordered pairs have the same first element.
relation = {(1, 10), (2, 20), (3, 30), (1, 40)}
domain = [x for (x, y) in relation]
is_function = len(domain) == len(set(domain))

print("Is it a function?:", is_function)

7. Identify Domain and Range of a Function


f = {(1, 2), (2, 4), (3, 9), (4, 16)}

domain = {x for (x, y) in f}


range_set = {y for (x, y) in f}

print("Domain:", domain)
print("Range:", range_set)

8. Composition of Two Functions


Example:
𝒇(𝒙) = 𝒙𝟐
𝒈(𝒙) = 𝒙 + 𝟏
Then:
(𝒇 ∘ 𝒈)(𝒙) = 𝒇(𝒈(𝒙))

def f(x):
return x * x
def g(x):
return x + 1

def fog(x):
return f(g(x))

def gof(x):
return g(f(x))

print("f(g(x)) =", fog(-3))


print("g(f(x)) =", gof(10))

9. Program to compute factorial


n = int(input("Enter a number: "))
fact = 1

for i in range(1, n + 1):


fact *= i

print("Factorial of", n, "is", fact)


10. Program to calculate permutation P(n, r)
Formula:
𝒏!
𝑷(𝒏, 𝒓) =
(𝒏 − 𝒓)!

import math

n = int(input("Enter value of n: "))


r = int(input("Enter value of r: "))

if r > n:
print("Permutation not possible (r should be ≤ n).")
else:
nPr = [Link](n) // [Link](n - r)
print(nPr)

11. Program to calculate combination C(n, r)


Formula:
𝒏!
𝑪(𝒏, 𝒓) =
𝒓! (𝒏 − 𝒓)!

import math

n = int(input("Enter value of n: "))


r = int(input("Enter value of r: "))
if r > n:
print("Combination not possible (r must be ≤ n).")
else:
nCr = [Link](n) // ([Link](r) * [Link](n - r))
print(nCr)

13. Evaluate a propositional expression


Find the truth value of the given propositions if P is T, Q is F and R
is T: (𝑷 ∨ 𝑸) ∧ ¬𝑹

P = True
Q = False
R = True

value = (P or Q) and (not R)


print("Truth value of the given proposition is:", value)

14. Truth Table for AND


print("P \t\t Q \t\t P AND Q")

for P in [True, False]:


for Q in [True, False]:
print(P, "\t", Q, "\t", P and Q)
15. Check if a propositional expression is a tautology, contradiction,
or contingency. Check for the Compound proposition (𝑷 ∧ 𝑸) → 𝑷.
Note: In the command we write: not (P and Q) or P
expr = input("Enter logical expression: ")

is_tautology = True
is_contradiction = True

for P in [True, False]:


for Q in [True, False]:
value = eval(expr)
if value == False:
is_tautology = False
if value == True:
is_contradiction = False

if is_tautology:
print("The expression is a TAUTOLOGY.")
elif is_contradiction:
print("The expression is a CONTRADICTION.")
else:
print("The expression is a CONTINGENCY.")

You might also like