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
y as our arguments to the function named
As it happens,
min returns the minimum value among its arguments,
which this example saves into its
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
compute the smallest; the difference between the results is
saved into the
range = max(x, y, z) - min(x, y, z)
For now, I'd like to highlight four numeric functions.
the absolute value of
the maximum of
the minimum of
the result of rounding
xto 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
name = Python”, the computer will
assume that you mean to copy value of the variable
name. To mention the specific
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
You can use
+ to append strings together.
maybe = sentence + 'to ' + name
In this case, we're taking the
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
maybe. (The variables
word don't change.)
repeats = 4 * word
word referred to the string “Python”,
repeat would be assigned to be four copies of that
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 will be the character “P”
word will be “n”.
We can also count backwards from the end:
word[-1] will be “n”
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.
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.
word[:2] yields “Py”
word[3:] yields “hon”.