Input and output are fundamental concepts in programming that allow developers to interact with users and display results to them. In this article, we'll delve into the various ways to handle input and output in Python, along with extensive code examples for better understanding.
Python provides a simple and versatile method for taking input from users through the input() function. Here's how it works:
# Taking input from the user
name = input("Enter your name: ")
# Output
print("Hello, " + name)
print(type(name))
In this example, the input() function prompts the user for input, and whatever they type is stored in the variable name. The subsequent print() statement displays a greeting message along with the entered name, followed by printing the data type of the input (which will be a string).
You can also use the input() function to take integer input from users:
# Taking input from the user as an integer
num = int(input("Enter a number: "))
add = num + 1
# Output
print(add)
In this snippet, the input is converted to an integer using the int() function, enabling mathematical operations.
Python allows you to receive multiple inputs of the same data type at once using the split() method in combination with the map() function:
a, b, c = map(int, input("Enter the Numbers: ").split())
print("The Numbers are:", a, b, c)
Here, the user can input three space-separated numbers, and the map() function converts them to integers.
You can collect list or set elements either by using iterative methods (like append() or add()) or by employing the map() and list()/set() methods:
List = list()
Set = set()
l = int(input("Enter the size of the List: "))
s = int(input("Enter the size of the Set: "))
print("Enter the List elements:")
for i in range(0, l):
List.append(int(input()))
print("Enter the Set elements:")
for i in range(0, s):
Set.add(int(input()))
print(List)
print(Set)
List = list(map(int, input("Enter List elements: ").split()))
Set = set(map(int, input("Enter the Set elements: ").split()))
print(List)
print(Set)
While tuples are immutable, you can still add elements using a workaround:
T = (2, 3, 4, 5, 6)
print("Tuple before adding new element:", T)
L = list(T)
L.append(int(input("Enter the new element: ")))
T = tuple(L)
print("Tuple After adding the new element:", T)
Python's print() function is a versatile tool for displaying output:
print("GFG")
# Disabling softspace feature
print('G', 'F', 'G')
You can adjust the sep and end parameters to control separation and line endings:
print("GFG", end="@")
print('G', 'F', 'G', sep="#")
Python offers various methods for output formatting:
name = "Gfg"
print(f'Hello {name}! How are you?')
a = 20
b = 10
sum = a + b
sub = a - b
print('The value of a is {} and b is {}'.format(a, b))
print('{2} is the sum of {0} and {1}'.format(a, b, sum))
print('{sub_value} is the subtraction of {value_a} and {value_b}'.format(value_a=a, value_b=b, sub_value=sub))
num = int(input("Enter a value: "))
add = num + 5
print("The sum is %d" % add)
In this comprehensive guide, we explored the various ways of handling input and output in Python. From taking user input using input() to formatting and displaying output using the print() function, you've learned the essential techniques to communicate with users and showcase results effectively in Python programs. Armed with this knowledge, you're ready to embark on your programming journey with confidence!
Introduction to Input and Output:
Taking Input from Users:
Accepting Integer Input:
Taking Multiple Inputs:
Input for Sequence Data Types:
Taking Input for Tuple:
Displaying Output:
Formatting Output: