Python Basic Exercise for Beginners


Solution

def multi_sum(n1, n2): pro = n1 *n2 if(pro < 500): return pro else: return n1 +n2 number1 = int(input("Enter first number ")) number2 = int(input("Enter second ")) result = multi_sum(number1, number2) print(result)

Output


>>>
Enter first number 20
Enter second 40

Result: 60


def fun(x):
    prevNum=0
    for i in range(x):
        tot = prevNum + i
	    print(tot)
        prevNum = i

print("cummulative addition in a given range")
fun(10)

>>>
cummulative addition in a given range
0
1
3
5
7
9
11
13
15
17



        Str1 = input("Enter String ")
        print("Original String is ", Str1)

        print("chars at even pos :")
        for i in range(0, len(Str1)-1, 2):
            print("index[",i,"]", Str1[i] )
        
    
>>>
Enter String dzonejpr
Orginal String is dzonejpr
chars at even pos :
index[ 0 ] d
index[ 2 ] o
index[ 4 ] e
index[ 6 ] p


def checkForSame(myList):
    p = myList[0]
    q = myList[-1]
    if(p == q):
        return True
    else:
        return False

test = [20, 30, 40, 50, 20]

print("result :", checkForSame(test))

>>>
result : True


def DivByThree(TempList):
    for n in TempList:
        if (n % 3 == 0):
            print(n)

Lst = [15, 25, 33, 70, 88]
print("Finding divisible of 3 in a list")
DivByThree(Lst)


>>>
Finding divisible of 3
15
33


def count_Dzone(statement):
    count = 0
    for i in range(len(statement)-1):
        count += statement[i:i+5] == 'Dzone'
    return count

count = count_Dzone(""Dzone is an IT Company.Dzone provide Python and Machine Learning Course in Jaipur"")
print("Dzone appeared ", count, "times")

>>>
Dzone appeared 2 times


def revCheck(number):
    originalNum = number
    reverseNum=0
    while(number > 0):
        reminder = number % 10
        reverseNum = (reverseNum *10) + reminder
        number = number // 10

    if(originalNum == reverseNum):
        return True
    else:
        return False

print("Is orig. and rev. number is same")
print(revCheck(141)

>>>
Is orig. and rev. number is same
True


def CustList(L1, L2):
	L3 = []
	for a in L1:
		if(a % 2 != 0):
			L3.append(a)
	for b in L2:
		if(b % 2 == 0):
			L3.append(b)
	return L3

print("Ans: \n")
L1 = [10, 20, 23, 11, 17]
L2 = [13, 43, 24, 36, 12]

print(CustList(L1, L2))


>>>
[23, 11, 17, 24, 36, 12]