INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
THEORY
PAGE 1 / 1
Lists - Useful Functions/Methods in Python
Home >>> Online Lessons, Python 3

Some Useful Functions/Methods

It's time to clarify our terminology. There is a distinction between a function and a method, even though both represent subroutines—sequences of code that perform specific tasks. A method is a function that belongs to a class or data type, etc.

For example, len() is a built-in function that we can call directly to determine the length of a string, a list, etc.:

len(list)

Methods of the list type can be found [here] in the official documentation of Python 3. We will only present some that are particularly important for now. Associated methods are used as shown below:

list.method_name(parameters)

where list is the name of the variable, and method_name is the name of the method.

EXAMPLES

We start with a list that contains some integers. Execute the following program:
Editor - lesson_11_lists.py
       
Console/Output done
DETAILS

The function sum(numeric_list, initial) returns the sum of the values of the elements in a list that contains only numeric values; optionally, if initial is specified, the sum is added to this value.

The functions min() and max() return the smallest and, respectively, the largest value held by the elements of the list.

The method count(element) returns the number of occurrences of the element sent as a parameter in the list.

Limitations of the Online System

This web page contains a simulation of working in Python, and some methods may not work online, but they are documented and can be executed on your computer after you [install the Python environment].

Once you have installed the Python environment on your PC, try:

• The sort() method sorts the elements of a list in ascending order:



We can also sort a list in descending order by calling the function as shown below:



• The reverse() method reverses the elements of the list, whether they are sorted or not:



Enough with the theory. Let's apply what you've learned!
 home   list  CONTENTS   perm_identity   arrow_upward