-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFunctions.py
More file actions
181 lines (115 loc) · 4.09 KB
/
Functions.py
File metadata and controls
181 lines (115 loc) · 4.09 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Python Plus Lesson First Subject
# Pre-class
# Global and Local Variables
text = "I am the global one"
def global_func():
print(text) # we can use 'text' in a function
# because it's a global variable
global_func() # 'I am the global one' will be printed
print(text) # it can also be printed outside of the function
text = "The globals are valid everywhere "
global_func() # we changed the value of 'text'
# 'The globals are valid everywhere' will be printed
def local_func():
local_text = "I am the local one"
print(local_text) # local_text is a local variable
local_func() # 'I am the local one' will be printed as expected
# print(local_text) # NameError will be raised
# because we can't use local variable outside of its function
# LEGB Ranking Rule
# 1- Locals. The space which is searched first, contains the local names defined in a function body.
# 2- Enclosing. The scopes of any enclosing functions, which are searched starting with the nearest enclosing scope
# (from inner to outer), contains non-local, but also non-global names.
# 3- Globals. It contains the current module’s global names. The variables defined at the top-level of its module.
# 4- Built-in. The outermost scope (searched last) is the namespace containing built-in names.
variable = "global"
def func_outer():
variable = "enclosing outer local"
def func_inner():
variable = "enclosing inner local"
def func_local():
variable = "local"
print(variable)
func_local()
func_inner()
func_outer() # prints 'local' defined in the innermost function
print(variable) # 'global' level variable holds its value
# Global and Nonlocal
# if we didn't use global count in func.
# counter() it will raise UnboundLocalError
# Global
count = 1
def counter():
global count # we've changed its scope
print(count) # it's global anymore
count += 1
counter()
counter()
counter()
# Nonscope
def enclosing_func1():
x = 'outer variable'
def enclosing_func2():
nonlocal x # its inner-value can be used in the outer scope
x = 'inner variable'
print("inner:", x)
enclosing_func2()
print("outer:", x)
enclosing_func1()
# In-Class
def brothers(bro1, bro2, bro3):
print(bro1, bro2, bro3, end='')
print()
bros = ['ali', 'hasan', 'mehmet']
brothers(*bros)
def merger(*genius):
print(f"For me, {genius[0]} {genius[1]} and {genius[2]} {genius[3]} are geniuses.")
merger('Bill', 'Gates', 'Guido van', 'Rossum')
def gene(x="Solomon", y="David"):
print(x, "belongs to x")
print(y, "belongs to y")
gens = {"y": "Ahmet", "x": "Mehmet"}
gene(**gens)
dict_couple = {"bride": ["esma", "ela", "ayşe", "gönül"],
"groom": ["ahmet", "semih", "tamer", "ali"]}
def muruvvet2(bride, groom):
return [x for x in zip(bride, groom)]
print(muruvvet2(** dict_couple))
# average of friend's age
dict_friends = {"ahmet": 23, "alfred": 34, "rose": 41, "sena": 21, "ali": 19}
def meaner(**ages):
values = sum([i for i in ages.values()])
return "The average is:", (values / len(ages))
print(meaner(** dict_friends))
# Lambda
# You can use it with its own syntax using parentheses,
# You can also assign it to a variable,
# You can use it in several built-in functions,
# It can be useful inside user-defined functions (def).
# lambda usage
print((lambda x, y: x**y)(2, 3))
v = (lambda x: x[::-1])("arter")
print(v)
n = [1, 4, 6]
n2 = [1, 2, 5]
k = list(map(lambda x, y: x*y, n, n2))
print(k)
words = ["alinin evi", "hasanın ocağı", "keremin yunusu", "asd"]
print(list(map(len, words)))
a = list(filter(lambda x: len(x) < 5, words))
print(a)
# arbitrary func ile lambda
equalambda = lambda *arg: list(arg).count(max(list(arg), key=list(arg).count)) \
if list(arg).count(max(list(arg), key=list(arg).count)) > 1 else 0
print(equalambda(1, 2, 3, 4, 45, 4, 4))
# Function Generator
# Start
def function_generator(x):
return lambda j: x(j)
d = function_generator(print)
d1 = function_generator(max)
d2 = function_generator(sorted)
d("hello")
print(d1([3, 4, 5, 6]))
print(d2([32, 45, 66, 90, 2]))
# End