Chapter 4: Input & Output Operations
Learn how Python programs interact with users by accepting input, displaying output, converting data types, and building simple interactive applications.
> Enter your name: Amin
> Enter marks: 85
Output: Welcome Amin
Output: Your marks are 85
4.1 Chapter Overview
Input and output operations are important because they allow a Python program to communicate with users. Output means displaying information to the user, while input means accepting information from the user.
In real applications, users enter names, marks, prices, quantities, passwords, email addresses, and many other values. Python accepts these values using the input() function and displays results using the print() function.
4.2 Chapter Objectives
- Understand the purpose of input and output operations.
- Use print() to display output in Python.
- Use input() to accept values from users.
- Understand why input values are received as strings.
- Convert input data to integer and float values.
- Perform calculations using user input.
- Develop simple interactive Python applications.
Learning Outcomes
- Create Python programs that accept user input.
- Convert user input into suitable data types.
- Calculate totals, averages, areas, fees, and results.
- Display clear and professional output messages.
4.3 Output Operation using print()
Output means showing information on the screen. In Python, the print() function is used to display output.
Example
print("Welcome to Python Programming")
print("Perak Digital Transformation Centre")
print("Chapter 4: Input and Output Operations")
Welcome to Python Programming
Perak Digital Transformation Centre
Chapter 4: Input and Output Operations
4.4 Displaying Variables with print()
The print() function can also display values stored in variables.
student_name = "Amin"
course_name = "Certified Python Programmer"
training_centre = "PDTC"
print("Student Name:", student_name)
print("Course Name:", course_name)
print("Training Centre:", training_centre)
Student Name: Amin
Course Name: Certified Python Programmer
Training Centre: PDTC
4.5 Formatting Output
Output should be clear and easy to understand. Python allows different ways to format output.
Using f-string
name = "Amin"
marks = 85
print(f"Student {name} scored {marks} marks.")
Student Amin scored 85 marks.
An f-string allows variables to be placed directly inside a message using curly brackets.
4.6 Input Operation using input()
Input means accepting data from the user. In Python, the input() function is used to take user input.
Example
student_name = input("Enter your name: ")
print("Welcome", student_name)
Enter your name: Amin
Welcome Amin
4.7 Input Values are Strings by Default
Even if the user enters a number, Python receives it as text when using input(). This is why data type conversion is required before calculation.
age = input("Enter your age: ")
print(type(age))
Enter your age: 20
<class 'str'>
4.8 Data Type Conversion while Taking Input
Type conversion changes input data from string to another data type such as integer or float.
| Function | Purpose | Example |
|---|---|---|
| int() | Converts input into a whole number. | age = int(input("Enter age: ")) |
| float() | Converts input into a decimal number. | price = float(input("Enter price: ")) |
| str() | Converts a value into text. | message = str(number) |
Integer Input Example
age = int(input("Enter your age: "))
print("Your age is:", age)
print(type(age))
Float Input Example
course_fee = float(input("Enter course fee: "))
print("Course Fee:", course_fee)
print(type(course_fee))
4.9 Calculating Using User Input
After converting input values into numbers, Python can perform calculations.
Add Two Numbers
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
total = number1 + number2
print("Total:", total)
Enter first number: 10
Enter second number: 5
Total: 15
4.10 Average Marks Calculator
This program accepts three subject marks, calculates total marks and average marks, and displays the result.
python_marks = float(input("Enter Python marks: "))
database_marks = float(input("Enter Database marks: "))
web_marks = float(input("Enter Web Development marks: "))
total_marks = python_marks + database_marks + web_marks
average_marks = total_marks / 3
print("Total Marks:", total_marks)
print("Average Marks:", average_marks)
Enter Python marks: 80
Enter Database marks: 75
Enter Web Development marks: 85
Total Marks: 240.0
Average Marks: 80.0
4.11 Course Fee Calculator
This example shows how user input can be used in a training centre fee calculation system.
course_fee = float(input("Enter course fee: "))
discount = float(input("Enter discount amount: "))
registration_fee = float(input("Enter registration fee: "))
final_amount = course_fee - discount + registration_fee
print("Course Fee: RM", course_fee)
print("Discount: RM", discount)
print("Registration Fee: RM", registration_fee)
print("Final Amount Payable: RM", final_amount)
Enter course fee: 1500
Enter discount amount: 300
Enter registration fee: 100
Final Amount Payable: RM 1300.0
4.12 Developing Interactive Python Applications
An interactive Python application accepts user input, processes the input, and provides output based on the user’s data.
Interactive Student Profile Application
name = input("Enter student name: ")
age = int(input("Enter student age: "))
course = input("Enter course name: ")
marks = float(input("Enter final marks: "))
print("----- Student Profile -----")
print("Name:", name)
print("Age:", age)
print("Course:", course)
print("Final Marks:", marks)
4.13 Interactive Certificate Eligibility App
This application checks whether a student is eligible for a certificate based on marks and attendance.
student_name = input("Enter student name: ")
marks = float(input("Enter marks: "))
attendance = float(input("Enter attendance percentage: "))
eligible = marks >= 50 and attendance >= 80
print("Student Name:", student_name)
print("Certificate Eligibility:", eligible)
Enter student name: Amin
Enter marks: 75
Enter attendance percentage: 90
Certificate Eligibility: True
4.14 Common Beginner Mistakes
| Mistake | Wrong Example | Correct Example |
|---|---|---|
| Calculating without converting input | total = input1 + input2 | total = int(input1) + int(input2) |
| Forgetting closing bracket | name = input("Enter name:" | name = input("Enter name:") |
| Using int for decimal values | fee = int(input("Enter fee: ")) | fee = float(input("Enter fee: ")) |
| Unclear output message | print(total) | print("Total Marks:", total) |
4.15 Hands-On Practice
Activity 1: Greeting Application
Create a Python program that asks for the user’s name and displays a welcome message.
Activity 2: Age Calculator
Ask the user to enter their birth year. Calculate and display their approximate age.
birth_year = int(input("Enter your birth year: "))
current_year = 2026
age = current_year - birth_year
print("Your age is approximately:", age)
Activity 3: Rectangle Area Calculator
Ask the user to enter length and width. Calculate and display the area.
Mini Project: Interactive Training Fee Calculator
Create a Python program that asks for student name, course fee, discount, and registration fee. The program must calculate and display the final payable amount clearly.
4.16 Final Assessment Quiz
Answer the following questions. Correct Answer = +1 Mark Wrong Answer = -0.5 Mark
1. The print() function is used to display output in Python.
2. The input() function always returns data as an integer.
3. int() can be used to convert user input into a whole number.
4. float() is suitable when accepting decimal values such as course fees or marks.
5. Interactive Python applications accept input, process data, and display output.
Your Score: 0
Final Practical Assessment
Develop an interactive Python program named training_fee_calculator.py. The program must ask for student name, course name, course fee, discount amount, and registration fee. It must calculate and display the final amount payable using proper data type conversion.
4.17 Chapter Summary
In this chapter, learners studied how Python programs communicate with users using input and output operations. Learners practiced using print(), input(), int(), and float() to build interactive Python applications.