Built-in Functions

See the previous section of these notes for information about how to use functions. This section is mostly a guide on what parameters and returns some (not all!) built in python functions take.

“Regular” built-in functions (non-string)

The python documentation for functions can be a bit confusing. Let’s walk through an example by looking at the len() function:

len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

This documentation talks about a lot of things that we don’t know yet. That’s ok!

The important parts:

  1. len(s) – this says that this function takes 1 parameter!
  2. “Return the length (the number of items) of an object” – this says that this function has a return value that is the length of the passed in parameter.
  3. “The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).” – this says what we can pass in to this function. For us right now, we just care that it says that we can pass in a string as a parameter.

Next, we’ll provide some pared-down definitions of built-in functions for you that should be a little clearer, but you shouldn’t be afraid of consulting the official python documentation!

If you ever have a question about documentation, feel free to post on piazza or come to office hours and we will help translate.

Note that the following definitions given are tailored specifically to where we are currently at in this course.

abs(value): we’re letting you explore this one on your own!

bool(value): returns the float version of the given value.

float(value): returns the float version of the given value.

input(prompt): prints out the given prompt, then waits for the user to enter input and returns it as a string.

int(value): returns the int version of the given value.

len(string): returns the length (int) of a given string

print(variable parameters): prints out a string representation of the given parameters to standard out. print() prints a new line. print(7) prints the str() version of 7. print(string1, string2, etc) will print out all strings given separated by a space.

max(number1, [number2, number 3, etc]): returns the maximum value among the arguments given. You can use this function with any number greater than or equal to one of arguments.

min(number1, [number2, number 3, etc]): returns the minimum value among the arguments given. You can use this function with any number greater than or equal to one of arguments.

range(number1, [number2]): ??? we’re letting you explore this in Homework 2. We will tell you that the second number parameter is optional.

round(number, [places]): returns the rounded version of the given number to the given number of places. The places parameter is optional. Both round(number) and round(number, places) work.

type(value): returns the data type of the given value.

# TODO: experiment with these functions!

Built-in string functions

Here is the python documentation for built-in string functions. We’ve provided some paired down descriptions below. Remember that the syntax for using string functions is slightly different than for our regular built-in functions.

str.capitalize(): return a copy of the string with its first character capitalized and the rest lowercased.

str.islower(): return True if the string is all lower case and False otherwise.

str.isupper(): return True if the string is all upper case and False otherwise.

str.lower(): return a lower case version of the string

str.upper(): return an upper case version of the string

str.count(value): return the number of times value occurs in str

str.replace(value1, value2): return the str with all occurrences of value1 replaced with value2

# TODO: experiment with the string functions!