Basic Data Types in Python With Examples
Basic Data Types in Python With Examples
Description: Create a calculator that converts fuel consumption to kilometers per liter (km/l) and miles per gallon (mpg).
Perfect for travelers and drivers who want to compare fuel efficiency across different measurement system.
# ================================================
# FUEL EFFICIENCY CALCULATOR
# User input
distance_km = float(input("Enter distance traveled (km): "))
fuel_liters = float(input("Enter fuel consumed (liters): "))
# Calculations
km_per_liter = distance_km / fuel_liters
miles_per_gallon = km_per_liter * 2.352 # 1 km/l j 2.352 mpg
# Display results
print("\n YOUR RESULTS:")
print(f"- Efficiency: {km_per_liter:.1f} km/l & {miles_per_gallon:.1f} mpg (miles per gallon)")
# ================================================
How It Works:
Data Collection: input() captures user values, and float() converts them to decimals. Example: "320 km" and "20 liters"
become 320.0 and 20.0.
Math Logic: Divide kilometers by liters to get km/l. Use the conversion factor 2.352 to calculate mpg.
Formatting: f-strings with :.1f round results to one decimal place for readability.
# Sample Output:
Enter distance traveled (km): 320
Enter fuel consumed (liters): 20
YOUR RESULTS:
- Efficiency: 16.0 km/l & 37.6 mpg (miles per gallon)
interactive-free-python-lab.uk
Interactive Free Python Lab
Discover the free Python course 3 learn to program through hands-on exercises and interactive examples. Start your coding journey
today!
to practice:
F-Strings, Operators, Conditional Statements, While Loop, For Loop, Control Statements, Functions, Built-in Functions,
Modules and Packages, Lists, Tuples, Dictionaries, Special Functions, Object-Oriented Programming, Exception
Handling, Magic (Special) Methods, File Operations, Text Operations.
To run the code, I recommend using VS Code (explained in Section 1/2B "Interactive Free Python Lab") or PyCharm
(explained in Section 1/3B "Interactive Free Python Lab"). Alternatively, you can search Google for: "free online Python
IDE".
Description: Build a tool to estimate monthly electricity costs for any device. Perfect for budgeting or comparing appliance
efficiency!
# ================================================
# ELECTRICITY COST CALCULATOR
# User input
device_power = float(input("Enter device power (W): "))
daily_hours = float(input("Daily usage (hours): "))
price_per_kwh = float(input("Electricity price ($/kWh): "))
# Calculations
daily_energy = (device_power * daily_hours) / 1000 # Convert W to kWh
monthly_cost = (daily_energy * 30) * price_per_kwh
# Display result
print(f"\n MONTHLY COST: {monthly_cost:.2f} $")
# ================================================
How It Works:
Input Parameters: The user provides three values: device power in watts, daily usage time, and electricity price. Example: A
1500 W iron used 2 hours daily at 0.75 $/kWh.
Calculations: Convert watts to kilowatts (/1000) because energy is billed per kWh. Multiply daily usage by 30 days and then
by the price per kWh.
User-Friendly Output: :.2f ensures the result is displayed to the nearest penny (e.g., 45.50 $).
# Sample Output:
Enter device power (W): 1500
Daily usage (hours): 4
Electricity price ($/kWh): 0.75
Description: Create a tool that converts travel time in hours to minutes and seconds. Perfect for route planning or activity
time calculations!
# ================================================
# TRAVEL TIME CONVERTER
number_of_hours = float(input("Enter travel time in hours: "))
min = number_of_hours * 60
sec = number_of_hours * 3600
How It Works:
# Sample Output:
Enter travel time in hours: 2.75
È Hungry for More? Level up your skills at our "Interactive Free Python Lab"
interactive-free-python-lab.uk
F-Strings, Operators, Conditional Statements, While Loop, For Loop, Control Statements, Functions, Built-in Functions,
Modules and Packages, Lists, Tuples, Dictionaries, Special Functions, Object-Oriented Programming, Exception
Handling, Magic (Special) Methods, File Operations, Text Operations.
To run the code, I recommend using VS Code (explained in Section 1/2B "Interactive Free Python Lab") or PyCharm
(explained in Section 1/3B "Interactive Free Python Lab"). Alternatively, you can search Google for: "free online Python
IDE".
4. Discount Calculator:
What You¾ll Learn?
Description: Build a tool to calculate discounted prices based on original prices and discount percentages. Ideal for e-
commerce or smart shopping!
# ================================================
# DISCOUNT CALCULATOR
price = float(input("Enter original price ($): "))
rebate = float(input("Enter rebate percentage (%): "))
How It Works:
# Sample Output
Enter original price ($): 249.99
Enter rebate percentage (%): 15