-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07_Function.py
More file actions
145 lines (103 loc) · 2.9 KB
/
07_Function.py
File metadata and controls
145 lines (103 loc) · 2.9 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
# Learn Python Functions - 20 Examples with Comments and Explanations
# 1️⃣ Basic Function
# Simple function that prints a message
def greet():
print("1. Hello, Sishir!")
greet()
# 2️⃣ Function with one parameter
def greet_user(name):
print("2. Hello,", name)
greet_user("Siam")
# 3️⃣ Function with return value
def add(a, b):
return a + b
print("3. Sum:", add(5, 10))
# 4️⃣ Function with default argument
def greet(name="Guest"):
print("4. Welcome", name)
greet()
greet("Tushar")
# 5️⃣ Keyword arguments
def user_info(name, age):
print(f"5. {name} is {age} years old.")
user_info(age=20, name="Sishir")
# 6️⃣ Arbitrary arguments (*args)
def sum_all(*numbers):
total = 0
for num in numbers:
total += num
return total
print("6. Total:", sum_all(1, 2, 3, 4, 5))
# 7️⃣ Arbitrary keyword arguments (**kwargs)
def print_info(**data):
for key, value in data.items():
print(f"7. {key} = {value}")
print_info(name="Siam", country="BD")
# 8️⃣ Return multiple values
def calculate(a, b):
return a + b, a - b, a * b
add, sub, mul = calculate(10, 5)
print("8. Add:", add, "Sub:", sub, "Mul:", mul)
# 9️⃣ Check even or odd
def is_even(n):
return n % 2 == 0
print("9. Is 6 even?", is_even(6))
# 🔟 Function with loop inside
def print_table(n):
print(f"10. Table of {n}:")
for i in range(1, 11):
print(n, "x", i, "=", n*i)
print_table(5)
# 1️⃣1️⃣ Function calling another function
def double(n):
return n * 2
def triple_double(n):
return double(n) * 3
print("11. Triple of double:", triple_double(2))
# 1️⃣2️⃣ Function with condition
def grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
else:
return "C"
print("12. Grade:", grade(85))
# 1️⃣3️⃣ Recursive function
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print("13. Factorial of 5:", factorial(5))
# 1️⃣4️⃣ Lambda (anonymous) function
square = lambda x: x * x
print("14. Square:", square(4))
# 1️⃣5️⃣ Function with list
def get_even(numbers):
return [n for n in numbers if n % 2 == 0]
print("15. Even numbers:", get_even([1, 2, 3, 4, 5]))
# 1️⃣6️⃣ Global variable usage
x = 10
def modify_global():
global x
x = 20
modify_global()
print("16. Global x:", x)
# 1️⃣7️⃣ Function with nested function
def outer():
def inner():
return "Inner Function"
return inner()
print("17. Result:", outer())
# 1️⃣8️⃣ Function with pass (empty function)
def to_do():
pass
print("18. Defined empty function to_do() with pass")
# 1️⃣9️⃣ Function with type hints
def add_numbers(a: int, b: int) -> int:
return a + b
print("19. Type hint result:", add_numbers(7, 3))
# 2️⃣0️⃣ Function using f-string
def welcome(name):
return f"20. Welcome, {name}!"
print(welcome("Shishir"))