Data Visualization : Lab Programs

1) A) Write a python program to find the best of two test average marks out of three test’s marks accepted from the user.

Source Code

mark1 = int(input("Enter the marks in the first test:")) 
mark2 = int(input("Enter the marks in second test: ")) 
mark3 = int(input("Enter the marks in third test: "))

if (mark1 > mark2):
	if (mark2 > mark3): 
		total_marks = mark1 + mark2
	else:
		total_marks = mark1 + mark3 
elif (mark1 > mark3):
	total_marks = mark1 + mark2 
else:
	total_marks = mark2 + mark3

Average_marks = total_marks / 2
print("The average of the best two test marks is:", Average_marks)

Output:

Enter the marks in the first test:18 
Enter the marks in second test: 19 
Enter the marks in third test: 24
The average of the best two test marks is: 21.5

1B) Develop a Python program to check whether a given number is palindrome or not and also count the number of occurrences of each digit in the input number.

Source Code

num = int(input("Enter a number: ")) 
temp1 = num
reverse = 0 
while temp1 > 0:
    remainder = temp1 % 10
    reverse = (reverse * 10) + remainder 
    temp1 = temp1 // 10
if num == reverse:
    print('The Entered Number %d is Palindrome'%(num)) 
else:
    print("The Entered Number %d is Not a Palindrome"%(num)) 
print("Digit\t Frequency")
for i in range(0,10): 
    count=0 
    temp2=num
    while temp2>0:
       digit=temp2%10 
       if digit==i:
          count=count+1 
       temp2=temp2//10
if count>0:
    print(i,"\t",count)

Output:

Enter a number: 3663
The Entered Number 3663 is Palindrome Digit	Frequency
3	2
6	2

2) A) Defined as a function F as Fn = Fn-1 + Fn-2. Write a Python program which accepts a value for N (where N >0) as input and pass this value to the function. Display suitable error message if the condition for input value is not followed.

Source Code

# Function for nth Fibonacci number 
def Fibonacci(n):
    if n == 1: 
        return 0
    elif n==2: 
        return 1
    else:
        return Fibonacci(n-1) + Fibonacci(n-2)

count = 1
N = int(input("Enter a value of N : ")) 
if N<=0:
    print("Invalid Input: Enter a value of N (>0)") 
else:
    print("Fibonacci sequence : ") 
    while count <= N:
        print(Fibonacci(count),end=' ') 
        count = count + 1

Output

Enter a value of N : 5
Fibonacci sequence : 
0 1 1 2 3 

2B) Develop a python program to convert binary to decimal, octal to hexadecimal using functions.

Source Code

def binary_to_decimal(binary): 
    decimal = 0
    power = 0
    while binary != 0: 
        last_digit = binary % 10
        decimal += last_digit * (2 ** power) 
        binary //= 10
        power += 1 
    return decimal

def octal_to_hexadecimal(octal): 
    decimal = 0
    power = 0 
    while octal > 0:
        digit = octal % 10
        decimal += digit * (8 ** power) 
        octal //= 10
        power += 1
    conversion_table = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
    hexadecimal = "" 
    while (decimal > 0):
        remainder = decimal % 16 
        if remainder >= 10:
            hexadecimal = conversion_table[remainder] + hexadecimal 
        else:
            hexadecimal = str(remainder) + hexadecimal 
        decimal = decimal // 16
    return hexadecimal

binary = int(input("Enter a binary number: ")) 
decimal = binary_to_decimal(binary) 
print("Decimal equivalent:", decimal)
octal = int(input("Enter an octal number: ")) 
hexadecimal = octal_to_hexadecimal(octal) 
print("Hexadecimal equivalent:", hexadecimal)

Output

Enter a binary number: 100
Decimal equivalent: 4
Enter an octal number: 7302
Hexadecimal equivalent: EC2

3A) Write a Python program that accepts a sentence and find the number of words, digits, uppercase letters and lowercase letters.

Source Code

sentence = input("Enter a sentence: ") 
(words, digits, upper, lower) = (0, 0, 0, 0) 
l_w = sentence.split()
words = len(l_w) 
for ch in sentence: 
    if ch.isdigit():
        digits = digits + 1 
    elif ch.isupper():
        upper = upper + 1 
    elif ch.islower():
        lower = lower + 1
print ("No of Words: ", words) 
print ("No of Digits: ", digits)
print ("No of Uppercase letters: ", upper) 
print ("No of Lowercase letters: ", lower)

Output

Enter a sentence: TOCXTEN Mysuru 573118
No of Words:  3
No of Digits:  6
No of Uppercase letters:  8
No of Lowercase letters:  5

3B)

Source Code

Output

4A)

Source Code

Output

4 B)

Source Code

Output

5 A)

Source Code

Output

5B)

Source Code

Output

6A)

Source Code

Output

6 B)

Source Code

Output

8 )

Source Code

Output

9 )

Source Code

Output

10 )

Source Code

Output

7 )

Source Code

Output

7 )