Topic: Input and Output in Python
Definition
Input means taking data from the user.
Output means showing data on the screen.
1) Output in Python
Python uses the print() function to display anything on the screen.
Example
print("Hello World")
Output
Hello World
Important Point
print() is used to show:
• text
• numbers
• results
• variables
2) Input in Python
Python uses the input() function to take data from the user.
Example
name = input("Enter your name: ")
print(name)
Important Point
input() always takes data in string form.
3) Input as String
When we enter text, Python stores it as a string.
Example
city = input("Enter your city: ")
print("Your city is", city)
4) Input as Integer
If we want whole numbers, we use int() with input().
Example
age = int(input("Enter your age: "))
print("Your age is", age)
Note
int() converts string into integer.
5) Input as Float
If we want decimal numbers, we use float().
Example
price = float(input("Enter price: "))
print("Price is", price)
Note
float() converts string into decimal number.
6) Printing Variables
We can print variables in different ways.
Example 1
name = "Ali"
print(name)
Example 2
name = "Ali"
print("My name is", name)
7) Input and Output Together
Example
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Name:", name)
print("Age:", age)
8) Important Points to Remember
• print() is used for output
• input() is used for input
• input() always takes data as string
• Use int() for whole numbers
• Use float() for decimal numbers