Skip to content

Commit 2287fe2

Browse files
02-Python
1 parent 3a27f56 commit 2287fe2

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
### Python Functions And Scope ###
2+
3+
#! Function Without Parameters
4+
5+
def my_func():
6+
print('Hello I\'m a function')
7+
8+
my_func()
9+
10+
#! Function Without Parameters or Return
11+
def empty_func():
12+
pass
13+
14+
#! Function With Parameters
15+
16+
num_one = 1
17+
num_two = 5
18+
19+
def add(num1, num2):
20+
print(num1 + num2)
21+
22+
add(num_one, num_two)
23+
24+
#! Function With Return
25+
26+
num_one = 1
27+
num_two = 5
28+
29+
def add(num1, num2):
30+
return num1 + num2
31+
32+
my_sum = add(num_one, num_two)
33+
34+
print(my_sum)
35+
36+
#! Function Inside Another Function
37+
38+
def top_function():
39+
my_num = 20
40+
num_one = 5
41+
num_two = 10
42+
43+
def inside_function(first_num, second_num):
44+
sum = first_num + second_num
45+
return sum
46+
47+
inside_func_val = inside_function(num_one, num_two)
48+
49+
print(inside_func_val + my_num)
50+
51+
top_function()
52+
53+
#! Python Built In Functions
54+
55+
# Range
56+
my_range = range(1, 10)
57+
print(*my_range)
58+
59+
# List
60+
my_list = list(my_range)
61+
print(my_list)
62+
63+
# Sorted
64+
print(sorted(my_list))
65+
66+
# Len
67+
print(len(my_list))
68+
69+
# Sum
70+
print(sum(my_list))
71+
72+
# Max
73+
print(max(my_list))
74+
75+
# Min
76+
print(min(my_list))
77+
78+
# Round
79+
print(round(20.85))
80+
81+
# Slice
82+
print(my_list[1:4])
83+
84+
# Int
85+
print(int('8'))
86+
87+
# Str
88+
print(str(10))
89+
90+
# Type
91+
print(type(int('8')))
92+
print(type(str(10)))
93+
94+
#! Functions Global Scope
95+
96+
num_one = 50 # Global variable
97+
num_two = 25 # Global variable
98+
99+
def function_global_vars():
100+
return num_one + num_two
101+
102+
print(function_global_vars())
103+
104+
num_one = 50 # Global variable
105+
num_two = 25 # Global variable
106+
107+
def function_global_vars():
108+
num_one = 25 # Local variable
109+
num_two = 10 # Local variable
110+
111+
return num_one + num_two
112+
113+
print(function_global_vars())
114+
115+
#! Functions Local Scope
116+
117+
def function_local_vars():
118+
num_one_local = 15
119+
num_two_local = 8
120+
121+
return num_one_local - num_two_local
122+
123+
print(function_local_vars())
124+
125+
#* print(num_one_local) will throw an error since the variable is declared inside a function local scope
126+
127+
#! Lambda Functions
128+
129+
sum = lambda first_num, second_num: first_num + second_num
130+
print(sum(10, 50))
131+
132+
sum_two = lambda first_num, second_num: first_num + second_num + 60
133+
print(sum_two(10, 50))
134+
135+
def function_with_lambda(first_arg, second_arg):
136+
lam_sum = lambda first_lam_arg, second_lam_arg: first_lam_arg + second_lam_arg * 30
137+
return lam_sum(first_arg, second_arg) + 500
138+
139+
print(function_with_lambda(125, 500))
140+
141+
#! Optional Challenge
142+
print('---Optional Challenge----')
143+
144+
def fizz_buzz_twist(txt1: str, txt2: str):
145+
times_when_num = 0
146+
147+
for num in range(1, 101):
148+
if num % 3 == 0 and num % 5 == 0:
149+
print(f'{txt1} and {txt2}.')
150+
elif num % 3 == 0:
151+
print(f'{txt1}.')
152+
elif num % 5 == 0:
153+
print(f'{txt2}.')
154+
else:
155+
times_when_num += 1
156+
157+
return times_when_num
158+
159+
print(fizz_buzz_twist('It\'s multiple of 3', 'It\'s multiple of 5'))
160+
161+

0 commit comments

Comments
 (0)