print() output function in Python with different examples.
🔹 Examples of Using Output Function (print())
1. Basic text output
print(" Welcome to ICT Club)
🖥 Output:
Welcome to ICT Club
2. Printing numbers
print(100)
print(3.14)
🖥 Output:
100
3.14
3. Printing multiple values
name = "Alice"
age = 18
print("My name is", name, "and I am", age, "years old.")
🖥 Output:
My name is Alice and I am 18 years old.
4. Using escape characters (\n,\t)
print("Line1\nLine2\nLine3") # new lines
print("Name\tAge\tClass") # tabs (like a table)
🖥 Output:
Line1
Line2
Line3
Name Age Class
7. Printing calculations
a=5
b=3
print("Sum:", a + b)
print("Product:", a * b)
🖥 Output:
Sum: 8
Product: 15
Exercise
1. Write a program that prints Hello, Python!
2. Print your name and age using the print() function.
3. Print the numbers 10, 20, and 30 each on a separate line.
4. Print the following output using \n:
Red
Green
Blue
5. Print this table using \t:
Name Age
John 15
Mary 16
6. Write a program that stores your favorite food in a variable and prints:
My favorite food is ___
7. Print the sum, difference, and product of 15 and 7 in one program.
8. Write a program that asks the user for two numbers and prints their average.
INPUT FUNCTION
The input() function allows the program to ask the user for data.
🔹 Examples of Using Input Function (input())
1. Basic input
name = input("Enter your name: ")
print("Hello,", name)
2. Getting numbers (with conversion)
age = int(input("Enter your age: "))
print("Next year you will be", age + 1)
🖥 Example run:
Enter your age: 17
Next year you will be 18
3. Getting decimal numbers
height = float(input("Enter your height in meters: "))
print("Your height is", height, "meters")
🖥 Example run:
Enter your height in meters: 1.72
Your height is 1.72 meters
4. Multiple inputs
first = input("Enter first name: ")
last = input("Enter last name: ")
print("Your full name is", first, last)
5. Simple calculator (with input)
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("The sum is", a + b)
7. Decision-making with input
password = input("Enter password: ")
if password == "python123":
print("Access granted ✅")
else:
print("Access denied ❌")
EXERCISE
🔹 Part A: Basic Input
1. Write a program that asks the user for their name and prints a greeting.
Example:
Enter your name: Alice
Hello, Alice
2. Ask the user to enter their school name and print:
You study at ___
🔹 Part B: Numbers with Input
3. Write a program that asks for the user’s age (as an integer) and prints how old they will
be in 5 years.
4. Ask the user to enter two numbers and print their sum.
5. Modify the program above to print their difference and product
🔹 Part C: Multiple Inputs
6. Write a program that asks for the user’s first name and last name separately, then prints
their full name.
7. Ask the user for their favorite subject and favorite color, then print a sentence that
combines them.
Example:
8. My favorite subject is Math and my favorite color is Blue.
🔹 Part D: Real-World Mini Programs
8. Ask the user for the length and width of a rectangle and calculate the area.
9. Ask the user for the current year and their birth year, then calculate their age.
🔹 Part E: Challenge ⭐
11. Create a program that works like a simple login system:
Ask the user to enter a username and password.
If they match "admin" and "1234", print "Login successful".
Otherwise, print "Invalid login".