Skip to content

Commit 8315b7e

Browse files
loops and functions
1 parent e694b7f commit 8315b7e

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

functions/define.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
def wish(name):
3+
print(f"Hello {name} wish you many more happy returns of the day !!")
4+
5+
wish("prabha")
6+

functions/function_argument.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# positional argument
3+
4+
def add(a, b):
5+
return a + b
6+
7+
result = add(3, 5)
8+
print(result) # Output: 8
9+
10+
11+
# keyword argument
12+
13+
def introduce(name, age):
14+
print(f"Name: {name}, Age: {age}")
15+
16+
introduce(age=25, name="prabha")
17+
18+
19+
# default argument
20+
21+
def wish(name, message="Hello"):
22+
print(f"{message}, {name}!")
23+
24+
wish("prabhakar")
25+
wish("prabhakar", "Hi")
26+
27+
28+
# variable length argument
29+
30+
def add(* args):
31+
return sum(args)
32+
33+
result = add(1, 2, 3, 4, 5)
34+
print(result) # Output: 15
35+

functions/global_local.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
def local_scope():
4+
x = 10 # Local variable
5+
print(f"Inside function: {x}")
6+
7+
8+
local_scope()
9+
10+
11+
x = 20 # Global variable
12+
13+
def global_scope():
14+
print(f"Inside function: {x}")
15+
16+
global_scope()
17+
print(f"Outside function: {x}")
18+

functions/lambda_function.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# lambda function
3+
# initialize variable becomes function
4+
# after lambda keyword is parameters
5+
# after column actual logic like below
6+
7+
square = lambda x: x * x
8+
print(square(5)) # Output: 25
9+
10+
add = lambda a, b: a + b
11+
print(add(3, 4)) # Output: 7
12+
13+
# run and check
14+
hello = lambda name: f"Namaste {name} How are you !"
15+
print(hello("prabhakar"))

loops/forloop.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# sample basic loop
3+
print("basic")
4+
for i in range(10):
5+
print(i)
6+
7+
print("beginner")
8+
for i in range(1,10):
9+
print(i)
10+
11+
print("medium")
12+
for i in range(1,11):
13+
print(i)
14+
15+
16+
print("advance")
17+
for i in range(10):
18+
print(i+1)
19+
20+
21+
print("Even in 1 to 20")
22+
for i in range(20):
23+
if(i % 2 == 0 and i > 0):
24+
print(i)
25+
26+
27+
print("Even in 1 to 20 without condition")
28+
for i in range(2,20,2):
29+
print(i)
30+
31+
print("Even way")
32+
for i in range(1,20,2):
33+
print(i+1)

loops/whileloop.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# simple basic loop
2+
print("first way")
3+
i = 0
4+
while i<=10:
5+
print(i)
6+
i += 1
7+
8+
9+
print("second way")
10+
i = 0
11+
while i < 10:
12+
print(1+i)
13+
i += 1
14+
15+
16+
print("third way")
17+
i = 1
18+
while i <= 10:
19+
print(i)
20+
i +=1
21+
22+
23+
print("even 1 ")
24+
i=0
25+
while i <= 10:
26+
if i % 2 == 0:
27+
print(i)
28+
i += 1
29+
30+
31+
print("Even 2")
32+
i = 2
33+
while i <= 10:
34+
print(i)
35+
i += 2
36+
37+
38+
print("odd 1")
39+
i=0
40+
while i <= 10:
41+
if i % 2 != 0:
42+
print(i)
43+
i += 1
44+
45+
46+
print("odd 2")
47+
i = 1
48+
while i <= 10:
49+
print(i)
50+
i += 2

0 commit comments

Comments
 (0)