-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda00.py
More file actions
84 lines (38 loc) · 1.23 KB
/
lambda00.py
File metadata and controls
84 lines (38 loc) · 1.23 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 22 00:46:34 2018
********* writing a simple lambda function and calling this function **********
@author: Shariful
"""
add_bangs = lambda a: a + '!!!'
add_bangs('hello')
"""
********* convert this simple function into a lambda function **********
def echo_word(word1, echo):
words = word1 * echo
return words
@author: Shariful
"""
# Define echo_word as a lambda function: echo_word
echo_word = lambda word, echo: word * echo
# Call echo_word: result
result = echo_word('hey', 5)
# Print result
print(result)
"""
** Map() and lambda functions **
map the functionality of the add_bangs() function you defined in previous
exercises over a list of strings
"""
# Create a list of strings: spells
spells = ["protego", "accio", "expecto patronum", "legilimens"]
# Use map() to apply a lambda function over spells: shout_spells
shout_spells = map(lambda spell: spell * '!!!', spells)
# Convert shout_spells to a list: shout_spells_list
shout_spells_list = shout_spells.
# Convert shout_spells into a list and print it
print(list(shout_spells_list))
nums = [3, 4, 5]
square_all = map(lambda num: num ** 2, nums)
print(list(square_all))