-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda1.py
More file actions
65 lines (63 loc) · 1.67 KB
/
Copy pathlambda1.py
File metadata and controls
65 lines (63 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Python 3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>> x = lambda(i: i * 2 + 3)
SyntaxError: invalid syntax
>>> x = lambda i: i * 2 + 3
>>> x(4)
11
>>> import math
>>> y = lambda i: math.sqrt(math.exp(i))
>>> y(4)
7.38905609893065
>>> x = lambda _: print('Ola mundo!')
>>> x()
Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "<pyshell#7>", line 1, in <module>
TypeError: <lambda>() missing 1 required positional argument: '_'
>>> x = lambda: print('Olá mundo!')
>>> x()
Olá mundo!
>>> type(x)
<class 'function'>
>>> x = lambda nome, idade: print(f"nome = {nome}\n idade = {idade}.")
>>> x('Danilo', 24)
nome = Danilo
idade = 24.
>>> x = lambda nome, idade: print(f"nome = {nome}\nidade = {idade}.")
>>> x('Caio', 20)
nome = Caio
idade = 20.
>>> x = lambda nome, idade: print(f"nome = {nome}\nidade = {idade}")
>>> x('Caio', 20)
nome = Caio
idade = 20
>>> x = lambda *args: print(args)
>>> x('caio', 'marcos', 'joao')
('caio', 'marcos', 'joao')
>>> x('caio')
('caio',)
>>> x()
()
>>> def f():
return 'caio', 'marcos'
>>> x(f)
(<function f at 0x7f941b161e50>,)
>>> x(f())
(('caio', 'marcos'),)
>>> x(*f)
Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "<pyshell#26>", line 1, in <module>
TypeError: <lambda>() argument after * must be an iterable, not function
>>> def teste():
return lambda *args: print(args)
>>> x = teste()
>>> print(x)
<function teste.<locals>.<lambda> at 0x7f941a0d8160>
>>> x('caio', 'marcos')
('caio', 'marcos')
>>>