-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
84 lines (48 loc) · 1.62 KB
/
functions.py
File metadata and controls
84 lines (48 loc) · 1.62 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
def greet(name):
"""
Function that greets someone.
name: string parameter
"""
print(f"Hello, {name}!")
# Calling / invoking the function
greet("Ali")
greet("Sara")
print("\n--- Function with return value ---")
def add(a, b):
"""Return the sum of a and b."""
return a + b
result = add(5, 3)
print("5 + 3 =", result)
print("\n--- Function with default parameter ---")
def power(base, exponent=2):
"""
Raise base to the power of exponent.
If exponent is not given, square the base.
"""
return base ** exponent
print("power(4):", power(4)) # uses default exponent=2 → 16
print("power(2, 3):", power(2, 3)) # 2^3 = 8
print("\n--- Function with multiple tasks ---")
def describe_person(name, age, hobby):
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Hobby: {hobby}")
describe_person("Ali", 25, "painting")
print("\n--- Function calling another function ---")
def square(x):
return x * x
def sum_of_squares(a, b):
return square(a) + square(b)
print("Sum of squares of 3 and 4:", sum_of_squares(3, 4))
print("\n--- Variable-length arguments *args, **kwargs ---")
def print_args(*args):
print("Positional arguments:", args)
def print_keyword_args(**kwargs):
print("Keyword arguments:", kwargs)
print_args(1, 2, 3, "hello")
print_keyword_args(name="Sara", age=20)
print("\n--- Returning nothing (i.e., returning None implicitly) ---")
def say_goodbye(name):
print(f"Goodbye, {name}!")
res = say_goodbye("Ahmed")
print("Return value is:", res) # will print None