Welcome to Lesson 18! In this lesson, we'll explore Pythonic idioms, which are specific coding styles and patterns that are considered more elegant and readable in Python.
Python offers list comprehensions, a concise way to create lists. They are often preferred over traditional loops when constructing lists.
Example 1: List Comprehension
# Using a for loop
squares = []
for i in range(1, 6):
squares.append(i ** 2)
# Using a list comprehension
squares_comp = [i ** 2 for i in range(1, 6)]When looping through a list, you can use Python's for item in list syntax directly, instead of using an index-based approach.
Example 2: Pythonic Looping
fruits = ["apple", "banana", "cherry"]
# Non-Pythonic approach
for i in range(len(fruits)):
print(fruits[i])
# Pythonic approach
for fruit in fruits:
print(fruit)Python's enumerate() function provides a more elegant way to loop through a list while keeping track of the index.
Example 3: Using enumerate()
fruits = ["apple", "banana", "cherry"]
# Non-Pythonic approach
for i in range(len(fruits)):
print(i, fruits[i])
# Pythonic approach
for index, fruit in enumerate(fruits):
print(index, fruit)Python allows you to unpack elements from iterables like lists or tuples into individual variables.
Example 4: Unpacking
# Unpacking a list
a, b, c = [1, 2, 3]
print(a, b, c) # Output: 1 2 3
# Unpacking a tuple
x, y = (10, 20)
print(x, y) # Output: 10 20Python allows you to assign multiple variables in a single line.
Example 5: Multiple Assignments
x = y = z = 0- Pythonic idioms focus on brevity and readability, often using expressive one-liners.
- Python's list comprehensions, enumerate, and unpacking offer more concise alternatives to common tasks compared to traditional loops and indexing.
Now that you've learned about Pythonic idioms, you can write more elegant and concise Python code following the community's best practices.
- PEP 8 - Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/
- Rewrite some of your previous Python code using Pythonic idioms, such as list comprehensions and unpacking. Compare the new code with the original and observe the differences in readability and conciseness.