Input and output (IO) is a major part of any programming language - being able to receive inputs and produce outputs are what makes a program interactive.
Output
Output in Python is normally achieved using the print
keyword.
Python
print ("Hello world!")
The print
method also supports multiple values which
it will print in one string separated by a single space:
Python
print ("Hello", "world!")
Input
Input in Python varies based on the version. Version 3.x introduced the input
method of getting an input from the user whereas previous versions relied on
the raw_input
method:
Python
# Prior to Python 3.x print (raw_input("Please insert your name")) # Python 3.x and later print (str(input("Please insert your name")))
Notice in Python 3.x the use of the str
method to convert the input to
an appropriate format.
Whilst brackets are not essential with the
print
method, they are recommended in most cases. The only time that the brackets are
not necessary is when there is only a single Python expression following it.
Did you know my courses are all printer-friendly? This means you can easily export them to PDF or print them for later use.