π― Lesson Objective
To understand how to take user input, display output, and convert data types in Python programs.
π§ 1. Input in Python
The input() function allows users to enter data from the keyboard.
By default, it returns data as a string.
Example:
name = input("Enter your name: ")
print("Hello,", name)
πΉ Example 2: Taking numeric input
num = input("Enter a number: ")
print(num + 10) # β Error (string + int not allowed)
To fix this, convert input to an integer:
num = int(input("Enter a number: "))
print(num + 10) # β
Works correctly
π¬ 2. Output in Python
Use the print() function to display information.
Example:
name = "Ali"
age = 25
print("My name is", name, "and I am", age, "years old.")
πΉ Formatting Output
You can format text neatly using f-strings:
name = "Sara"
age = 21
print(f"My name is {name} and I am {age} years old.")
Another method: format()
print("My name is {} and I am {} years old.".format(name, age))
π 3. Type Casting
Type casting means converting one data type to another.
Common conversion functions:
| Function | Description | Example | Output |
|---|---|---|---|
int() | Converts to integer | int("10") | 10 |
float() | Converts to float | float("3.14") | 3.14 |
str() | Converts to string | str(25) | “25” |
bool() | Converts to boolean | bool(0) | False |
Example:
x = "100"
print(int(x) + 50) # 150
π 4. Combining Input, Output & Type Casting
Example Program:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Welcome {name}! Next year you will be {age + 1} years old.")
Output:
Enter your name: Ahmed
Enter your age: 20
Welcome Ahmed! Next year you will be 21 years old.
π§© 5. Real-Life Example
Suppose you are building a billing program:
item = input("Enter item name: ")
price = float(input("Enter item price: "))
quantity = int(input("Enter quantity: "))
total = price * quantity
print(f"Total cost for {item} = βΉ{total}")
π§ 6. Practice Ideas
Take age as input and display how old the person will be after 10 years.
Write a program to input two numbers and display their sum, difference, and product.
Create a greeting program that asks for the userβs name and city, then prints a personalized message.
Convert a string "123.45" to a float and print its type.

Leave a Reply