User Tools

Site Tools


python:workout

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python:workout [2026/02/27 08:21] – created v1ctorpython:workout [2026/03/24 11:31] (current) – [GEERATORS] v1ctor
Line 3: Line 3:
 ==== SPLAT OPERATOR ==== ==== SPLAT OPERATOR ====
  
-Splat operator (aka "*****") allows a function to receive any number or arguments.+Splat operator (aka ''*'') allows a function to receive any number or arguments. 
 + 
 +<code python> 
 +def mysum(*numbers): 
 +    print(type(numbers))  # tuple 
 +     
 +mysum(1,2,3,4,5)     # passing arbitrary number of arguments 
 +mysum(*[2,3,4,5,6])  # unpacking an iterable 
 +</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> 
 +" ".join(some_func(w) for w in s.split())    # parens omitted (cleaner) 
 +</code>
python/workout.1772180483.txt.gz · Last modified: by v1ctor