Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Functions: Input and Output

Give Feedback

00:01 Input and Output.

00:05 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() and open(). 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 Hello World!.

00:26 But there are other values which we can use with print(), as you can see here in bpython. And one of them in particular can be really useful, which is end.

00:37 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.

01:01 So, all this program is going to do is print out the numbers from 0 to 99. We have the range() constructor here, which is just going to print those out.

01:13 Seeing that in action, you can see it running

01:17 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.

01:55 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.

02:12 This can make it much easier to deal with.

02:16 Next up, open(). Here, 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 open() function.

02:42 It takes two values, the name of the file and the mode in which it will be opened—in this case, write ('w'). Here we have 'test.txt' and that’s going to be opened in the write mode.

02:58 Once this has been executed, we have a file object that we can now access, and one of the methods that it supports is .write().

03:07 We’re going to write just some text, as seen there. We’re going to write 'Hello World!' and you’ll see there’s a newline entered at the end with '\n'.

03:19 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.

03:35 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.

04:05 To make the code difference clear, I’m going to comment out the original writing part of the program and see how it’s opened in read mode ('r').

04:15 You can see that the open() line is very similar with open('test.txt', 'r'). And this time, the file object gives us the .readlines() method, which reads all the lines of the file as a list.

04:33 And then we’ll ensure the file is closed. So this time, once it’s run, you can see that we have a list of the text which is present, including the newline character.

04:46 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.