Python Input and Output(input() & print())
Python Input and Output(input() & print())
Introduction
Just like we need clear instructions in real life to complete a task, Python programs need input from users to work correctly. In Python, we take input using the input() function, and after processing that input, we display the result as output using the print() function.
In this blog, you'll learn Python Input and Output with easy examples.
What is Output in Python?
Output means showing something on the screen. Python uses the print() function to display output.
Example:
print("Hello, world!")
Output:
Hello, world!
Printing Multiple Values
You can print multiple values in one line.
Code:
name = "Sundeep"
age = 21
Output:
Sundeep 21
Print Text with Variable
Code:
name = "Python"
print("I am learning", name)
Output:
I am learning Python
What is Input in Python?
Input means taking data from the user. Python uses the input() function to take user input.
Example:
name = input("Enter your name:")
print("Welcome", name)
But remember, by default, the input() function always takes input as a string.
Example:
a= input("Enter the number1: ")
b=input("Enter the number2:")
print("result by adding a and b:", a + b )
Output:
Enter the number1: 10
Enter the number2: 20
result by adding a and b: 1020
Here, even if you enter a number, Python will return the value as text(string), that's why, instead of adding two numbers, Python concatenated them. So to take integer or decimal numbers, we need to convert input using the int() function for integer type and the float() function for decimal numbers.
Example:
a= int(input("Enter the number1: "))
b=int(input("Enter the number2:"))
print("result by adding a and b:", a + b )
Output:
Enter the number1: 10
Enter the number2: 20
result by adding a and b: 30
Conclusion
- input() used to take input.
- print() used to display output.
- input() has string as default type.
Practice Questions (Must Try)
- Take a number and print a welcome message.
- Take two numbers and print their sum.
- Take length and breadth and calculate the area of a rectangle.
- Take marks and Display percentage.
- Ask the user's age and print whether they can vote.
Comments
Post a Comment