Python "built-in" Functions
Function calls
In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can "call" the function by name. We have already seen one example of a function call:
The name of the function is type
. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.
It is common to say that a function "takes'' an argument and "returns'' a result. The result is called the return value.
Type conversion functions
Python provides built-in functions that convert values from one type to another. The int
function takes any value and converts it to an integer, if it can, or complains otherwise:
int
can convert floating-point values to integers, but it doesn't round off; it chops off the fraction part:
float
converts integers and strings to floating-point numbers:
Finally, str
converts its argument to a string:
We know that the values returned are strings due to the single quotes around the numbers.
Common built-in functions
Python provides many useful function for common programming tasks. We have already seen one, the print} function. A subset of the built-in function is given in the table below:
abs(x)
Returns the absolute value for x
.
abs(-2)
is 2.
max(x1, x2,...)
Returns the largest among x1
, x2
, ...
max(1, 5, 2)
is 5.
min(x1, x2,...)
Returns the smallest among x1
, x2
, ...
min(1, 5, -2)
is -2.
pow(a, b)
pow(2, 3)
is 8.
round(x)
Returns an integer nearest to x
. If x
is equally close to two integers, the even one is returned.
round(5.4)
is 5,
round(3.5)
is 4,
round(4.5)
is 4.
Keyboard input
The programs we have written so far are a bit rude in the sense that they accept no input from the user. They just do the same thing every time.
Python provides a built-in function called input
that gets input from the keyboard. When this function is called, the program stops and waits for the user to type something. When the user presses the "Return" or "Enter" key, the program resumes and input
returns what the user typed as a string.
Before getting input from the user, it is a good idea to print a prompt telling the user what to input. The function input
can take a prompt as an argument:
The sequence '\n'
at the end of the prompt represents a newline, which is a special character that causes a line break. That's why the user's input appears below the prompt.
If you expect the user to type an integer, you can try to convert the return value to am int
:
But if the user types something other than a string of digits, you get an error:
We will see how to handle this kind of error later.
Last updated