Introduction
Python Input and Output are the foundation of interactive programming. Input allows users to enter information, while output displays results on the screen. By learning these concepts, you can create programs that communicate with users, solve real-world problems, and build applications that respond to user input effectively.
Hinglish:Python Input aur Output interactive programming ki foundation hai. Input ka use karke user se information li jaati hai, aur Output ka use karke screen par result dikhaya jaata hai. In concepts ko seekhne ke baad aap aise programs bana sakte ho jo users se communicate karein, real-world problems solve karein, aur user ke input ke hisaab se response dene wale applications develop karein.Learning Objectives
After completing this lesson, you will be able to:
1.Understand the concept of Input and Output in Python.
2.Use the input() function to take data from users.
3.Use the print() function to display information.
4.Convert user input into different data types using Type Casting.
5.Format output using f-strings, sep, and end.
6.Write interactive Python programs using input and output.
7.Avoid common beginner mistakes related to user input.
8. Build simple real-world programs with confidence.
Table of Content
1. What is Input and Output in Python?
2. Python
print()Function3. Python
input()Function4. How
input()Works in Python5. Taking Integer Input (
int())6. Taking Float Input (
float())7. Taking Multiple Inputs
8. Output Formatting with f-Strings
9.
print()Parameters (sepandend)10. Escape Characters (
\n,\t,\\)11. Common Input and Output Errors
12. Practice Questions
13. Mini Project
14. Summary
1. What is Input and Output in Python?
Input is used to receive data from the user, and Output is used to display data or results on the screen. This process is called Input and Output in Python. It helps the Python program communicate with the user.
For example, a program asks the user to enter their name and displays a message like "Hello, Hamid!" or "Good Morning, Hamid!".
Example
name = input("Enter your name: ")
print(f"Hello,{name}")
print(f"Goodmorning,{name}")
Output
Enter your name:Hamid
Hello,Hamid
Goodmorning,Hamid
Explanation
- The program starts by displaying
"Enter your name:" using the input() function.
- The program waits until the user types a name and presses the Enter key.
- The entered name is stored in the
name variable.
- The
print() function then displays the output on the screen.
- If the user enters Hamid, the output will be
Hello, Hamid and Good Morning, Hamid.
- This is how Input and Output work in Python.
"Enter your name:" using the input() function.name variable.print() function then displays the output on the screen.Hello, Hamid and Good Morning, Hamid.2.Python print() Function
What is the print() Function?
The print() function is a built-in (predefined) function in Python.
This means it is already created by Python, so you do not need to define it yourself.
You only need to use it in your program.
The print() function is used to display information or output on the screen.
It helps users see messages, results, and other information generated by the program.
You can use it to display text, numbers, symbols, variables, and program results.
Real-Life Example
When you open an app or website like Instagram or ChatGPT, the first screen usually shows options such as Sign Up or Log In. You may also see instructions like:
- Enter your Name
- Enter your Email
- Enter your Password
- Click the Login button
After you submit your information, the screen displays a message such as "Login Successful", "Invalid Password", or "Welcome!".
In programming, the print() function is used to display similar
messages, instructions, and results on the screen so users can understand
what the program is doing.
3..Python input() Function
The input() function is a built-in Python function used to take user input and store the entered data in a variable.
Example
age = input("Enter your age: ")
print("Your age is", age)
Output
Enter your age:19
Your age is 19
Explanation
The program asks the user to enter a name using input(). The entered name is stored in the name variable, and print() displays a personalized greeting message.
4.How input() Works in Python
-
The
input()function asks the user to enter data and stores the entered value in a variable. -
The message inside
input()is displayed on the screen, and the program waits until the user enters data and presses the Enter key. -
The
print()function is used to display the stored data or result on the screen.
5.Taking Integer Input (int())
int(input())is used to take whole number input from the user.- Whole numbers are like 1, 5, 10, 100 (no decimal values).
- By default,
input()stores data as a string. int()converts the entered value into an integer.- It is used to perform mathematical operations like addition, subtraction, multiplication, and division.
Example
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("The sum of two numbers is:", a + b)
Output
Enter the first number:10
Enter the second number:5
The sum of two numbers is:15
6.Taking Float Input (float())
float(input())is used to take decimal number input from the user.- Decimal numbers are like 2.5, 10.75, 99.99.
- By default,
input()returns data as a string. float()converts the entered value into a float.- It is used for calculations with decimal numbers.
Example
price = float(input("Enter the price of Product: "))
print("Price of Product is:", price)
Output
Enter the price Product: 99.99
Price of Product is: 99.99
7.Take Multiple Values in Python
Definition
The input() function can take two or more values from the user in a
single line. This is called Multiple Input.
Key Points
- One
input()function is used to take multiple values. nameandageare two different variables..split()breaks one string into multiple parts.- By default,
.split()separates values using a space. - The first value is stored in the first variable, and the second value is stored in the second variable.
- By default,
input()returns all values as strings. - To perform mathematical operations, use
int()orfloat()withmap().
Example 1: Multiple String Input
name, age = input("Enter your name and age: ").split()
print(name, age)
# or
print(name)
print(age)
Output
Enter your name and age: Hamid 19
Hamid 19
Hamid
19
🍕 Real-Life Example: How .split() Works
Imagine you have one pizza 🍕 and two people 👨👩 want to eat it. The pizza is cut into multiple slices 🔪🍕 so everyone gets a separate piece.
In the same way, .split() breaks one string into multiple parts.
User enters:
Hamid 19
After using .split():
"Hamid 19"
⬇️
["Hamid", "19"]
Python stores the values like this:
name = "Hamid"
age = "19"
Remember:
- 🍕 One Pizza → 🍕🍕 Multiple Slices
- 📝 One String → ✂️ Multiple Values using
.split()
Example 2: Multiple Integer Input
a, b = map(int, input("Enter two numbers: ").split())
print("Sum:", a + b)
Output
Enter two numbers: 10 20
Sum: 30
How map() Works
.split()separates the entered values.map()applies the same function to every value.intconverts every value into an integer.floatconverts every value into a decimal number.- After conversion, mathematical operations can be performed.
Important Note
The number of variables and the number of values entered by the user must be the same. Otherwise, Python will show an error.
✅ Correct:
name, age = input().split()
Input:
Hamid 19
❌ Incorrect:
name, age = input().split()
Input:
Hamid
This gives an error because Python expects two values, but the user enters only one value.
8.Output Formatting with f-Strings
What is an f-String?
An f-string is a simple and modern way to display variables inside a string.
It makes the output more readable and easier to write.
Key Points
- Used to display variables inside a string.
- Start the string with
f. - Write variables inside
{ }(curly braces). - Makes the code clean and easy to read.
- Commonly used with the
print()function.
Syntax
print(f"Text {variable}")Example
name,age=input("Enter name and age:").split()
pritn(f"The Name is {name} and Age is {age} Years old")
Output
Enter name and age:Raju 20
The Name is Raju and Age is 20 years old
9.print() Parameters (sep and end)
What are sep and end?
sep and end are optional parameters of the print() function. They are used to format the output and make it more readable.
sep Parameter
The sep (separator) parameter is used to separate multiple values
printed by the print() function.
By default, Python uses a space between values. You can change
it to any symbol like -, /, |,
,, etc.
Example
print("Date", "Month", "Year", sep="/")
Output
Date/Month/Year
Explanation
sep="/"adds / between each value.- You can use any separator such as
-,|, or*.
Example
print("Python", "Java", "C++", sep=" | ")
Output
Python | Java | C++
end Parameter
The end parameter is used to change what is printed after the
output.
By default, every print() statement starts on a new line. Using
end allows the next print() statement to continue on
the same line.
Example
print("Hamid", end=" ")
print("Hasnain", end=" ")
print("Ali", end=" ")
print("Raju")
Output
Hamid Hasnain Ali Raju
Explanation
- By default,
print()moves to the next line. end=" "adds a space instead of a new line.- All values are printed on the same line.
📝 Remember
- ✅
sep= Separates multiple values. - ✅
end= Decides what comes after the output.
10. Escape Characters (\n, \t, \\)
An escape character is a special character used inside a string to represent special characters or perform special actions.
In Python, the escape character is the backslash (\).
There are many escape sequences, but some common and important ones are:
- \n → New line
- t → Tab space
- \\ → Backslash
- \' → Single quote
- \" → Double quote
These escape sequences are commonly used by programmers when working with strings.
Example of New Line \n Escape Character
\n is used to create a new line. It allows you to print multiple lines using a single print() statement.
\n is written inside the string.
print("Hello, How are you?\nWhat is your name?\nWhat is your work?")
Output
Hello, How are you?
What is your name?
What is your work?
Example of Tab space \t Escape Character
\t is used to add a tab space between two or more strings or pieces of text.
\t is written inside the string.
print("Name:\tHamid")
print("Age:\t19")
Output
Name: Hamid
Age: 20
Example of Backslash \\ Escape Character
\\ is used to print a backslash (\) inside a string.
Example Print a date from users
day=int(input("Enter Day:")
month=int(input("Enter Month:")
years=int(input("Enter Years:")
print(f"Current Date:\t{day}\\{month\\{years}")
Explanation
- input() → Takes Day, Month, and Year from the user.
- int() → Converts the input into an integer.
- f" " → Allows variables to be used directly inside the string.
- \t → Adds a tab space.
- \\ → Prints a backslash (`\`).
- {day}, {month}, {years} → Display the values entered by the user.
Output
Enter Day: 4
Enter Month:8
Enter Years:2026
Current Date: 4\8\2026
Example of Single Quote \' Escape Character
\' is used to print a single quote (') inside a string.
print('It\'s Python')
Output
It's Python
Example of Double Quote \" Escape Character
\" is used to print a double quote (") inside a string.
print("He said \"Hello\"")
Output
He said "Hello"
Comments
Post a Comment