Input and Output
Input and Output
As you saw in the video lesson, input and outputs play an important role in software. Python has
numerous built-in functions/methods to handle I/O tasks. You got to know about two of the most
commonly used methods named input() and print(). We will look into a bit more detail about
each method and what additional functionalities they have in this tutorial.
Inputs
When it comes to input, the entity performing the action should not necessarily be a human. In
most use cases, it is probably one computer software interacting with another. For this lesson,
we will only focus on the human aspect of the process, and in future lessons you will learn how
to write programs that interact with each other.
One of the main methods a human can interact with the software is through the console. By now
you should have a good understanding of what a console is and how to use it. When it comes to
python programs, there is another method to interact with it in the form of the IDLE. In either
case, to take input from the user, we can use the input()method. This instructs the program to
pause and wait till the user input something. By pressing the enter key we notify the program
that we have completed typing our input and requests the program to process it.
Even though we only used examples related to text during the lesson, inputs do not necessarily
have to be text-based. They can also take the form of mouse clicks, drag and drops, and even
an audio clip. Unlike text, handling these forms of data is a bit more complex. Therefore, when
you have a better understanding of the programming language, you can delve deeper into these
areas too.
Outputs
Outputs can be in various shapes and forms. The most common format we will be dealing with
is text outputs, but there are many more interesting formats such as binary files, images, and
even speech. When it comes to text outputs, instead of simply displaying them on the screen,
we can format them to look prettier and make them easier to understand. For example, if we
need to print data in a table format, we can use the string formatting function available in Python
to structure how a row should look and fill in the blanks with the .format() method.
In addition to spatial formatting, we can also add decorative features such as text colours and
background colours when displaying text. You can do this manually or by using a library that is
available for Python. Since this is a bit of an advanced topic we will not delve deeper into the
area, but if you are interested you can learn more about it here. Try making a green Christmas
tree if you are up for the challenge. You can also print letters in styles like bold and italic in this
manner.
Another important feature you will need in the future is visualizing data structures. You should
have learnt about lists in a previous lesson. A list is a form of a data structure. There are many
more data structures that you will learn about in the future, such as dictionaries and tuples.
When your programs get complex, you will need to see how each data structure is formatted
and what values they hold. There is a library name pprint available in python, which can print
the details of a data structure in an understandable manner instead of printing them in a single
line. You can see some examples here, and if you are interested try playing around with it
yourself. See you in the next lesson. Happy learning!