Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python program to convert meters in yards and vice versa
Next article icon

Python Program to Convert Temperatures using Classes

Last Updated : 20 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Temperature conversion is a common task in various fields, and Python provides a versatile platform for creating programs to handle such conversions. In this article, we will explore how to create a temperature converter using classes in Python for four different conversions: Celsius to Fahrenheit, Fahrenheit to Celsius, Kelvin to Celsius, and Celsius to Kelvin.

Python Program To Convert Temperatures Using Classes

below, are the Python programs to Convert Temperatures Using Classes in Python.

  • Celsius to Fahrenheit Converter
  • Fahrenheit to Celsius Converter
  • Kelvin to Celsius Converter
  • Celsius to Kelvin Converter

Python Program To Convert Celsius to Fahrenheit

In this example, This code defines a `CelsiusToFahrenheitConverter` class with a `convert` method for converting Celsius to Fahrenheit. An instance of the class is created, and the `convert` method is used to convert 25 Celsius to Fahrenheit, with the result printed as output. The class encapsulates the conversion functionality.

Python
class CelsiusToFahrenheitConverter:     def convert(self, celsius):         return (celsius * 9/5) + 32  # Example usage celsius_value = 25 converter = CelsiusToFahrenheitConverter() fahrenheit_value = converter.convert(celsius_value) print(f"{celsius_value} Celsius is equal to {fahrenheit_value} Fahrenheit") 

Output

25 Celsius is equal to 77.0 Fahrenheit

Python Program To Convert Fahrenheit to Celsius

In this article, code defines a `FahrenheitToCelsiusConverter` class with a `convert` method for converting Fahrenheit to Celsius. An instance of the class is created, and the `convert` method is used to convert 77 Fahrenheit to Celsius, with the result printed as output. The class encapsulates the conversion logic.

Python
class FahrenheitToCelsiusConverter:     def convert(self, fahrenheit):         return (fahrenheit - 32) * 5/9  # Example usage fahrenheit_value = 77 converter = FahrenheitToCelsiusConverter() celsius_value = converter.convert(fahrenheit_value) print(f"{fahrenheit_value} Fahrenheit is equal to {celsius_value} Celsius") 

Output

77 Fahrenheit is equal to 25.0 Celsius

Python Program To Convert Kelvin to Celsius

In this example, code defines a `KelvinToCelsiusConverter` class with a `convert` method for converting Kelvin to Celsius. An instance of the class is created, and the `convert` method is used to convert 300 Kelvin to Celsius, with the result printed as output. The class structure enhances code organization.

Python
class KelvinToCelsiusConverter:     def convert(self, kelvin):         return kelvin - 273.15  # Example usage kelvin_value = 300 converter = KelvinToCelsiusConverter() celsius_value = converter.convert(kelvin_value) print(f"{kelvin_value} Kelvin is equal to {celsius_value} Celsius") 

Output

300 Kelvin is equal to 26.850000000000023 Celsius

Python Program To Convert Celsius to Kelvin

In this example, code defines a `CelsiusToKelvinConverter` class with a `convert` method for converting Celsius to Kelvin. An instance of the class is created, and the `convert` method is used to convert 25 Celsius to Kelvin, with the result printed as output. The class structure encapsulates the conversion functionality.

Python
class CelsiusToKelvinConverter:     def convert(self, celsius):         return celsius + 273.15  # Example usage celsius_value = 25 converter = CelsiusToKelvinConverter() kelvin_value = converter.convert(celsius_value) print(f"{celsius_value} Celsius is equal to {kelvin_value} Kelvin") 

Output

25 Celsius is equal to 298.15 Kelvin

Conclusion

In conclusion, the Python program utilizing classes for temperature conversions provides a modular and organized approach to handling various temperature scales. Each class encapsulates a specific conversion logic, enhancing code readability and reusability. This object-oriented design allows for easy integration into larger projects and facilitates the maintenance of temperature conversion functionality. Overall, using classes in Python for temperature conversions promotes a structured and scalable solution.


Next Article
Python program to convert meters in yards and vice versa

K

khanaayuxyc
Improve
Article Tags :
  • Python
  • Python Programs
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Python Program to Get the Class Name of an Instance
    In this article, we will see How To Get a Class Name of a class instance. For getting the class name of an instance, we have the following 4 methods that are listed below: Using the combination of the __class__ and __name__ to get the type or class of the Object/Instance.Use the type() function and
    4 min read
  • Python program to convert meters in yards and vice versa
    Given the distance in either meter or yard, the task here is to generate a Python program that converts distance given in meters to yards and vice versa. Examples: Input: Length in Meter: 245 Length in Yard : 100 Output: 245 Meter in Yard = 267.9344 100 Yards in Meter = 91.4403 Input: Length in Mete
    2 min read
  • Python Program to Calculate Gross Salary of a Person
    The task of calculating the Gross Salary of a Person in Python involves taking the basic salary and grade as input, applying the necessary calculations for salary components and displaying the final salary. The gross salary formula: Gross Salary = Basic Salary + HRA + DA + Allowance - PF Where: HRA
    3 min read
  • Python program to convert int to exponential
    Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t
    1 min read
  • Convert Celsius To Fahrenheit - Python
    We are given the temperature in Celsius and our task is to convert the temperature value in the Fahrenheit scale and display it. To convert the temperature from Celsius to Fahrenheit or vice versa in Python firstly we will take the input in Celsius or Fahrenheit and then convert that temperature to
    2 min read
  • Python program to convert float to exponential
    Given a float number, the task is to write a Python program to convert float to exponential. Examples: Input: 19.0 Output: 1.900000e+01 Input: 200.2 Output: 2.002000e+02 Input: 1101.02 Output: 1.101020e+03Approach: We will first declare and initialise a float number.Then we will use format method to
    1 min read
  • Python Tutorial | Learn Python Programming Language
    Python Tutorial – Python is one of the most popular programming languages. It’s simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
    10 min read
  • Python program to convert exponential to float
    Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101
    1 min read
  • Create Class Objects Using Loops in Python
    We are given a task to create Class Objects using for loops in Python and return the result, In this article we will see how to create class Objects by using for loops in Python. Example: Input: person_attributes = [("Alice", 25), ("Bob", 30), ("Charlie", 35)]Output: Name: Alice, Age: 25, Name: Bob,
    4 min read
  • How to Scrape Web Data from Google using Python?
    Prerequisites: Python Requests, Implementing Web Scraping in Python with BeautifulSoup Web scraping is a technique to fetch data from websites. While surfing on the web, many websites don’t allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences