Input and Output in Python
0 948
Introduction
Input and output operations are fundamental in Python programming. They allow interaction with users and display of results. Python provides built-in functions likeinput() and print() to facilitate these operations.
Reading Input with input()
The input() function prompts the user for input and returns it as a string. To obtain other data types, explicit type conversion is necessary.
name = input("Enter your name: ")
print("Hello,", name, "!")
Output:
Enter your name: Alice
Hello, Alice !
Type Conversion
Sinceinput() returns a string, converting it to other types like integers or floats requires casting.
age = int(input("Enter your age: "))
print("You are", age, "years old.")
Output:
Enter your age: 30
You are 30 years old.
Printing Output with print()
The print() function displays output to the console. It can print strings, variables, and expressions.
print("Welcome to Python programming!")
Output:
Welcome to Python programming!
Printing Multiple Variables
Multiple variables can be printed by separating them with commas.name = "Bob"
age = 25
print(name, "is", age, "years old.")
Output:
Bob is 25 years old.
Taking Multiple Inputs
To take multiple inputs in a single line, usesplit().
x, y = input("Enter two numbers: ").split()
print("First number:", x)
print("Second number:", y)
Output:
Enter two numbers: 10 20
First number: 10
Second number: 20
Conditional Input Handling
Inputs can be used in conditional statements after appropriate type conversion.age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
else:
print("You are an adult.")
Output:
Enter your age: 16
You are a minor.
Conclusion
Understanding how to effectively useinput() and print() is essential for building interactive Python programs. These functions form the basis of user interaction and output display.If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments