Python Practice › OOP Programs › Program 34
ElectricCar with range
Create ElectricCar (inherits Car) with battery_size and range_per_charge and a method to calculate the range.
Output
Tata Nexon EV 300.0 km
Approach
- Range here is battery size (kWh) times km per kWh.
Solution
class Car: def __init__(self, make, model, year): self.make, self.model, self.year = make, model, year class ElectricCar(Car): def __init__(self, make, model, year, battery_size, range_per_charge): super().__init__(make, model, year) self.battery_size = battery_size # kWh self.range_per_charge = range_per_charge # km per kWh def total_range(self): return self.battery_size * self.range_per_charge ev = ElectricCar("Tata", "Nexon EV", 2025, 40, 7.5)print(ev.make, ev.model, ev.total_range(), "km")Output
Tata Nexon EV 300.0 km
Learn the concept
New to this topic? Read the lesson, then come back.