Standard input and output

In this section, we are going to learn about input and output in Python. We will learn about stdin and stdout, and the input() function.

stdin and stdout are file-like objects. These objects are provided by the operating system. Whenever a user runs a program in an interactive session, stdin acts as input and stdout will be the user's Terminal. As stdin is a file-like object, we have to read the data from stdin rather than reading data at runtime. stdout is used for the output. It is used as an output for expressions and print() function, as well as a prompt for the input() function.

Now, we will see an example of stdin and stdout. For that purpose, create a script, stdin_stdout_example.py, and write the following content in it:

import sys

print("Enter number1: ")
a = int(sys.stdin.readline())

print("Enter number2: ")
b = int(sys.stdin.readline())

c = a + b
sys.stdout.write("Result: %d " % c)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 stdin_stdout_example.py
Enter number1:
10
Enter number2:
20
Result: 30

In the preceding example, we have used stdin and stdout for taking input and displaying the output. The sys.stdin.readline() will read from stdin. The will write the data.

Now, we will learn about the input() and print() functions. The input() function is used for taking input from the user. The function has an optional parameter: prompt string.

Syntax:

            input(prompt)

The input() function returns a string value. If you want a number value, simply write the 'int keyword before input(). You can do this as follows:

            int(input(prompt))

Similarly, you can write float for float values. Now, we will look at an example. Create a input_example.py script and write the following code in it:

str1 = input("Enter a string: ")
print("Entered string is: ", str1)
print()

a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
c = a + b
print("Value of c is: ", c)
print()

num1 = float(input("Enter num 1: "))
num2 = float(input("Enter num 2: "))
num3 = num1/num2
print("Value of num 3 is: ", num3)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 input_example.py
Output:
Enter a string: Hello
Entered string is: Hello
Enter the value of a: 10
Enter the value of b: 20
Value of c is: 30
Enter num 1: 10.50
Enter num 2: 2.0
Value of num 3 is: 5.25

In the preceding example, we used input() function for three different values. Firstly for string, second for integer value, and third for float value. To use input() for integer and float, we have to use int() and float() type conversion functions to convert the received string into integer and float respectively.

Now, the print() function is used to output the data. We have to put in a comma-separated list of arguments. In input_example.py, to get the output, we used the print() function. Using the print() function, you can simply write the data on to your screen by enclosing them in the  " " or ' '. To access just the value, just write the variable name in the print() function. If you want to write some text as well as accessing a value in the same print() function, then separate these two by putting a comma between them.

We will look at a simple example for the print() function. Create a print_example.py script and write the following content in it:

# printing a simple string on the screen.
print("Hello Python")

# Accessing only a value.
a = 80
print(a)

# printing a string on screen as well as accessing a value.
a = 50
b = 30
c = a/b
print("The value of c is: ", c)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 print_example.py
Hello Python
80
The value of c is: 1.6666666666666667

In the preceding example, first, we simply printed a string on the screen. Next, we just accessed the value of a and printed it on the screen. Lastly, we entered the values of a and b, then added them and stored the result in the variable c, and then we printed a statement and accessed a value from the same print() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset