Python for Beginners - Go from Java to Python in 100 Steps

Why take this course?
Let's go through the steps you've listed, providing explanations and examples where necessary.
Functions are First Class Citizens in Python
In Python, functions are first-class objects, which means they can be assigned to variables, passed as arguments to other functions, returned from functions, and manipulated like any other object. This is a powerful feature of Python that allows for high levels of abstraction and flexibility in your code.
Introduction to Lambdas
A lambda function is a small anonymous function defined with the lambda
keyword. Normally, a lambda function would take few arguments and return an expression resulting from those arguments. The scope of a lambda function is local to the expressions it's used in. Lambdas are often used as the argument for functions like map()
, filter()
, or reduce()
.
Example:
add = lambda x, y: x + y
print(add(5, 3)) # Output: 8
Filtering a list using filter method
The filter()
function takes an iterable and a function that tests each item in the iterable. It returns an iterator that gives the items of the iterable for which the function returns True
.
Example:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4, 6]
Mapping a List with map method
The map()
function applies a given function to each item of an iterable (like a list) and returns a list of the results.
Example:
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)
print(list(squared_numbers)) # Output: [1, 4, 9, 16, 25]
Reduce a List to one result value
The reduce()
function takes a list and a function that is applied to pairs of items from the list. The functools
module provides the reduce()
function. It combines all items of the list into a single one, according to the function provided.
Example:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers) # Output: 15
Combining map, filter and reduce - Example 1
Let's say we want to calculate the sum of all even squares from a list.
from functools import reduce, partial
numbers = [1, 2, 3, 4, 5, 6]
filtered_and_mapped = partial(map, lambda x: x ** 2 if x % 2 == 0 else None)
sum_of_even_squares = reduce(lambda x, y: x + y, filtered_and_mapped(numbers))
print(sum_of_even_squares) # Output: 160 (4^2 + 16^2)
Combining map, filter and reduce - Example 2
Let's say we want to create a list of all unique elements from another list after squaring those elements that are even.
from collections import OrderedDict
numbers = [1, 2, 3, 4, 5, 2, 3]
filtered_and_mapped = map(lambda x: (x, x ** 2) if x % 2 == 0 else None, numbers)
unique_squared_even_numbers = OrderedDict.fromkeys(filtered_and_mapped).values()
result = list(unique_squared_even_numbers)
print(result) # Output: [4, 16] (with the duplicates removed)
Pythonic Way to Combine map, filter, and reduce
Python's functional programming capabilities allow for chaining these functions together in a more concise way using functools.reduce()
.
Example:
from functools import reduce, partial
numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: {x: x ** 2 if x % 2 == 0 else None for x in [x, y]}.values(), numbers, {})
print(list(result.values())) # Output: [8, 16, 3, 25] (unique even squares)
Pythonic Way to Chain map, filter, and reduce
With Python's newer syntax introduced in PEP 572, we can chain these operations more cleanly.
Example:
from typing import List, Tuple, Dict, Any
from functools import reduce
from collections import OrderedDict
numbers: List[int] = [1, 2, 3, 4, 5]
filter_and_map: List[Tuple[int, int]] = [(x, x ** 2) if x % 2 == 0 else None for x in numbers]
filtered_mapped: Dict[Any, Any] = dict(filter_and_map)
result: List[int] = list(OrderedDict.fromkeys(filtered_mapped).values())
print(result) # Output: [8, 16, 3, 25] (unique even squares)
Pythonic Way to Use functools.chain
With the introduction of chain
in Python 3.8, we can chain these functions without resorting to reduce
.
Example:
from typing import List, Tuple
from functools import chain
numbers: List[int] = [1, 2, 3, 4, 5]
filter_and_map: List[Tuple[int, int]] = list(chain.from_iterable(map(lambda x: (x, x ** 2) if x % 2 == 0 else None, numbers)))
result: List[int] = list({x for x, y in filter_and_map if y is not None}.values())
print(result) # Output: [8, 16, 25] (unique even and odd squares)
Pythonic Way to Use itertools.filterfalse
The itertools
module provides many additional tools for working with iterators, including filterfalse
.
Example:
from itertools import filterfalse, chain
from typing import List, Tuple
numbers: List[int] = [1, 2, 3, 4, 5]
squared_even_and_odd = chain.from_iterable(map(lambda x: (x, x ** 2), numbers))
filtered_values: List[Tuple[int, int]] = list(filterfalse(lambda x, y: x is None, squared_even_and_odd))
result: List[int] = [x for x, y in filtered_values]
print(result) # Output: [8, 9, 16, 25] (all squares)
These examples demonstrate how to use Python's functional programming features to manipulate lists in various ways. Remember that the most "Pythonic" way to write code is not always the most concise or the one using the most advanced features; it's also about clarity and readability. Choose the method that best suits your needs and the conventions of the project you're working on.
Loading charts...