-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobReturn.py
More file actions
95 lines (66 loc) · 1.58 KB
/
GlobReturn.py
File metadata and controls
95 lines (66 loc) · 1.58 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
def add(value1, value2):
return value1 + value2
result = add(3, 5)
print(result)
# Output: 8
# first without the global variable
def add(value1, value2):
result = value1 + value2
add(2, 4)
print(result)
# Oh crap, we encountered an exception. Why is it so?
# the python interpreter is telling us that we do not
# have any variable with the name of result. It is so
# because the result variable is only accessible inside
# the function in which it is created if it is not global.
# Now lets run the same code but after making the result
# variable global
def add(value1, value2):
global result
result = value1 + value2
add(2, 4)
print(result)
n = int(input('Give n ='))
def code_fkt(n):
f = 1
count = 0
for j in range(1, 2 ** n):
if f > 2**n:
break
f = f*2
for k in range(1, n+1):
count = count + 1
print(count)
return count
print('No of times ' + str(code_fkt(n)))
#2. Multiple return values, but better not with global keyword
def profile():
global name
global age
name = "Danny"
age = 30
profile()
print(name)
# Output: Danny
print(age)
# Output: 30
#giving funcs with indexes works nice
def profile():
name = "Danny"
age = 30
return (name, age)
profile_data = profile()
print(profile_data[0])
# Output: Danny
print(profile_data[1])
# Output: 30
#finally we can also use the conventional method
def profile():
name = "Danny"
age = 30
return name, age
profile_name, profile_age = profile()
print(profile_name)
# Output: Danny
print(profile_age)
# Output: 30