Python Basics — Methods

Mehdi Lotfinejad
3 min readNov 10, 2022

Python is an Object-Oriented Programming (OOP) language, which means almost everything is an object in Python. Basically, an object is a collection of attributes (variables) and methods that act on the attributes. In this lesson, I am not going to open the object-oriented programming discussion up here; we will learn more about OOP in the following lessons. What I am going to talk about in this lesson is all about objects’ methods.

As I mentioned, technically, everything is treated as an object in Python, including variable, function, list, tuple, dictionary, set, etc. All data types, variables, or data structures are Python objects. A string is an object, A floating-point number is an object, and a list is an object. These objects have a specific type that you already know: str, float, and list, and of course, they represent the values we assign to them, such as “Mehdi”, 10.37, and the shirt size list.

Go ahead and run the cell. Here, I declared three variables with different data types and then printed their data types out. As can be seen, each of these variables belongs to a specific object class.

But in addition to this, Python objects come with many methods. You can think of methods as functions that belong to Python objects. A string object has methods, such as capitalize and replace, similarly a floating-point object or a list has specific methods. Let’s try to discover some of them in more detail.

Go ahead and run the cell. The upper() method belongs to string objects, converts all lowercase characters into uppercase characters, and returns it, so the first print statement outputs her_name as a string variable in uppercase letters. The count() method returns the number of occurrences of a substring in the given string. So, the second print statement returns the number of 'a's on the her_name variable.

The following list shows the average rainfall (cm) in Kuala Lumpur, Malaysia, in a year.

KL_rainfall = [17.0,16.5,24.0,26.0,20.5,12.5,12.5,15.5,19.5,25.5,29.0,24.5]

Each item in the list represents the average rainfall in a specific month from January to December. We want to use the index() and reverse() methods in the following example. First, let’s see what these two methods return and then write some code.

  • index(), that returns the index of the first element of a list that matches its input.
  • reverse(), that reverses the order of the elements in the list it is called on.

Go ahead and run the cell. It exactly returns what we expected from these two methods. Let’s see the output.

So, as you’ve seen, depending on the type of Python object, you are able to use different methods, and they behave differently.

Alright, I hope you enjoyed this lesson. In the next lesson, we will discuss Python packages.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mehdi Lotfinejad
Mehdi Lotfinejad

No responses yet

Write a response