Python
How to Implementing Logic design in Python | Request video
1. What is Boolean in python
2. Gate implementation (AND, OR)
3. Half adder
4. Full adder
5. 4 Bit Binary Adder
6. program explanation
7. Structural level programming
#Program for AND gate
function for AND gate
def AND_fun (a, b):
if a == 1 and b == 1:
return 1
else:
return 0
def AND_fun (a, b):
out = a and b
return out
# calling AND function
AND_fun(1,0)
---------------------------------------
#Program for OR gate
def OR_fun (a, b):
out = a | b
return out
# calling OR function
OR_fun(0,0)
---------------------------------------------
# A half adder
def half_adder(a, b):
sum = a ^ b # ^ is the symbol for XOR operator
carry = a and b
return carry,sum
# A full adder
def full_adder(carry_in, a, b):
carry1,sum1 = half_adder(a,b) #calling half adder function
carry2,sum = half_adder(sum1,carry_in) #calling half adder function
carry = carry1 or carry2
print("sum= ",sum)
print("carry= " ,carry)
return carry,sum
# calling full adder function
full_adder(1,1,1)
-------------------------------------------
# function for getting input and if length mismatch then fill with zero
def get_input():
fill='0'
bits_a = input('input your first binary ')
bits_b = input('input your second binary ')
l1 = len(bits_a)
l2 = len(bits_b)
if l1 > l2:
bits_b = bits_b.rjust(l1, fill)
elif l2 > l1:
bits_a = bits_a.rjust(l2, fill)
return bits_a,bits_b
# function for perform XOR operation
def xor(bit_a, bit_b):
A1 = bit_a and (not bit_b)
A2 = (not bit_a) and bit_b
return int(A1 or A2)
# function for Half adder
def half_adder(bit_a, bit_b):
return (xor(bit_a, bit_b), bit_a and bit_b)
# function for full adder
def full_adder(bit_a, bit_b, carry=0):
sum1, carry1 = half_adder(int(bit_a), int(bit_b))
sum2, carry2 = half_adder(sum1, carry)
# print(sum2, carry1 or carry2)
return sum2, carry1 or carry2
# function for binarry adder
def binary_string_adder(bits_a, bits_b):
carry = 0
result = ''
s0,c0 = full_adder(bits_a[3],bits_b[3],carry)
s1,c1 = full_adder(bits_a[2],bits_b[2],c0)
s2,c2 = full_adder(bits_a[1],bits_b[1],c1)
s3,cout = full_adder(bits_a[0],bits_b[0],c2)
result = str(cout)+str(s3)+str(s2)+str(s1)+str(s0)
return result
# # alternative function for binarry adder
# def binary_string_adder(bits_a, bits_b):
# carry = 0
# result = ''
# for i in range(len(bits_a)-1 , -1, -1):
# summ, carry = full_adder(int(bits_a[i]), int(bits_b[i]), carry)
# result += str(summ)
# result += str(carry)
# return result[::-1]
#mail function
def main():
bits_a, bits_b = get_input()
print('first bit is :',bits_a,'(',int(bits_a,2),')')
print('Second bit is :',bits_b,'(',int(bits_b,2),')')
result = binary_string_adder(bits_a, bits_b)
print('Answer is :',result,'(',int(result,2),')')
calling the mail function
main()
-------------------------
Check out my website for more : https://sites.google.com/view/iammanuprasad
Blog : https://iammanuprasad.blogspot.com/
Other Python videos
Chapter 1 - Learn Python in Malayalam | For Ultimate beginner | Chapter 1 | Introduction | Google Colab |
https://youtu.be/WiyQ8wTsRO0
Chapter 2 - Learn Python in Malayalam | Chapter 2 | Identifier | Indentation | Multi-line Statement
https://youtu.be/YFngtW42Mls
Chapter 3 - Learn Python in Malayalam | Chapter 3 | Quotation | Comments | Assigning values
https://youtu.be/gxalG8Aps0A
Chapter 4 - Learn Python in Malayalam | Chapter 4 | Data Types | Numbers | Strings
https://youtu.be/AaEL_f2RHIk
Chapter 5 - Learn Python in Malayalam | Chapter 5 | Data Types | List | Tuple | Dictionary
https://youtu.be/irND35-6BZ0
Chapter 6 - Learn Python in Malayalam | Chapter 6 | decision making | if.. else | nested if
https://youtu.be/MRfg2QZBHvE
Chapter 7 - Learn Python in Malayalam|Chapter 7| Loops in python | while loop | for loop | nested loop
https://youtu.be/VYXndepcTCM
Chapter 8 - Learn Python in Malayalam | Chapter 8 | Functions in python
https://youtu.be/d7Y2D9H4u7c
Chapter 9 - Learn Python in Malayalam | Chapter 9 | Classes in python | OOPs concept | Object | constructors
https://youtu.be/hFOTKyYCkdw
Chapter 10 - Learn Python in Malayalam |Chapter 10|Matrices in python|addition & mul | transpose | inverse | rank
https://youtu.be/w-_x2vfpJk0
Chapter 11 - Learn Python in Malayalam|Chapter 11 | Plotting & Visualization in python | Subplot | Bar Plot
https://youtu.be/-7DB7OEenms
Chapter 12 - Learn Python in Malayalam|Chapter 12|File handling in Python | Read, write and plot from a file
https://youtu.be/aID_Dlx-oys
Chapter 13 - Learn Python in Malayalam | Chapter 13 | Solving linear equation using matrix method in Python
https://youtu.be/zk6Fcs-NY6I
Chapter 14 - Learn Python in Malayalam|Chapter 14 | How to find differentiation in python
https://youtu.be/7k5oATBKrd8
Chapter 15 - Learn Python in Malayalam | Chapter 15 |Solving Integration in Python|single, double,triple integral
https://youtu.be/BoEPyohAvtI
Chapter 16 - Learn Python in Malayalam|Chapter 16 | Probability in Python | Coin toss problem | absolute error
https://youtu.be/rF7Tw_EWRqw
Learn Python in Malayalam
Learn Python in Malayalam | Chapter 1 | Introduction | Google Colab | For Ultimate beginner
Learn Python in Malayalam | Chapter 2 | Identifier | Indentation | Multi-line Statement
Learn Python in Malayalam | Chapter 3 | Quotation | Comments | Assigning values
Learn Python in Malayalam | Chapter 4 | Data Types | Numbers | Strings
Learn Python in Malayalam | Chapter 5 | Data Types | List | Tuple | Dictionary
Learn Python in Malayalam | Chapter 6 | decision making | if.. else | nested if
Learn Python in Malayalam | Chapter 7 | Loops in python | while loop | for loop | nested loop
Learn Python in Malayalam | Chapter 9 | Classes in python | OOPs concept | Object | constructors
Learn Python in Malayalam|Chapter 11 | Plotting & Visualization in python | Subplot | Bar Plot
Learn Python in Malayalam|Chapter 12|File handling in Python | Read & write a file
Learn Python in Malayalam | Chapter 13 | Solving linear equation using matrix method in Python
Learn Python in Malayalam | Chapter 14 | How to solve differentiation in python
Learn Python in Malayalam|Chapter 16 | Probability in Python | Coin toss problem | absolute error