Learn Python Programming
Start with getting started, installation, and core basics. Clear explanations and practical examples to help you learn faster.
Python Lambda/Anonymous Function
A lambda is a small anonymous function defined in a single expression. Lambdas are useful for short operations passed as arguments to other functions.
Lambda Syntax
# Syntax: lambda parameters: expression
# Simple lambda
add = lambda a, b: a + b
print(add(3, 5)) # 8
# Equivalent regular function
def add(a, b):
return a + b
# Lambda with one parameter
double = lambda x: x * 2
square = lambda x: x ** 2
is_even = lambda x: x % 2 == 0
print(double(7)) # 14
print(square(4)) # 16
print(is_even(6)) # True
# Lambda with no parameters
greet = lambda: "Hello, World!"
print(greet()) # Hello, World!
Common Use: Sorting
# sorted() and .sort() accept a key function
students = [
{"name": "Charlie", "grade": 85},
{"name": "Alice", "grade": 92},
{"name": "Bob", "grade": 78},
]
# Sort by grade
by_grade = sorted(students, key=lambda s: s["grade"])
print([s["name"] for s in by_grade]) # ["Bob", "Charlie", "Alice"]
# Sort by grade descending
by_grade_desc = sorted(students, key=lambda s: s["grade"], reverse=True)
# Sort by name length
words = ["banana", "pie", "apple", "kiwi"]
print(sorted(words, key=lambda w: len(w)))
# ["pie", "kiwi", "apple", "banana"]
# Sort by multiple criteria
people = [("Alice", 30), ("Bob", 25), ("Alice", 25)]
sorted_people = sorted(people, key=lambda p: (p[0], p[1]))
# [("Alice", 25), ("Alice", 30), ("Bob", 25)]
Common Use: map(), filter(), reduce()
# map() — apply function to every item
numbers = [1, 2, 3, 4, 5]
doubled = list(map(lambda x: x * 2, numbers))
# [2, 4, 6, 8, 10]
# filter() — keep items where function returns True
evens = list(filter(lambda x: x % 2 == 0, numbers))
# [2, 4]
# reduce() — accumulate values
from functools import reduce
total = reduce(lambda acc, x: acc + x, numbers)
# 15 (1+2+3+4+5)
product = reduce(lambda acc, x: acc * x, numbers)
# 120 (1*2*3*4*5)
# Note: list comprehensions are often clearer
doubled = [x * 2 for x in numbers] # prefer over map+lambda
evens = [x for x in numbers if x % 2 == 0] # prefer over filter+lambda
Lambda in Real-World Patterns
# As callback / event handler
buttons = {
"save": lambda: print("Saving..."),
"load": lambda: print("Loading..."),
"quit": lambda: print("Goodbye!"),
}
buttons["save"]() # Saving...
# As a quick key extractor
max_student = max(students, key=lambda s: s["grade"])
print(max_student["name"]) # Alice
# In dictionary default values
from collections import defaultdict
word_count = defaultdict(lambda: 0)
for word in "hello world hello".split():
word_count[word] += 1
# Conditional expression in lambda
classify = lambda x: "positive" if x > 0 else "negative" if x < 0 else "zero"
print(classify(5)) # positive
print(classify(-3)) # negative
print(classify(0)) # zero
Lambda Limitations
# Lambdas are limited to a single expression — no statements
# These are NOT allowed in lambda:
# - assignments (x = 5)
# - multiple lines
# - if/elif/else statements (only ternary expressions)
# - try/except
# - loops
# If you need any of these, use a regular function
# BAD: trying to do too much in a lambda
# process = lambda x: (x.strip(), x.lower(), x.replace(" ", "_"))
# GOOD: use a named function
def process(x):
x = x.strip()
x = x.lower()
return x.replace(" ", "_")
# RULE OF THUMB:
# - Lambda: simple, single expression, used once inline
# - def: anything complex, reused, or needing a docstring
- Lambda creates a one-expression anonymous function:
lambda args: expression. - Most common use:
key=argument insorted(),max(),min(). - Prefer list comprehensions over
map(lambda ...)for readability. - Lambdas cannot contain statements, assignments, or multiple expressions.
- If a lambda is complex or reused, refactor it into a named
deffunction.
Frequently Asked Questions
Answers to common Python getting-started questions
Python Programming Tutorial — Learn Python from Scratch
Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples. You can try every example in our free Python Compiler without installing anything.
Each topic builds on the previous one, starting from installation and Hello World through advanced concepts like decorators, generators, and file I/O. Whether you are a complete beginner or refreshing specific skills, every page gives you immediately usable code.
What This Tutorial Covers
- Getting Started: Install Python, run online, Hello World
- Basics: Variables, data types, type conversion, input/output
- Operators: Arithmetic, comparison, logical, assignment
- Control Flow: if/elif/else, for loops, while, break/continue
- Data Structures: Lists, tuples, sets, dictionaries
- Strings: Methods, slicing, formatting, f-strings
- Functions: Parameters, return values, *args, **kwargs, scope
- OOP: Classes, objects, inheritance, polymorphism
- File I/O: Reading, writing, CSV, JSON handling
- Exceptions: try/except, custom exceptions, raise
- Advanced: List comprehensions, lambda, generators, decorators
- Modules: import, pip, packages, __name__ == "__main__"
Why Learn Python in 2026?
- #1 most popular language: Ranked first on TIOBE, Stack Overflow, and GitHub for multiple years running.
- AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn), data analysis (Pandas, NumPy), and AI development.
- Web development: Django and Flask power backends at companies like Instagram, Spotify, and Pinterest.
- Automation: Automate files, emails, web scraping, reports, and system administration tasks in minutes.
- Beginner-friendly: Clean syntax with enforced indentation makes code readable from day one — no curly braces or semicolons.
- Massive job market: Python developers are in high demand across tech, finance, healthcare, and research.
Python vs Other Languages
| Feature | Python | Java | JavaScript | C++ |
|---|---|---|---|---|
| Syntax | Very clean, readable | Verbose | Moderate | Complex |
| Typing | Dynamic, strong | Static, strong | Dynamic, weak | Static, strong |
| Speed | Slower (interpreted) | Fast (JIT) | Fast (V8 JIT) | Fastest (native) |
| Best For | AI/ML, data, automation | Enterprise, Android | Web frontend/backend | Systems, games |
| Learning Time | 2–4 weeks basics | 4–6 weeks basics | 3–4 weeks basics | 8–12 weeks basics |
How to Get Started
- Run Python online: Use our free Python Compiler — no installation needed.
- Install locally: Download Python 3 from
python.org(Windows/Mac) or useapt install python3(Linux). - Verify: Run
python3 --versionin your terminal to confirm installation. - Choose an editor: VS Code with Python extension (free), PyCharm Community (free), or Jupyter Notebook for data science.
- Follow this tutorial in order: Start from Introduction and work through each topic sequentially.
Frequently Asked Questions
No. Python is designed to be beginner-friendly. This tutorial starts from absolute zero and builds up gradually.
Python 3.10+ is recommended. Python 2 reached end-of-life in 2020. All examples in this tutorial use Python 3 syntax.
Basics (syntax, loops, functions) take 2–4 weeks. Intermediate (OOP, file I/O, modules) adds 3–4 weeks. Specialisation (Django, data science, ML) takes another 2–3 months.
Yes, completely free. No account, no sign-up. All topics and examples available without restriction.
Who Is This For?
Complete beginners choosing their first programming language. Students in CS courses needing a Python reference. Data analysts transitioning from Excel to Python (Pandas). Self-taught developers adding Python to their skill set. Professionals automating repetitive tasks. Anyone preparing for Python coding interviews.