Functions: Input and Output
In this section, you’re going to be taking a look at features which are classified under input and output, and particularly in this case, it will be
print() is probably the first thing you came across ever, which you can see here with
print() the archetypal
"Hello World!", and obviously that returns
You can see
end there, and it defaults to the newline character (
'\n'). This is going to be shown to you in the context of a program to see how useful it can be when printing out multiple values to the command line and you don’t want them on separate lines. Flicking over to code, you’re going to see a program which is a toy example, but will show you how useful this can be.
and we get those numbers. You can see we’ve got a hundred lines printed out, and it can be difficult to see everything which is happening onscreen at once. If you don’t want that, you can make use of the
end variable. So, we can set this. As default,
end is the newline character we saw earlier on in the course. Instead of that, let’s change it just to be a space (
01:43 Now when it’s run, you can see that all of the numbers are available onscreen at the same time, even to the point where we don’t get a newline at the end of the print, and our prompt is on the same line as that last print.
You can see that can be fixed with just a blank
print() statement and running it again. You can clearly see where the program stops, and this can be really useful for dealing with programs where you have a lot of printout which comes onto the screen at the same time, and you may end up scrolling from hundreds of lines.
open() is going to be used in a fairly simplistic way. This is not what you’d do typically, because normally you would use a thing called a context manager, as you will see later on. But here, the example will make sure the file is closed by explicitly closing the file object. Here, we’ll start with the
file object being defined using the
And finally, you should close the
file object, in this case with the
.close() method. As mentioned previously, typically you would use a context manager, but this is going to ensure the file is manually closed in this simple example.
Here you can see the program running. There’s no direct output, but looking at a list of the directory you can see
test.txt exists. And if I use
more to examine the contents of
test.txt, you can see the text
Hello World! has been created. Now clearly, this simple example could be expanded by adding more text, but you can see the basics of how
open() works to open a file on the computer’s hard drive. The other way you’re going to see here is in read mode.
And that’s the very basics of reading using
open(). If you want to look further into this, Real Python has a course on reading and writing files which will show you much more about how
open() works, the different modes available to it, using context managers, and how to deal with the contents of different types of files.
Become a Member to join the conversation.