Python Difference Between “is” & “==”


Reference : https://realpython.com/products/python-tricks-book/?utm_source=drip&utm_medium=email&utm_campaign=pytricks-is-vs

# "is" vs "=="

>>> a = [1, 2, 3]
>>> b = a

>>> a is b
True
>>> a == b
True

>>> c = list(a)

>>> a == c
True
>>> a is c
False

# • "is" expressions evaluate to True if two 
#   variables point to the same object

# • "==" evaluates to True if the objects 
#   referred to by the variables are equal

args & kwargs – python


Reference : https://realpython.com/products/python-tricks-book/?utm_source=drip&utm_medium=email&utm_campaign=Python+Tricks&utm_content=%5B%F0%9F%90%8DPyTricks%5D%3A+Function+argument+unpacking+in+Python

Difference of args & **kwargs,

def myfunc(x, y, z):
    print(x, y, z)

args = (1, 0, 1)
kwargs = {'x': 1, 'y': 0, 'z': 1}

>>> myfunc(*args)
1, 0, 1

>>> myfunc(**kwargs)
1, 0, 1

Decorators – Python

Decorators – One function takes argument as an another one function,

def test_dec(func):
    def test1(ip):
        ts = sum(ip)
        for x in range(len(ip)):
            yield ts-ip[x]
    return test1

@test_dec
def test2(ip):
    return ip
a = list(test2([1,2,3,4,5]))
print (a)
print (min(a)) #10
print (a)
print (max(a)) #14

Yield in Python

Yield – Instead of return can use this, It returns as a generator object, Mainly designed for memory consumption

ip = [1,2,3,4,5]
def test(ip):
    ts = sum(ip)
    for x in range(len(ip)):
        yield ts-ip[x]
print (min(test(ip))) #10
print (max(test(ip))) #14

Find Second Largest Number

print (' ************* To Find Second Largest Number *************** ')

num_count = input('Enter Numbers Count : ')

if (num_count > 1):
	range_val = range(1,int(num_count)+1)

	numlist = []
	for n in range_val:
		n_temp = str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))
		num = input('Enter '+n_temp+' integer : ')
		numlist.append(num)

	numlist = list(set(numlist))
	numlist.sort(reverse=True)
	print ('Second Largest Number : '+str(numlist[1]))
else:
	print ('You Should Enter More Than Two Integers')

OUTPUT ************* To Find Second Largest Number *************** Enter Numbers Count : 5 Enter 1st integer : 1 Enter 2nd integer : 2 Enter 3rd integer : 3 Enter 4th integer : 4 Enter 5th integer : 4 Second Largest Number : 3

Python – Hamming Distance

Hamming Distance – Returns count of difference characters between same length of two strings


# TO FIND HAMMING DISTANCE

def hamming_distance(a1, a2):
	if len(a1) == len(a2):
		diff = [a1_c for a1_c,a2_c in zip(a1,a2) if a1_c != a2_c]
		print (diff)
		distance_res = len(diff)
	else:
		distance_res = False
	return distance_res

a1 = str(input('Enter 1st String : '))
a2 = str(input('Enter 2nd String : '))

print hamming_distance(a1, a2)

PYTHON – HANDLE TWO LISTS IN SINGLE LOOP

N = int(input())

# Get the array
numArray1 = list(map(int, input().split()))
numArray2 = list(map(int, input().split()))

sumArray = []

def addval(a,b):
return a+b

# Write the logic here:
sumArray = [sum(x) for x in zip(numArray1, numArray2)]

# Print the sumArray
for element in sumArray:
print(element, end=” “)

print(“”)


OUTPUT:

Input:
N = 3
numArrayA = 3 9 8
numArrayB = 8 12 74
Output:
11 21 82