You'd be familiar with functions in mathematics, like f(x). A function in Python is similar: You use the function by giving the function name followed by some arguments listed in parentheses, and the function computes a result called the return value.
smaller = min(x, y)
In this example, we pass the current values of x
and
y
as our arguments to the function named min
.
As it happens, min
returns the minimum value among its arguments,
which this example saves into its smaller
variable.
You can use functions wherever you would like within an
expression. The following example uses max
to compute
the largest among three different values and min
to
compute the smallest; the difference between the results is
saved into the range
variable.
range = max(x, y, z) - min(x, y, z)
For now, I'd like to highlight four numeric functions.
code result abs(x)
the absolute value of x
max(x, y)
the maximum of x
andy
min(x, y)
the minimum of x
andy
round(x)
the result of rounding x
to the nearest integer
In addition to numeric values, Python programs can also deal with strings — character sequences like words or sentences. These will prove important as we write programs that interact with the user (reading and displying text) and that process files (which often store data as character sequences).
Creating a particular string is easy: Just enclose it in quotes.
name = 'Python'
You can use either double quotes or single quotes, as long as both sides match. Most use single quotes unless the string has single quotes in it.
sentence = "I'm catching on"
The quotes are necessary, though. If you
type “name = Python
”, the computer will
assume that you mean to copy value of the variable Python
into name
. To mention the specific
character sequence
P-y-t-h-o-n,
you need to enclose it in quotes.
To start working with strings, here are four things you can do to them:
You can use the len
function to compute how many
characters are in the string (counting spaces!). So
len(word)
would be 6 if word
has
“Python”.
You can use +
to append strings together.
maybe = sentence + 'to ' + name
In this case, we're taking the sentence
variable,
which we've already set to “I'm catching on” and
appending the string “to ” and the value of
name
, which is “Python”. The end result is
the string “I'm catching onto Python”, which is
assigned to maybe
. (The variables sentence
and
word
don't change.)
repeats = 4 * word
If word
referred to the string “Python”,
repeat
would be assigned to be four copies of that
appended together,
“PythonPythonPythonPython”.
Finally, you can use brackets to pull out a portion of
the string. We identify which portion using numbers, counting
the letters from 0, so if word
is “Python”,
word[0]
will be the character “P”
and word[5]
will be “n”.
We can also count backwards from the end:
word[-1]
will be “n”
while word[-2]
will be “o”.
What's more, we can pull out a sequence of several characters by
listing the start and end indices separated by a colon;
the end index is excluded from the result.
Thus, word[2:4]
would give me characters 2 and 3 from
word
: “th”, and
word[4:6]
would give “on”.
Finally, you can include the colon but omit the start or end
index; with the start index omitted, Python will assume you mean
to start from the beginning, and with the end index omitted,
Python will assume you mean to end at the string's end.
Thus, word[:2]
yields “Py”
while word[3:]
yields “hon”.