Lesson 25 · Python Fundamentals
Python Fundamentals: Build a Temperature Converter Step-by-Step
Welcome! Today you will learn how to make your own temperature converter in Python. Converting temperatures is useful in science, cooking, and travel. We…
- CoursePython Fundamentals
- Lesson25 of 22
- Video15 min
- FormatJupyter notebook · 25 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Python Temperature Converter: A Beginner's Journey#
Welcome! Today you will learn how to make your own temperature converter in Python.
Converting temperatures is useful in science, cooking, and travel. We will start from the very basics and build up step by step.
By the end, you will have a mini-project you can use and share!
# Let us print a welcome message to start.
print('Welcome to the Python Temperature Converter!')
What is a variable?#
Variables are like boxes that store information. Let us use them to store a temperature value.
# Here we create a variable called celsius and assign it a value.
celsius = 20
# Let us print the value of celsius.
print('Celsius temperature:', celsius)
# Let us convert Celsius to Fahrenheit using a formula.
fahrenheit = celsius * 9 / 5 + 32
print('Fahrenheit temperature:', fahrenheit)
User Input: Making it Interactive#
Instead of using a fixed value, we can ask the user for input.
The input() function lets the user type a number.
# Ask the user to type a Celsius value.
celsius = input('Enter a temperature in Celsius: ')
# Convert the input from a string to a number.
celsius = float(celsius)
fahrenheit = celsius * 9 / 5 + 32
print('That is', fahrenheit, 'degrees Fahrenheit.')
# What if the user enters something that is not a number?
# Let us handle it with a try-except block.
try:
celsius = float(input('Type a temperature in Celsius: '))
fahrenheit = celsius * 9 / 5 + 32
print('That is', fahrenheit, 'degrees Fahrenheit.')
except ValueError:
print('That was not a number. Please try again.')
Making a Function#
We can wrap the conversion code in a function. This helps us reuse it and keeps our code tidy.
A function is a named section of code that does one thing.
# Define a temperature conversion function.
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
# Try it out:
temp = 100
print(temp, 'Celsius is', celsius_to_fahrenheit(temp), 'Fahrenheit.')
# Can we also convert Fahrenheit to Celsius? Let us make a new function.
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
# Let us try converting ninety-eight point six Fahrenheit to Celsius.
print('98.6 Fahrenheit is', fahrenheit_to_celsius(98.6), 'Celsius.')
Making Choices: if Statements#
We can let users choose which conversion they want. An if statement lets us make decisions in code.
# Ask the user what type of conversion they want.
choice = input('Type C to convert Celsius to Fahrenheit, or F for Fahrenheit to Celsius: ')
if choice == 'C':
c = float(input('Enter Celsius: '))
print(c, 'Celsius is', celsius_to_fahrenheit(c), 'Fahrenheit.')
elif choice == 'F':
f = float(input('Enter Fahrenheit: '))
print(f, 'Fahrenheit is', fahrenheit_to_celsius(f), 'Celsius.')
else:
print('Sorry, that is not a valid choice!')
# What if we want to convert a lot of temperatures?
# Lists help us store many values.
temps_c = [0, 10, 20, 30, 40]
for t in temps_c:
print(t, 'C =', celsius_to_fahrenheit(t), 'F')
# We can also store each result in a new list using a loop.
converted = []
for c in temps_c:
converted.append(celsius_to_fahrenheit(c))
print('All Fahrenheit values:', converted)
# Let us use a list comprehension for a shorter way.
temps_f = [celsius_to_fahrenheit(c) for c in temps_c]
print('With list comprehension:', temps_f)
# What if we want only temperatures above freezing?
above_freezing = [c for c in temps_c if c > 0]
print('Celsius values above zero:', above_freezing)
Mini-Project: Full Temperature Converter Program#
Let us pull all the pieces together.
You will make a program that:
- Greets the user
- Asks for their name
- Lets them pick a conversion
- Handles errors
- Shows results and says goodbye
# Welcome the user and ask their name.
print('Welcome to the Full Temperature Converter!')
name = input('What is your name? ')
print('Hi,', name + '!')
# Ask which conversion and handle case differences.
convert_choice = input('Type C to convert Celsius to Fahrenheit, or F for Fahrenheit to Celsius: ').upper()
try:
if convert_choice == 'C':
c = float(input('Enter Celsius value: '))
f = celsius_to_fahrenheit(c)
print(c, 'Celsius is', f, 'Fahrenheit.')
elif convert_choice == 'F':
f = float(input('Enter Fahrenheit value: '))
c = fahrenheit_to_celsius(f)
print(f, 'Fahrenheit is', c, 'Celsius.')
else:
print('Not a valid choice! Please pick C or F.')
except ValueError:
print('That was not a number. Please try again.')
# Say goodbye at the end.
print('Thank you for using the converter! Have a great day!')
Best Practices and Tips#
- Write clear variable names.
- Handle unexpected user input.
- Use functions to keep your code tidy.
- Test your program with different values.
# Common mistake: forgetting to convert input() to float.
temp = input('Enter a Celsius temperature: ')
# print(celsius_to_fahrenheit(temp)) # This would cause an error.
print('Be sure to turn input into a number before converting!')
# Bonus: Convert a whole list typed by the user.
numbers = input('Enter Celsius values separated by commas: ')
c_list = [float(n) for n in numbers.split(',')]
f_list = [celsius_to_fahrenheit(c) for c in c_list]
print('Converted:', f_list)
Your Turn: Final Practice#
- Try changing the messages and prompts.
- Extend your converter to support Kelvin or Rankine.
- Challenge: Add an option to repeat the program until the user says 'no.'
Recap#
You learned about:
- Variables
- User input
- Functions
- Decisions with if statements
- Lists and list comprehensions
- Error handling
- Mini-project building
Keep practicing and you will get better over time!
Thanks for joining!#
Keep exploring Python. Check the video description for extras, and let us know your ideas for future tutorials. Happy coding!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



