TelenkovDmitry 5 месяцев назад
Родитель
Сommit
ce01fed4d8
2 измененных файлов с 93 добавлено и 2 удалено
  1. 33 0
      courses/python_func/func_kwargs.py
  2. 60 2
      courses/python_func/func_unpack.py

+ 33 - 0
courses/python_func/func_kwargs.py

@@ -0,0 +1,33 @@
+
+# Распаковка словаря с ключами и значениями
+def func_1():
+    d1 = {'key1': 1, 'key2': 2}
+    d2 = {'key2': 1, 'key3': 2}
+    d3 = {'a1': 1, **d2, **d1}
+    print(d3)
+
+
+def concatenate(**kwargs):
+    ret = ""
+    for x in kwargs.values():
+        if (type(x) == list):
+            ret += ''.join(str(x))
+        else:
+            ret += str(x)
+    return ret
+
+
+
+
+def main():
+    # func_1()
+
+    print(concatenate(q='iHave', w="next", e="Coins", r=[10, 5, 10, 7]))
+    # print(concatenate(q='iHave', w="next", e="Coins"))
+    # l = [1, 2, 3]
+    # foo = ''.join(str(l))
+    # print(foo)
+
+
+if __name__ == '__main__':
+    main()

+ 60 - 2
courses/python_func/func_unpack.py

@@ -1,3 +1,5 @@
+from functools import reduce
+
 
 def func_1():
     a, b, *c = [1, True, 4, 6, 'hello ', 7, 9]
@@ -28,10 +30,66 @@ def func_2():
     print(values)
 
 
+# def print_values(one, two, three):
+#     print(one, two, three)
+
+
+def print_values(*values):
+    print(values, sep=',')
+
+
+def count_args(*args):
+    return len(args)
+
+
+def multiply(*args):
+    if not args:
+        return 1
+    return reduce(lambda a, b: a*b, args)
+
+
+def check_sum(*args):
+    return print("not enough") if sum(args) < 50 else print("verification passed")
+
+
+def is_only_one_positive(*args):
+    return len(list(filter(lambda x: x > 0, args))) == 1
+
+
+def print_goods(*args):
+    counter = 1
+    for x in args:
+        if isinstance(x, str) and x != "":
+            print(f'{counter}. {x}')
+            counter += 1
+    if counter == 1:
+        print('Нет товаров')
+
+
+
 def main():
     # func_1()
-    func_2()
+    # func_2()
+
+    # words = 'Hello', 'Aloha', 'Bonjour'
+    # print_values(*words)
+
+    # print_values(1, 2, 3, 4)
+
+    # print(count_args(1, 2, 3, 4, 5))
+
+    # print(multiply(1, 2, 3, 4, 5))
+
+    # print(multiply(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10))
+
+    # check_sum(10, 10, 10, 10, 9)
+    
+    # print(is_only_one_positive(-3, -4, -1, 3, 4))
+
+    print_goods(1, True, 'Грушечка', '', 'Pineapple') 
+
 
 
 if __name__ == '__main__':
-    main()
+
+        main()