note.py

Day 2 — Input & Calculations

Your code can display a message. Now let it start a conversation. Day 2 turns user input into numbers, calculations, and clear results. Build a purchase cost calculator—one practical step in your 10-day Python journey.

article = {published

Day 2 — Input & Calculations

Today, you will learn how to ask users for information, convert their answers into numbers, calculate results, and display them clearly.

Your project is a purchase cost calculator.

Your 180-minute study plan

ActivityTimeWhat you will do
Review20 minutesPractise variables, print, and data types
Lesson40 minutesLearn input, conversions, operators, and f-strings
Guided exercises50 minutesComplete five exercises
Mini-project50 minutesBuild your purchase cost calculator
Review and self-check20 minutesTest your program and explain your code
Total180 minutesBreaks are separate

Review — 20 minutes

Spend 5 minutes opening IDLE, creating a new file, and saving it as day02.py. Use Run → Run Module to execute your code.

Spend 10 minutes writing a program without copying yesterday’s example:

  • Create name with a text value.
  • Create age with an integer value.
  • Create study_hours with a decimal value.
  • Print each value with a label.
  • Change one value and run the program again.

Spend 5 minutes explaining the difference between 25 and "25".

Remember: str means text, int means an integer, and float means a floating-point number, such as 2.5.

Lesson — 40 minutes

Spend 10 minutes on each topic below. Type the examples yourself, predict the output, run them, and change a value.

1. Get information with input

Input is information a user gives your program. Output is information your program displays.

name = input("What is your name? ")
print("Hello,", name)

Line by line:

  • Line 1: Displays the question and waits. When the user types an answer and presses Enter, the answer is assigned to name.
  • Line 2: Displays Hello, followed by the value of name.

Example interaction:

What is your name? Ali
Hello, Ali

input always returns a string—even when the user types a number.

Change the example to ask for a city instead of a name.

2. Convert text into numbers

Type conversion means changing a value from one data type to another.

ToolPurposeExample
int(...)Convert suitable input into an integer"12" becomes 12
float(...)Convert suitable input into a floating-point number"2.5" becomes 2.5
str(...)Convert a value into text12 becomes "12"

Example:

age_text = input("Enter your age: ")
age = int(age_text)
print("Age:", age)

Line by line:

  • Line 1: Gets the answer as text.
  • Line 2: Converts that text into an integer and assigns it to age.
  • Line 3: Displays the numeric value with a label.

For a decimal value:

hours_text = input("Enter study hours: ")
hours = float(hours_text)
print("Study hours:", hours)

Line by line:

  • Line 1: Gets the study hours as text.
  • Line 2: Converts the text into a floating-point number.
  • Line 3: Displays the result.

You can also combine the first two steps:

hours = float(input("Enter study hours: "))

Python first runs the inner part, input(...), then converts the answer with float(...).

Use inputs such as 3 for int and 2.5 for float. Use a decimal point, not a comma. int("3.5") cannot convert that text directly. We will learn how to handle invalid input on Day 8.

3. Calculate with operators

An operator is a symbol that performs an operation.

OperatorMeaningExampleResult
+Addition8 + 210
-Subtraction8 - 26
*Multiplication8 * 216
/Division8 / 24.0

Example:

morning_hours = 1.5
evening_hours = 2.0
total_hours = morning_hours + evening_hours
print("Total hours:", total_hours)

Line by line:

  • Line 1: Sets the morning study hours.
  • Line 2: Sets the evening study hours.
  • Line 3: Adds the two values and assigns the result to total_hours.
  • Line 4: Displays the total.

Output:

Total hours: 3.5

The + operator behaves differently with strings:

print("10" + "5")
print(10 + 5)
  • Line 1: Joins two strings and displays 105.
  • Line 2: Adds two numbers and displays 15.

This is why you must convert numeric input before calculating.

Multiplication and division happen before addition and subtraction. Parentheses change the order:

ExpressionResult
2 + 3 * 414
(2 + 3) * 420

4. Display values with f-strings

An f-string, or formatted string, lets you put values inside text.

name = "Sara"
hours = 2.5
print(f"{name} studied for {hours} hours.")

Line by line:

  • Line 1: Sets the name.
  • Line 2: Sets the study hours.
  • Line 3: Places both values inside the sentence and displays it.

Output:

Sara studied for 2.5 hours.

Put f immediately before the opening quotation mark. Put variable names inside braces: {}.

To display two decimal places:

amount = 12.5
print(f"Amount: {amount:.2f}")
  • Line 1: Assigns the number to amount.
  • Line 2: Displays it as Amount: 12.50.

The format :.2f means display this number with two digits after the decimal point. It changes the display, not the stored value.

Guided exercises — 50 minutes

Spend 10 minutes on each exercise: 6 minutes writing, 2 minutes checking, and 2 minutes running it with different input. Try before asking for the solution.

Exercise 1 — Say hello

Ask for the user’s name and display a greeting.

Hint: Assign the result of input to a variable.

Exercise 2 — Count books

Ask how many books the user has. Convert the answer into an integer and display it with a label.

Hint: Use int to convert the text.

Exercise 3 — Add study hours

Ask for morning and evening study hours. Calculate and display the total.

Hint: Both inputs may contain decimal values.

Exercise 4 — Calculate an average

Ask for two grades. Calculate and display their average.

Hint: Add the grades first, then divide by two. Think about parentheses.

Exercise 5 — Build a sentence

Ask for the user’s city and commute duration in hours. Display both values in one sentence using an f-string. Show the duration with two decimal places.

Hint: The city is text; the duration is a number.

Mini-project — 50 minutes

Create a new file called purchase_cost.py.

Your program must ask for:

InformationSuggested variableType
Product nameproduct_namestr
Quantityquantityint
Unit priceunit_pricefloat
Shipping costshipping_costfloat

Calculate:

Product cost = quantity × unit price
Final cost = product cost + shipping cost

Display the product name, quantity, product cost, shipping cost, and final cost. Use f-strings and show all amounts with two decimal places.

TimeTask
10 minutesPlan your inputs, types, formulas, and output
20 minutesWrite the program
10 minutesRun the two checks below and fix mistakes
10 minutesReopen the saved file, run it again, and explain each line

Check your calculations against these expected results:

QuantityUnit priceShippingProduct costFinal cost
210030200.00230.00
312.5037.5037.50

For today, enter valid numbers. Input validation will come later.

Review and self-check — 20 minutes

Spend 5 minutes explaining input, type conversion, operators, and f-strings in your own words.

Spend 10 minutes answering these questions—two minutes each:

  1. What type does input return when the user enters 7?
  2. Which conversion should you use for a price of 19.75?
  3. How does "4" + "6" differ from 4 + 6?
  4. What do f and {} do in an f-string?
  5. Why are parentheses useful when calculating an average?

Spend 5 minutes running your project with new values and checking that the result changes correctly.

Success criteria

  • Your program receives all four inputs.
  • Quantity uses int; prices and shipping use float.
  • Both calculation checks produce the expected results.
  • Amounts display with two decimal places.
  • You can explain why numeric inputs need conversion.
  • You answer at least four self-check questions correctly.

}
reply.py

Leave a Reply

Your email address will not be published. Required fields are marked *