🚀 Python Concept Simplified: Function vs Method One of the most common confusions beginners have in Python is the difference between a Function and a Method. Let’s end that confusion once and forever 👇 🔹 FUNCTION A function is an independent block of code. It is written outside a class and can be called directly using its name. ➡️ Think of it as a free worker — works independently. def add(a,b): return a+b add(5,7) # function call 🔹 METHOD A method is a function that belongs to a class. It must be called using an object. ➡️ Think of it as a worker inside a company (class) — works only through that company’s object. class Calculator: def add(self,a,b): return a+b c = Calculator() c.add(5,7) # method call 🧠 Shortcut to remember If called like this It is name() Function object.name() Method One Line Summary Every method is a function, but not every function is a method.
Alok Agarwal’s Post
More Relevant Posts
-
Tim Peters, a Python programmer, wrote this now-famous “poem” of guiding principles for coding in Python: The Zen of Python Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one—and preferably only one—obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
To view or add a comment, sign in
-
Python Starters Daily Nugget Tuples: When You Don't Want Change Tuples are like lists, but immutable. Once created, you can't alter them. point = (1, 4) They are great for coordinates, constant or return values from functions. Follow the Python 🐍 Starters Hub: WhatsApp: https://lnkd.in/dbjAFv52 LinkedIn: https://lnkd.in/dkJE3tZq
To view or add a comment, sign in
-
🐍 Python 3.14 Finally Gets Native Max Heap Support! For years, Python developers working with heaps had to resort to workarounds – negating values, using custom comparators, or implementing max heaps from scratch. Not anymore!! Python 3.14 introduces three new functions to the heapq module: heapify_max() - Convert a list into a max heap heappop_max() - Pop and return the largest element heappush_max() - Push an element onto the max heap While I'm excited about this addition, I have to admit the old "negate your values" workaround helped me truly understand how min heaps work under the hood instead of blindly calling functions. Sometimes constraints breed deeper understanding. That said, production code wins here. The new API is clearer and less error-prone for teams.
To view or add a comment, sign in
-
Walrus Operator (:=) introduced in Python 3.8 — a powerful feature that lets you assign values within expressions. We’ll explore how it makes your code cleaner, shorter, and more efficient with simple, real-world examples. Perfect for both beginners and intermediate Python developers! Subscribe to my YouTube Channel:- https://lnkd.in/gWsRRdej Follow my Code Camp Academy LinkedIn Page:- https://lnkd.in/gb7-8pKF #python3 #pythonprogramming #pythontutorial #pythonforbeginners #pythonbeginner #visualcode #visualstudio #visualstudiocode #datatypes #datatypesinpython #array #pythonlist
To view or add a comment, sign in
-
Walrus Operator (:=) introduced in Python 3.8 — a powerful feature that lets you assign values within expressions. We’ll explore how it makes your code cleaner, shorter, and more efficient with simple, real-world examples. Perfect for both beginners and intermediate Python developers! Subscribe to my YouTube Channel:- https://lnkd.in/gWsRRdej Follow my Code Camp Academy LinkedIn Page:- https://lnkd.in/gb7-8pKF #python3 #pythonprogramming #pythontutorial #pythonforbeginners #pythonbeginner #visualcode #visualstudio #visualstudiocode #datatypes #datatypesinpython #array #pythonlist
To view or add a comment, sign in
-
A quick Python refresher - 👇 def test_value(a): if (a): print("✅ TRUE") else: print("❌ FALSE") test_value(5) # ✅ test_value(0) # ❌ test_value("Hi") # ✅ test_value("") # ❌ test_value([]) # ❌ test_value({}) # ❌ test_value(None) # ❌ In Python, it’s not just about True or False — it’s about truthiness. Empty values like 0, "", [], {}, and None are Falsy. Everything else is Truthy. 💡 Pro tip: Always include an else (or even better, elif) — it keeps your logic clear and helps catch unexpected falsy cases. #PythonTips #CodeQuality #CleanCode
To view or add a comment, sign in
-
🔹 What is return in Python? In Python, the keyword return is used inside a function to send a value back to the place where the function was called. When a function executes a return statement: It stops running immediately It sends the value written after return back to the caller 🔹 Example: def add(a, b): return a + b result = add(5, 3) print(result) # Output: 8 ✅ The return keyword sends back the result of a + b. If you don’t use return, Python automatically returns None. 🔹 In short: Situation What happens return value Function gives that value back return (nothing) Function ends, returns None No return Function ends, returns None automatically 💡 Tip: return is used to give a result back from a function, while print() is used only to display output on the screen. #Python #DataScience #LearningPython #CodingJourney #Abdurrahman
To view or add a comment, sign in
-
Today I learned one of the most important topics in Python — Functions. A function is a block of reusable code that performs a specific task. It helps make programs organized, efficient, and easy to debug. - Why Functions Are Important: 🔹 They reduce code repetition 🔹 They make code easier to maintain 🔹 They improve clarity and structure - Keywords to Remember: 🔹 def → defines a function 🔹 return → sends output from a function 🔹 Parameters → inputs passed to a function Understanding how to structure logic into reusable blocks is a big step toward writing cleaner and smarter code. Simple Structure Example def function_name(parameters): # Block of code return result Explanation of Each Part: 🔹 def -> Keyword used to define a function 🔹 function_name -> Name of your function (we decide it) 🔹 parameters -> Inputs the function can take (optional) 🔹 : -> Indicates the start of the function block 🔹 return -> Sends a result back (optional) 🔹 Indented code block -> Code that runs when the function is called #Python #DataAnalytics #LearningJourney #Upskilling #CareerGrowth
To view or add a comment, sign in
-
"Understanding the pivotal role that wheels play in the Python ecosystem can make your life easier as both a user and developer of Python packages. Furthermore, increasing your Python literacy when it comes to wheels will help you to better understand what’s happening when you install a package and when, in increasingly rare cases, that operation goes awry." by Real Python #python #packages #codereuse #librarydevelopment https://lnkd.in/dWGiFhQF
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development