Snippets Collections
def byte_size(string):

  return(len(string.encode('utf-8')))

byte_size('😀’) # 4
byte_size('Hello World') # 11
Use functools.reduce() to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
from functools import reduce

1.def compose(*fns):
  2.return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
EXAMPLES
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)

multiply_and_add_5(5, 2) # 15
#define a function 
 Def cube(num)
      #write the formula of cube
      return num*num*num
     #give the number to calculate the cube 
     cube(3)
   # print the cube of that number simply by using print command
    print(cube(3))
     “return” keyword means that function have to return          value
 # give a name of function after def
 1.def sayhi():
      #put the statement in the function  
     2.print(“hello world”)
#call function by name:
3.sayhi()


Output: 
Hello world
#define function
defall_unique(lst):

   return len(lst) == len(set(lst))
   
x = [1,1,2,2,3,2,3,4,5,6]


y = [1,2,3,4,5]


all_unique(x) # False
all_unique(y) # True
This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.
   
#make a function:
def get_vowels(string):

#return is the keyword which means function have to return value: 
 return [each for each in string if each in 'aeiou']


#assign the words and function will return vowels words.
get_vowels('foobar') # ['o', 'o', 'a']


get_vowels('gym') # []
#define a function 
 1.Def cube(num)
      #write the formula of cube
      2.return num*num*num
     #give the number to calculate the cube 
     3.cube(3)
   # print the cube of that number simply by using print command
    4.print(cube(3))
     5.“return” keyword means that function have to return value         
def when(predicate, when_true):
  return lambda x: when_true(x) if predicate(x) else x
  
EXAMPLES
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
double_even_numbers(2) # 4
double_even_numbers(1) # 1
def unfold(fn, seed):
  def fn_generator(val):
    while True: 
      val = fn(val[1])
      if val == False: break
      yield val[0]
  return [i for i in fn_generator([None, seed])]
  
  
EXAMPLES
f = lambda n: False if n > 50 else [-n, n + 10]
unfold(f, 10) # [-10, -20, -30, -40, -50]
from functools import reduce

def compose(*fns):
  return reduce(lambda f, g: lambda *args: f(g(*args)), fns)


EXAMPLES
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)

multiply_and_add_5(5, 2) # 15
bookmark
dashboard

Mon Apr 20 2020 13:45:46 GMT+0000 (UTC) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #function #bytesize #return
bookmark
dashboard

Mon Apr 20 2020 13:38:08 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/compose/

#python #python #function #composition
bookmark
dashboard

Mon Apr 20 2020 13:32:07 GMT+0000 (UTC) https://www.youtube.com/watch?v=rfscVS0vtbw&t=5s

#python #python #function #return
bookmark
dashboard

Mon Apr 20 2020 13:17:59 GMT+0000 (UTC) https://www.youtube.com/watch?v=rfscVS0vtbw&t=5s

#python #function #python #def
bookmark
dashboard

Tue Mar 31 2020 11:40:31 GMT+0000 (UTC) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #function #return #allunique
bookmark
dashboard

Tue Mar 31 2020 11:35:03 GMT+0000 (UTC) https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172

#python #python #strings #vowels #function
bookmark
dashboard

Tue Mar 31 2020 05:43:21 GMT+0000 (UTC) https://www.youtube.com/watch?v=rfscVS0vtbw&t=5s

#python #function #returnfunction
bookmark
dashboard

Sat Jan 11 2020 20:54:48 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/when/

#python #function
bookmark
dashboard

Fri Jan 10 2020 19:00:00 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/unfold/

#python #lists #function
bookmark
dashboard

Thu Jan 09 2020 19:00:00 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/compose/

#python #function

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension