Python Tutorial — Learn Python Step by Step
Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.
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+lambdaLambda 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)) # zeroLambda 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.
Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.
Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial
Python Career & Learning Guide
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 in our free Python Compiler.
Why Learn Python in 2026?
- #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
- AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
- Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
- Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.
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 |
| Best For | AI/ML, Data, Web | 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 |
Frequently Asked Questions
Do I need prior programming experience to learn Python?
No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.
Which Python version should I use?
Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.
Is this Python tutorial completely free?
Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.