Header Ads Widget

Converter codes in Python

 1. Convert Kilometer to Miles


# Program to convert Kilometers to Miles
km = int(input("Enter the value in kilometers: "))

# 1 Kilometer = 0.621371 Mile
ratio = 0.621371

# Converting km to mi.
mi = km * ratio

print("The entered value in Miles: ", mi)

Output :-


example....

Enter value in kilometers : 75
The entered value in Miles: 46.602825


2. Unit Converter


num1 = input('Enter the value: ')
unit1 = input('Which unit do you want it converted from:  ')
unit2 = input('Which unit do you want it converted to: ')

if unit1 == "cm" and unit2 == "m":
    ans = float(num1)/100
    print(ans)
elif unit1 == "mm" and unit2 == "cm":
    ans = float(num1)/10
    print(ans)
elif unit1 == "m" and unit2 == "cm":
    ans = float(num1)*100
    print(ans)
elif unit1 == "cm" and unit2 == "mm":
    ans = float(num1)*10
    print(ans)
elif unit1 == "mm" and unit2 == "m":
    ans = float(num1)/1000
    print(ans)
elif unit1 == "m" and unit2 == "mm":
    ans = float(num1)*1000
    print(ans)
elif unit1 == "km" and unit2 == "m":
    ans = float(num1)*1000
    print(ans)
elif unit1 == "m" and unit2 == "km":
    ans = float(num1)/1000
    print(ans)
elif unit1 == "mm" and unit2 == "km":
    ans = float(num1)/1000000
    print(ans)
elif unit1 == "ft" and unit2 == "cm":
    ans = float(num1)*30.48
    print(ans)
elif unit1 == "ft" and unit2 == "mm":
    ans = float(num1)*304.8
    print(ans)
elif unit1 == "ft" and unit2 == "inch":
    ans = float(num1)*12
    print(ans)
elif unit1 == "inch" and unit2 == "cm":
    ans = float(num1)*2.54
elif unit1 == "inch" and unit2 == "mm":
    ans = float(num1)*25.4


Output :-



Enter the value: 30
Which unit do you want it converted from: mm
Which unit do you want it converted to: km
3e-05

Enter the value: 20
Which unit do you want it converted from: ft
Which unit do you want it converted to: inch
240.0


3. Convert Binary, Octal, Decimal......


dec = int(input("Enter the value:"))

print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")

Output :-


Enter the value: 125
The decimal value of 125 is:
0b1111101 in binary.
0o175 in octal.
0*7d in hexadecimal.


4. Convert Text to Speech


import pyttsx3
engine =pyttsx3.init()
engine.say("Hello! Anonymous")
engine.runAndWait()


Note :- This will show result as speech. It will transfer your text into speech.



5. Generate Beep sound 


import winsound
f = 3599   #Frequency
d = 5000   #Duration
winsound.Beep(f,d)


Note :-    This will throw some beep sound, when you run this code. It can be changed by Frequency and Duration..


4. Convert Temperature


# Python Program to convert temperature

celsius = int(input("Enter the Temperature in Celsius:"))

# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print("in Fahrenheit:",fahrenheit)

#calculate celsius
fahrenheit = int(input("Enter the Temperature in Fahrenheit:"))
celsius = ((fahrenheit - 32) / 1.8)
print("in Celsius:",celsius)

Output :-


Enter the Temperature in Celsius: 96
in Fahrenheit: 204.8
Enter the Temperature in Fahrenheit: 21
in Celsius: -6.1111111

Post a Comment

0 Comments