0% found this document useful (0 votes)
8 views2 pages

Develop A Python Program To Convert Binary To Decimal, Octal To Hexadecimal Using Functions

The document contains Python programs for various tasks: converting binary to decimal and octal to hexadecimal, analyzing a sentence for word and character counts, and calculating string similarity between two inputs. Each program utilizes functions to perform specific operations and outputs the results. The code snippets demonstrate practical applications of Python for data conversion and text analysis.

Uploaded by

Nidhi Rao
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)
8 views2 pages

Develop A Python Program To Convert Binary To Decimal, Octal To Hexadecimal Using Functions

The document contains Python programs for various tasks: converting binary to decimal and octal to hexadecimal, analyzing a sentence for word and character counts, and calculating string similarity between two inputs. Each program utilizes functions to perform specific operations and outputs the results. The code snippets demonstrate practical applications of Python for data conversion and text analysis.

Uploaded by

Nidhi Rao
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
You are on page 1/ 2

2.b.

Develop a python program to convert binary to decimal, octal to hexadecimal using


functions.

def binary_to_decimal(binary):
decimal = 0
power = len(binary)-1
for bit in binary:
if bit == '1':
decimal += 2**power
power -= 1
return decimal

def octal_to_hexadecimal(octal):
decimal = 0
power = len(octal)-1
for digit in octal:
decimal += int(digit) * 8**power
power -= 1
hexadecimal = hex(decimal)[2:]
return hexadecimal

binary_number = "101010"
decimal_number = binary_to_decimal(binary_number)
print(f"{binary_number} in decimal is {decimal_number}")

octal_number = "245"
hexadecimal_number = octal_to_hexadecimal(octal_number)
print(f"{octal_number} in hexadecimal is
{hexadecimal_number}")
3.a.Write a Python program that accepts a sentence and find the number of words,
digits, uppercase letters and lowercase letters.

def analyze_sentence(sentence):
word_count = len(sentence.split())
digit_count = sum(c.isdigit() for c in sentence)
uppercase_count = sum(c.isupper() for c in sentence)
lowercase_count = sum(c.islower() for c in sentence)

print(f"Number of words: {word_count}")


print(f"Number of digits: {digit_count}")
print(f"Number of uppercase letters: {uppercase_count}")
print(f"Number of lowercase letters: {lowercase_count}")

sentence = input("Enter a sentence: ")


analyze_sentence(sentence)

3.b Write a Python program to find the string similarity between two given strings

from difflib import SequenceMatcher

str1 = input("Enter the first string: ")


str2 = input("Enter the second string: ")

matcher = SequenceMatcher(None, str1, str2)


similarity = matcher.ratio()

print(f"String 1: {str1}")
print(f"String 2: {str2}")
print(f"Similarity between two said strings: {similarity}")

You might also like