func_unpack.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from functools import reduce
  2. def func_1():
  3. a, b, *c = [1, True, 4, 6, 'hello ', 7, 9]
  4. print(a, b, c)
  5. a, *b, c = [1, True, 4, 6, 'hello ', 7, 9]
  6. print(a, b, c)
  7. *a, b, c = [1, True, 4, 6, 'hello ', 7, 9]
  8. print(a, b, c)
  9. a, *b, c = 'hello moto'
  10. print(a, b, c)
  11. a, *b, c = [1, 4]
  12. print(a, b, c)
  13. def func_2():
  14. a, b, *c = range(5)
  15. print(c)
  16. *a, b, c = 'No money', 'no honey'
  17. print(a, b, c)
  18. # *values = [1, 2, 3, 4, 5] # так не сработает
  19. *values, = [1, 2, 3, 4, 5]
  20. print(values)
  21. # def print_values(one, two, three):
  22. # print(one, two, three)
  23. def print_values(*values):
  24. print(values, sep=',')
  25. def count_args(*args):
  26. return len(args)
  27. def multiply(*args):
  28. if not args:
  29. return 1
  30. return reduce(lambda a, b: a*b, args)
  31. def check_sum(*args):
  32. return print("not enough") if sum(args) < 50 else print("verification passed")
  33. def is_only_one_positive(*args):
  34. return len(list(filter(lambda x: x > 0, args))) == 1
  35. def print_goods(*args):
  36. counter = 1
  37. for x in args:
  38. if isinstance(x, str) and x != "":
  39. print(f'{counter}. {x}')
  40. counter += 1
  41. if counter == 1:
  42. print('Нет товаров')
  43. def main():
  44. # func_1()
  45. # func_2()
  46. # words = 'Hello', 'Aloha', 'Bonjour'
  47. # print_values(*words)
  48. # print_values(1, 2, 3, 4)
  49. # print(count_args(1, 2, 3, 4, 5))
  50. # print(multiply(1, 2, 3, 4, 5))
  51. # print(multiply(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10))
  52. # check_sum(10, 10, 10, 10, 9)
  53. # print(is_only_one_positive(-3, -4, -1, 3, 4))
  54. print_goods(1, True, 'Грушечка', '', 'Pineapple')
  55. if __name__ == '__main__':
  56. main()