0% found this document useful (0 votes)
27 views1 page

Basic Data Types in Python With Examples

This document explores fundamental data types in Python through practical examples. Mastering these basic data types is essential for writing efficient and reliable programs. Included Examples: 1. Fuel Efficiency Calculator 2. Electricity Cost Calculator 3. Travel Time Converter 4. Discount Calculator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
27 views1 page

Basic Data Types in Python With Examples

This document explores fundamental data types in Python through practical examples. Mastering these basic data types is essential for writing efficient and reliable programs. Included Examples: 1. Fuel Efficiency Calculator 2. Electricity Cost Calculator 3. Travel Time Converter 4. Discount Calculator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 1

Basic Data Types in Python with Examples T

1. Fuel Efficiency Calculator:


What Will You Learn?

This example will help you master:

' Keyboard input handling (input())

' Type conversion (float())

' Mathematical operations and variables

' Text formatting with f-strings

' Practical unit conversions

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)

Expand This Project! m

Add a trip cost calculator based on fuel price

Create a version for UK gallons (1 UK gallon = 4.546 liters)

Display eco-ratings (e.g., "low emission" for < 8 l/100km)

À Ready for More? Visit our "Interactive Free Python Lab"

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".

2. Electricity Cost Calculator:


What Will You Learn?

This project covers:

' Handling multiple user inputs

' Unit conversions (W ³ kW)

' Simulating real-world economic scenarios

' Currency formatting in results (f-strings)

' Mathematical order of operations

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

# MONTHLY COST: 135.00 $

Expand This Project! T

Add annual cost comparisons

Create a table for multiple devices

Implement budget alerts

3. Travel Time Converter:


What You¾ll Learn?

This project will help you understand:

' User input handling and data conversion (input(), float())

' Basic arithmetic operations

' Formatting rounded results

' Creating clear output messages

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

print(f"\n The result of our converter is:")


print(f"- {number_of_hours} hours equals {min:.0f} minutes and {sec:.0f} seconds")
# ================================================

How It Works:

User enters hours (e.g., 1.5 for 1h 30m)

Multiply by 60 to get minutes

Multiply by 3600 to get seconds

Results are rounded for clarity

# Sample Output:
Enter travel time in hours: 2.75

The result of our converter is:


- 2.75 hours equals 165 minutes and 9900 seconds

Expand This Project! À

Add conversions to days/weeks

Estimate arrival time based on average speed

Create a flight time version with time zones

È Hungry for More? Level up your skills at our "Interactive Free Python Lab"

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!

where you can:

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?

In this project, you¾ll master:

' Working with multiple variables

' Percentage calculations

' Currency amount formatting

' Mathematical operator precedence

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 (%): "))

rebate_amount = price * (rebate / 100)


final_price = price - rebate_amount

print(f"\n Discounted price: {final_price:.2f} $")


print(f"(You save: {rebate_amount:.2f} $)")
# ================================================

How It Works:

Program takes base price and discount percentage

Calculates discount amount (e.g., 15% of 249.99 $ = 37.50 $)

Subtracts discount from original price

Displays results with 2 decimal places

# Sample Output
Enter original price ($): 249.99
Enter rebate percentage (%): 15

# Discounted price: 212.49 $


(You save: 37.50 $)

Expand This Project! ä

Add alerts for discounts over 50%

Compare prices across stores

Create a purchase history in CSV files

You might also like