Python includes both functions and methods in its standard library.
A function takes one or more arguments and
optionally returns a value. Some of Python's built-in functions that
we've already seen in this course are len(),
chr(), ord(),
input() and print().
To call a function, we simply write its name followed by the
arguments:
name = input('Enter your name: ')A method is like a function, but is invoked on a particular object. For example:
s = 'yoyo'
b = s.startswith('yo') # method call
In the second line above, we are invoking (or calling) the
startswith() method on the string s. We
pass the string 'yo' to the method. The
method returns a value, which is True in
this case since the string 'yoyo' does
start with 'yo'.
Above, we said that a method is invoked on an
object. In Python any value
is an object, so (for example) 3, False,
and 'yoyo'
are all objects. (In some other
languages, there is a technical distinction between objects and other
kinds of values.) Methods (and a related feature, classes, which
we'll discuss later) are fundamental building blocks in
object-oriented programming.
A language that has methods and classes, such as Python, C++, Java,
or C#, is called object-oriented.
Not all programming languages have both functions and methods. For example, C has only functions, and classic Java has only methods. Python is a bit of a hybrid since it has both functions and methods. This arguably makes the language more flexible and convenient (at the cost of some complexity).
In this course we will soon learn how to write our own functions, and before too long we'll learn how to write our own methods (and classes) as well.
Python's library contains many more useful methods
on strings. For example, .lower()
converts all characters in a string to lowercase:
>>> s = 'YUMMY PIE' >>> s.lower() 'yummy pie' >>> s 'YUMMY PIE'
Notice that the call to
lower() above
returned a new
string that was
like s, but in which all characters are lowercase. It did
not modify the
original string s, which still contains uppercase characters. In
fact, it could not
possibly modify s,
since Python strings are immutable. Many string methods are similar
to lower()
in
that they return a new string derived by modifying a given string in
some way.
Our
quick reference lists more string
methods and operators. Note that strings are iterable,
since you can loop over them with for.
They are also sequences,
since you can access string elements using the syntax s[i].
Soon we'll see other kinds of iterables and sequences. (In Python
every sequence is iterable, but some iterables such as sys.stdin
are
not sequences). So if you're looking in the quick reference for
operations that work on strings, you can find them in three places:
in the section on iterables,
in the section on sequences,
and in the section specifically about strings.
Python includes f-strings, which are formatted strings that can contain interpolated values. For example:
>>> color1 = 'blue'
>>> color2 = 'green'
>>> f'The sky is {color1} and the field is {color2}'
'The sky is blue and the field is green'
Write the character f
immediately before a string
to indicate that it is an f-string.
Interpolated values can be arbitrary expressions. For example, consider a program that reads two values and prints their sum. Without an f-string, we might write
x = int(input('Enter x: '))
y = int(input('Enter y: '))
print('The sum of', x, 'and', y, 'is', x + y)Using an f-string, we may instead write the last line like this:
print(f'The sum of {x} and {y} is {x + y}')In my opinion, this is easier to read and write.
You may optionally specify a format code after each interpolated value to indicate how it should be rendered as a string. Some common format codes include
b – an integer in binary (base 2)
d – an integer in decimal (base 10)
x – an integer in hexadecimal (base 16)
f – a floating-point number. This
format code may optionally be preceded by a period ('.')
followed by an integer precision, which indicates how many digits
should appear after
the decimal point.
For example:
>>> import math
>>> m = 127
>>> f'hex value is {m:x}'
'hex value is 7f'
>>> import math
>>> math.pi
3.141592653589793
>>> f'pi is {math.pi:.3f}'
'pi is 3.142'Notice that Python rounds (rather than truncates) a floating-point number to a given number of digits.
You can specify a comma (',')
before a d or x
format code to specify that digits should be printed in groups of 3,
with a separator between
groups:
>>> x = 2 ** 100
>>> f'{x:,d}'
'1,267,650,600,228,229,401,496,703,205,376'An integer preceding a format code indicates a field width. If the value's width in characters is less than the field width, it will be padded with spaces on the left:
>>> f'{23:9d}'
' 23'
>>> f'{723:9d}'
' 723'
>>> f'{72377645:9d}'
' 72377645'
If the field width is preceded with a 0,
then the output will be padded with zeroes instead:
>>> f'{23:09d}'
'000000023'
>>> f'{723:09d}'
'000000723'
>>> f'{72377645:09d}'
'072377645'There are many more format codes that can you can use to control output formatting in more detail. See the Python library documentation for a complete description of these.
We've already seen Python's input()
and print() functions. input()
reads a line from standard input. print()
reads a line to standard output. By default standard input and
output are the terminal, but we'll soon see that we can redirect
them from or to a file.
At some point the input may reach its end. When the input comes from a terminal, we can signal the end of the input by typing ctrl+D (on Linux or macOS) or Ctrl+Z Enter (on Windows).
Let's write a program that reads numbers from
standard input, one per line, until it ends. The program will print
the sum of the numbers. We could try to use input()
to read the numbers, however when the input ends input()
will produce an error, which is inconvenient. As another approach, we
can use the object sys.stdin, which
represents a program's standard input. We can loop over sys.stdin
using a for statement. On each
iteration, we'll receive the next input line as a string:
import sys
sum = 0
for line in sys.stdin: # for each line of standard input
n = int(line) # convert string to integer
sum += n
print('The sum is', sum)Let's run the program, and give it some numbers as input:
$ python sum.py 3 4 5 ^D
At the end we typed ^D (control D), meaning end of input. The program now prints
The sum is 12
When you run a program you may redirect its standard input to come from a file, and may also redirect standard output to a file.
Use the
'<' character
to redirect the input. For example, let's use a text editor to create
a file test.in with
these contents:
4 5 6
Now let's run the Python program from the previous section,
redirecting its input from test.in:
$ python sum.py < test.in 15
Similarly, you can use the > character to redirect a program's output. Let's run the program again, redirecting both the input and output:
$ python sum.py < test.in > test.out $
The program ran, but produced no output on the terminal since its
output was redirected. Let's look at the output it produced. You can
view it in an editor. Alternatively, the cat
command (on Linux or macOS) will display the contents of a file:
$ cat test.out 15 $
Many of our homework assignments in our ReCodEx system will contain sample input(s) and sample output(s) for the program that you're supposed to write. You may want to place each sample input in a text file. Then you can run your program with its input redirected from each file in turn. That will be much more convenient than manually entering input data each time you run your program.
Earlier,
we saw that we can loop over sys.stdin
to read lines from a file.
You should be aware that when you do this, each string you receive
will end with a
newline character.
Consider this program print.py,
which reads all lines from standard input and copies them to standard
output:
import sys
for line in sys.stdin:
print(line)
Suppose that we have a text file story.txt
with three lines:
the beginning the middle the end
Let's run the program above and redirect its input from this file:
$ python print.py < story.txt the beginning the middle the end $
Notice the extra blank lines after each output line. As
mentioned above, each string generated by the for
loop will end with a newline character. For example, the first line
read from the file will be 'the beginning\n'.
(As we saw in an earlier section, on Windows the file will actually
contain '\r\n' at the end of the line,
but Python will convert this sequence to '\n'.)
When we invoke print() on this string,
it prints the newline in the string, and then prints a second
newline because print() normally prints a newline after any
output string you give it.
If we don't want the extra lines, we can call the
strip() method to remove the newlines
returned by for. strip()
removes all whitespace at the beginning and end of a string.
Whitespace includes unprintable characters such as spaces and
newlines:
>>> ' one two three '.strip() 'one two three' >>> 'down the street\n'.strip() 'down the street'
Let's modify the program print.py above
so that it strips each line read from standard input:
importsysforline in sys.stdin:line = line.strip()print(line)
Now it won't print extra blank lines:
$ py print.py < story.txt the beginning the middle the end $
Alternatively, if want to remove only the newline character at the
end of the line but leave all other whitespace intact, then instead
of calling strip() we could call
line = line[:-1]
The open() function
opens a file. It takes two parameters: a filename plus an optional
mode string, which can contain one or more of these
characters:
'r' – open for
reading (default)
'w' – open for
writing. If the file already exists, its existing contents will be
lost!
'a' – append
to an existing file, or create a new file if the file does not exist
already
't' – text
mode (default)
'b' – binary
mode
If you don't specify a mode string, the file will be opened for reading in text mode.
When you open a file in text mode, Python will
perform universal newline translation, i.e. translate any sort of
newlines to '\n'. We will only use text
mode in this course.
open() returns a file
object. When reading, the file object is iterable: you can loop
over it with for to receive a sequence
of lines, just like with sys.stdin. When
a file is opened for writing, you may
write lines to the file by calling the write()
method on the file object
once for each output line.
When we
are finished reading or writing data, you should call close()
to close the file.
Here's a
program that opens
two files called input.txt and
output.txt.
It reads lines from the input file and writes them to the output
file, duplicating every line as it writes:
input = open('input.txt', 'r') # open for reading ('r' is default mode)
output = open('output.txt', 'w') # open for writing
for line in input:
output.write(line) # write line to output
output.write(line) # write it again
input.close()
output.close()You can read about more methods for input and output in our Python quick library reference.
with statementWhenever
you open a file, you'll want to close it, even if your program exits
unexpectedly with an error. Python has a special statement that makes
this easy. The with statement
assigns a file object (or other resource) to a variable, then runs a
block of code. When the block of code exits for any reason, the
object is automatically closed, just as if you had called close()
on the object.
Let's
rewrite the previous program using with:
with open('input.txt', 'r') as input: # open for reading ('r' is default mode)
with open('output.txt', 'w') as output: # open for writing
for line in input:
output.write(line) # write line to output
output.write(line) # write it again
It's good practice to use
with
whenever you open a file, to ensure that the file will be closed even
if the program exits with an error.
In fact with also
works with other types of objects that need to be closed, such as
network sockets. We may see some of these later in this course.
Lists are a fundamental type in Python. We can make a list by specifying a series of values surrounded by square brackets:
l = [3, 5, 9, 11, 15]
A list may contain values of various types:
l = ['horse', 789, False, -22.3]
It may contain any number of values, or may even be empty:
l = []
The len function returns the number of elements in a list:
len(['potato', 'tomato', 'tornado']) # returns 3
We can access elements of a list by index. The
first element has index 0, and the last element has index len(l)
– 1:
>>> l = [3, 5, 9, 11, 15] >>> l[0] 3 >>> l[4] 15
Just like with strings, we can use negative indices to count from the end of the list:
>>> l = [3, 5, 9, 11, 15] >>> l[-1] 15 >>> l[-2] 11
Slice syntax works with lists, just like with strings:
>>> l = [3, 5, 9, 11, 15] >>> l[1:3] [5, 9] >>> l[3:] [11, 15]
The in
operator tests whether a list contains a given value:
>>> 77 in [2, 8, 77, 3, 1] True
Note that this is a bit different than in
on strings. The in operator does not
test whether a sublist is present
in a list:
>>> [8, 77] in [2, 8, 77, 3, 1] False
Unlike strings, lists in Python are mutable. We can set values by index:
>>> l = [3, 5, 9, 11, 15] >>> l[0] = 77 >>> l[3] = 99 >>> l [77, 5, 9, 99, 15]
A list's
length may change over time. The append()
method adds a single element to a list:
>>> l = [3, 5, 9, 11, 15] >>> l.append(20) >>> l.append(30) >>> l [3, 5, 9, 11, 15, 20, 30]
We'll often use append()
to build up a list in a loop. For example, we can build a list of the
squares of all numbers from 1 to 10:
l = []
for i in range(1, 11): # 1 .. 10
l.append(i * i)
The extend()
method adds a series of elements to a list. The += operator is a
synonym for extend():
>>> l = [2, 4, 6] >>> l.extend([8, 10]) >>> l [2, 4, 6, 8, 10] >>> l += [12, 14] >>> l [2, 4, 6, 8, 10, 12, 14]
The insert()
method inserts an element into a list at a given position:
>>> l = [3, 5, 9, 11, 15] >>> l.insert(2, 88) >>> l [3, 5, 88, 9, 11, 15]
The del
operator can delete one or more elements of a list by index:
>>> l = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'grape'] >>> del l[1] >>> l ['orange', 'pear', 'banana', 'kiwi', 'grape'] >>> del l[2:4] >>> l ['orange', 'pear', 'grape']
We may even assign to a slice in a list, replacing that slice with an arbitrary sequence of values:
>>> l = [2, 4, 6, 8, 10] >>> l[1:3] [4, 6] >>> l[1:3] = [100, 200, 300] >>> l [2, 100, 200, 300, 8, 10]
The list()
function converts any sequence to a list:
>>> list('watermelon')
['w', 'a', 't', 'e', 'r', 'm', 'e', 'l', 'o', 'n']
>>> list(range(120, 130))
[120, 121, 122, 123, 124, 125, 126, 127, 128, 129]
Like strings, lists are
iterable, so you can loop over them using for.
Lists are also sequences, since you can access their elements using
the syntax l[i].
So you can find list operations in three sections in our quick
reference guide, namely the sections about iterables,
sequences,
and specifically about lists.