- Hartl, Michael: Learn enough Python to be dangerous, Humble Bundle Pearson, 2023
Source Code | GitHub
- Index | docs
- Built-in Functions | docs
- Built-in Types | docs
- 5. Data Structures | docs
- 8. Compound statements | docs
- see hartl p.2
- see hartl p.6
["ant", "bear", "spider"] # Literal list
("ant", "bear", "spider") # Literal tuple
{1, 2, 3, 3, 4, 1} # Literal set
{"stop": "red", "wait": "yellow", "go": "green"} # Literal dictionary
[] # empty list
() # empty tuple
{} # empty dictionary
set() # empty set
states = ["Kansas", "North Dakota", "Nebraska", "South Dakota"]
# List comprehension ➡️ ['north-dakota', 'south-dakota']
["-".join(state.lower().split()) for state in states if len(state.split()) > 1]
# Dictionary comprehension ➡️ {6: 'Kansas', 8: 'Nebraska'}
{len(state): state for state in states if len(state.split()) == 1}
", ".join(
... # Generator comprehension ➡️ 'kansas, nebraska'
... state.lower() for state in states if len(state.split()) == 1
... )
>>> from itertools import chain
>>> list(chain.from_iterable([state.split() for state in states]))
['Kansas', 'North', 'Dakota', 'Nebraska', 'South', 'Dakota']
# Set comprehension
>>> {element for element in list(chain.from_iterable([state.split() for state in states]))}
{'Kansas', 'South', 'Dakota', 'Nebraska', 'North'}
- 2.1 String Basics
- 2.2 String Concatenation and Interpolation
- 2.3 Printing
- 2.4 Length, Booleans, and Control Flow
- 2.4 Length, Booleans, and Control Flow | Boolean Context
- 2.5 Methods
- 2.6 String Iteration
- 3.1 Splitting
- 3.2 List Access
- 3.3 List Slicing
- 3.4 More List Techniques
- 3.5 List Iteration
- 3.6 Tuples and Sets
- 4.1 Math
- 4.2 Time and Datetime
- 4.3 Regular Expressions
- 4.4 Dictionaries
- 4.5 Application - Unique Words
- 5.1 Function Definitions
- 5.1.1 First Class Functions
- 5.1.2 Variable and Keyword Arguments
- 5.2 Functions in a file
- 5.3 Iterators
- 5.3.1 Generators