Python Functions

Python · Functions & OOP

Theory

A function is a named, reusable block of code. You define it once with the def keyword, give it parameters (the inputs it expects), and call it as many times as you like with different arguments (the actual values). Inside the body, return sends a value back to the caller and ends the function immediately; a function with no return gives back None.

For example, total_sales(sales) receives a list, adds it up with sum() and returns the total, so total_sales([100, 250, 300]) evaluates to 650. Parameters can have default values (def greet(name, msg='Hi')), which makes those arguments optional, and callers may pass arguments by position or by name (greet(msg='Hello', name='Asha')).

Why we use it

Without functions you copy and paste the same logic everywhere, and every bug has to be fixed in many places. A function gives that logic one name and one home, making code shorter, easier to test and easier to read.

When to use it

Write a function as soon as you repeat a piece of logic twice, or when a block does one clear job (clean a column, compute tax). For a tiny one-off expression passed to sorted() or map(), a lambda is often enough.

How it works

  1. Caller runs total_sales([100, 250, 300])
  2. Python binds the argument to the parameter sales
  3. Execute the body line by line
  4. Is a return statement reached? Yes: Send the value back and exit. No: Return None at the end.
  5. Caller receives 650 and continues

Syntax

def function_name(params):
    return value

Example

def total_sales(sales):
    return sum(sales)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic