python:workout
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:workout [2026/02/27 08:23] – [SPLAT OPERATOR] v1ctor | python:workout [2026/03/24 11:31] (current) – [GEERATORS] v1ctor | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| <code python> | <code python> | ||
| def mysum(*numbers): | def mysum(*numbers): | ||
| - | print(type(numbers)) | + | print(type(numbers)) |
| + | |||
| + | mysum(1, | ||
| + | mysum(*[2, | ||
| + | </code> | ||
| + | |||
| + | ==== GEERATORS ==== | ||
| + | |||
| + | A generator expression looks just like a list comprehension but with parentheses instead of brackets: | ||
| + | <code python> | ||
| + | # List comprehension — builds a list | ||
| + | [some_func(w) for w in s.split()] | ||
| + | |||
| + | # Generator expression — produces values lazily | ||
| + | (some_func(w) for w in s.split()) | ||
| + | </code> | ||
| + | |||
| + | When you pass a generator directly as the only argument to a function, you can drop the extra parentheses: | ||
| + | <code python> | ||
| + | " " | ||
| </ | </ | ||
python/workout.1772180616.txt.gz · Last modified: by v1ctor
