'Newlines are represented with '\n' and show up as '
', i.e. a break in the line.'.split()
'Cats and dogs'.split('')
''.join(['Cats', 'and', 'dogs']
wishlist
, how would you remove 'pony' from the list?list
FunctionThere are times where you will want to convert something into a list. We haven't seen a lot of data structures yet, but a lot of them can be converted into a list using the list "function" (technically, it's a callable class, but it's most often used as a function). The list function takes any iterable object. Iterable objects are objects that we can do this to:
So far, we've see these iterable objects:
When we use the range
function, we get back a range object. Sometimes we want an actual list of the integers represented by the range. The example below shows how to convert a range object into a list.
Tuples are almost like lists, except they can't change their values. So once a tuple is created, you cannot add new elements to it nor can you change the value of existing elements. To create a tuple:
()
a,
or (a,)
a, b, c
or (a, b, c)
You can also convert an iterable type into a tuple using the tuple
"function":
What's the point of tuples? Why have a list-like data type that can't change its values? It can be more efficient than lists because it doesn't need to be resized (lists actually allocate more memory than they're currently using to make insertions/appending faster).
So far, all input to our programs have been through the keyboard and all output has been to the screen. This means that every time the user runs our programs, they must enter all of the data again. If there's a lot of data, this can take a long time. Additionally, if there's a lot of output, the user may be overwhelmed if it is all displayed on the screen. The solution to these problems is files.
There are different kinds of files, but two basic categories: text files and binary files. We will work with text files (binary files include sound files, picture files, zip files, and video files). There are two basic operations we can do with a file: read from a file or write to a file. We can only do one operation at a time (technically, that's not true, but things get more complicated if we try to do both). When we open a file, we must specify whether it is to read from or write to.
To write to a file, we must first open it. To open it, we use the open function:
where:
'w'
indicates to open the file for writingWith our file open, we can start writing to it. We write to it through our file_reference, using the write
method. You can only write strings to the file. Once we are finished writing to the file, we use the close
method. It is a very good idea to close a file once you are finished with it. Once a file is closed, you can no longer write to it. Here's an example:
Since we just specified the file's name, this creates a file called "writing_example.txt" in the same location that Python is being run. If you want it to be written in a different location, you need to specify that! More on that below.
The contents of the file are:
Anything sent to the write method is written to the file.
At times, you may want to write a list of strings to a file. This is possible with the writelines
method:
The contents of 'lines.txt' is:
You can use both writelines
and write
on the same file.
If you open a file for writing and that file already exists, it will erase the contents of the file! So, if we execute these statements:
"writing_example.txt" will now contain only:
If you just want to add on to the end of a file, you can open the file to append by passing 'a' instead of 'w' to open
:
"writing_example.txt" now contains:
Why does "Here we are adding new content to the file." appear on the same line as "The old content is erased!"?
Other than passing in 'a' instead of 'w' to open
, writing to a file and appending to a file are done the same way.
To read from a file, we must first open it. To open it, we again use the open function:
Notice that this time, we're passing 'r' instead of 'w'. The 'r' means "read".
Just like with writing to files, you need to close the file after you are finished reading from it. You again use the close
method.
Once our file is open, we can read from it using the read
method. This method returns a string of the contents read in. This method is most often used to read in the entire file:
You can specify the number of bytes to read in, but you would need to know how many bytes constitute the thing you want to read in. Generally, you won't know that ahead of time. However, if you want to read in a certain number of bytes, here's how to do it:
Note: One byte does not always equal one character. In English, that's the case, but in some other languages that isn't the case. This is yet another reason why it is best to not read byte-by-byte in most cases.
There are two other options for reading contents from a file: readline
and readlines
. In both cases, they read whole lines. readline
will read in just one line (from the current position in the file to the newline character) and return it as a string. readlines
will read in all of the lines, each as a string, and put them in a list.
It is very common to process each line of file, one at a time. Often, that's because each line represents one data point. To make processing each line individually, you can use a for loop with a file object:
<< Previous Notes | Daily Schedule | Next Notes >> |