Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Extend Class Method in Python
Next article icon

Define and Call Methods in a Python Class

Last Updated : 14 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, provides a straightforward syntax for defining and calling methods within a class. In this article, we will explore the concepts of methods in a class in Python and will see how to define and call methods in a class with examples and explanations.

What are Methods in a Class in Python?

A method in a class is a function that is associated with an object. It defines the behavior and actions that objects of the class can perform. Methods are essential for encapsulating functionality and promoting code reusability. They are defined inside a class and can access the attributes (variables) of the class.

Syntax of Method

class ClassName:

def method_name(self, parameter1, parameter2, ...):

# Method body - code goes here

# Creating an object of the class

object_name = ClassName()

# Calling a method on the object

object_name.method_name(argument1, argument2, ...)

Define And Call Methods In A Class In Python

Let's look at how to define and call methods in a class with the help of examples.

Example 1: Simple Class with a Method

In this example, we define a class GeeksClass with a method say_hello. The say_hello method simply prints the message "Hello, Geeks!" when called. We then create an instance of the class (geeks_object) and call the say_hello method on that instance.

Python3
class GeeksClass:     def say_hello(self):         print("Hello, Geeks!")  # Creating an object of GeeksClass geeks_object = GeeksClass()  # Calling the say_hello method geeks_object.say_hello() 

Output
Hello, Geeks!

Example 2: Class with Parameterized Method

In this example, we define a Calculator class with an add method that takes two parameters (num1 and num2) and returns their sum. We create an instance of the Calculator class (calculator_object) and call the add method with arguments 5 and 7.

Python3
class Calculator:     def add(self, num1, num2):         return num1 + num2  # Creating an object of Calculator calculator_object = Calculator()  # Calling the add method with parameters result = calculator_object.add(5, 7) print("Result of addition:", result) 

Output
Result of addition: 12

Conclusion

Defining and calling methods in a class in Python is fundamental to object-oriented programming. Methods enable us to encapsulate functionality within objects, promoting code organization and reusability. By understanding how to define and call methods, you can build powerful and modular code structures in Python. As demonstrated with GeeksforGeeks examples, these concepts are not only fundamental but also applicable in real-world coding scenarios.


Next Article
Extend Class Method in Python
author
sourabhsahu33
Improve
Article Tags :
  • Python
  • python-oop-concepts
Practice Tags :
  • python

Similar Reads

  • Call a Class Method From another Class in Python
    In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method f
    3 min read
  • Extend Class Method in Python
    In Python, class methods are functions that are bound to the class rather than the instance of the class. This means they receive the class (cls) as the first argument instead of the instance (self). Extending a class method refers to enhancing or customizing the behavior of an inherited class metho
    3 min read
  • How to Define and Call a Function in Python
    In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function. Example: [GFGTABS] Python # Defining a function def fun(): print("Welcome to GFG") # calling a fu
    3 min read
  • Call Parent class method - Python
    In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effe
    5 min read
  • Accessing Attributes and Methods in Python
    In Python, attributes and methods define an object's behavior and encapsulate data within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes
    4 min read
  • How to clone a method code in Python?
    With the help of methodName.__code__.replace() method, we can clone the code of builtin method as well as any other defined method and we can also fix the positional only arguments in any of the cloned method code by using methodName.__code__.replace() method. Syntax : methodName.__code__.replace()
    1 min read
  • Python Classes and Objects
    A class in Python is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type. Classes are created using class k
    6 min read
  • Data Classes in Python | An Introduction
    dataclass module is introduced in Python 3.7 as a utility tool to make structured classes specially for storing data. These classes hold certain properties and functions to deal specifically with the data and its representation.DataClasses in widely used Python3.6 Although the module was introduced
    3 min read
  • Accessor and Mutator methods in Python
    In Python, class is a prototype for objects which is a user-defined type. It specifies and defines objects of the same type, a class includes a cluster of data and method definitions. Moreover, an object is a single instance of a class but you can create many objects from a single class.Note: For mo
    3 min read
  • Difference between Method and Function in Python
    Here, key differences between Method and Function in Python are explained. Java is also an OOP language, but there is no concept of Function in it. But Python has both concept of Method and Function. Python Method Method is called by its name, but it is associated to an object (dependent).A method d
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences