×

First steps in Python

First steps in Python


Hello dear friends,

I am thrilled to share with you two Python projects that I’ve crafted after completing my basic programming courses. At the advice of my advisor, I decided to delve into the Python programming language to create models and simulations related to my field of study.

In this blog post, I present a straightforward Python code example that calculates the charging time and estimated completion time of a rechargeable battery. This project incorporates fundamental concepts of Python, including data types and mathematical operators.

I hope you find this post insightful and engaging as we explore the world of Python together. Let’s dive into the exciting realm of programming and simulations!

  1. from datetime import datetime, timedelta: We import the datetime and timedelta modules. These modules allow us to work with time-related operations.
  2. We define a function called rechargeable_battery_simulation(), which is used to calculate the charging time and estimated completion time of a rechargeable battery.
  3. We assign the battery properties (capacity, current charge level, maximum charge level, and charging power) to variables.
  4. To calculate the remaining charge, we subtract the current charge level from the maximum charge level.
  5. We calculate the charging time by dividing the remaining charge by the charging power.
  6. We convert the charging time to minutes by multiplying it by 60.
  7. We calculate the estimated completion time by adding the charging time to the current time.
  8. Finally, we print the calculated information and battery properties to the screen.

Used Python Concepts:

  • Variable declaration and assignment
  • Mathematical operators (+, -, *, /)
  • Data type conversions (float() and strftime())
  • Importing modules (from datetime import datetime, timedelta)
  • Basic time operations (datetime.now() and timedelta)

This simple code example demonstrates how to utilize the fundamental features of Python, such as data types and mathematical operations. You can simulate your rechargeable battery, calculate the charging time, and estimate when the battery will be fully charged.

Charging time and estimated completion time of a rechargeable battery:

from datetime import datetime, timedelta

def rechargeable_battery_simulation():
    capacity = float(input("Capacity of the Battery (mAh): "))
    current_charge = float(input("Current Charge Level (mAh): "))
    max_charge = float(input("Maximum Charge Level (mAh): "))
    charging_power = float(input("Charging Power (mAh/hour): "))

    # Calculate the total amount to be charged
    remaining_charge = max_charge - current_charge

    # Calculate the charging time (in hours)
    charge_time_hours = remaining_charge / charging_power

    # Convert charging time to minutes
    charge_time_minutes = charge_time_hours * 60

    # Calculate the estimated completion time
    completion_time = datetime.now() + timedelta(minutes=charge_time_minutes)

    print("\n--- Rechargeable Battery Simulation ---")
    print(f"Capacity of the Battery: {capacity} mAh")
    print(f"Current Charge Level: {current_charge} mAh")
    print(f"Maximum Charge Level: {max_charge} mAh")
    print(f"Charging Power: {charging_power} mAh/hour")
    print(f"Remaining Charge: {remaining_charge} mAh")
    print(f"Charging Time: {charge_time_hours:.2f} hours ({charge_time_minutes:.2f} minutes)")
    print(f"Estimated Completion Time: {completion_time.strftime('%H:%M:%S')}")

rechargeable_battery_simulation()
  1. From datetime import datetime, timedelta: We import the datetime and timedelta modules. These modules allow us to work with time-related operations.
  2. We define a function called rechargeable_battery_simulation(), which is used to calculate the charging time and estimated completion time of a rechargeable battery.
  3. We assign the battery properties (capacity, current charge level, maximum charge level, and charging power) to variables.
  4. To calculate the remaining charge, we subtract the current charge level from the maximum charge level.
  5. We calculate the charging time by dividing the remaining charge by the charging power.
  6. We convert the charging time to minutes by multiplying it by 60.
  7. We calculate the estimated completion time by adding the charging time to the current time.
  8. Finally, we print the calculated information and battery properties to the screen.

Battery capacity calculation


# Battery capacity calculation

# Enter the voltage and ampere-hour of the battery
voltage = float(input("Voltage of the Battery: "))
ampere_hour = float(input("Current charge level: "))

# Calculate the battery capacity by multiplying the voltage and ampere-hour
capacity = voltage * ampere_hour

# Print the battery capacity
print("Battery capacity:", capacity, "Ah")

In this code example, we focus on calculating the capacity of a rechargeable battery.

By providing the battery’s voltage and ampere-hour (Ah) values as input, the Python program efficiently computes the battery’s capacity. This project highlights the use of basic mathematical operations and variable assignments in Python.

“As of now, the place I work at is Gebze Technical University’s Cafeteria. It’s 10:13 PM.

#Tomorrow belongs to those who sacrifice comfort.

Yorum gönder