|
| 1 | +""" |
| 2 | +Title: Python Functions Tutorial |
| 3 | +Author: Python-Basics-to-Advanced Contributors |
| 4 | +Difficulty: Beginner |
| 5 | +Description: Comprehensive guide to defining and using functions in Python with examples |
| 6 | +Date: October 2025 |
| 7 | +""" |
| 8 | + |
| 9 | +# ============================================================================= |
| 10 | +# 1. DEFINING FUNCTIONS |
| 11 | +# ============================================================================= |
| 12 | + |
| 13 | +print("=== Python Functions Tutorial ===\n") |
| 14 | + |
| 15 | +def greet(name): |
| 16 | + """ |
| 17 | + Greets a person by name. |
| 18 | +
|
| 19 | + Args: |
| 20 | + name (str): The person's name. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + str: Greeting message. |
| 24 | + """ |
| 25 | + return f"Hello, {name}!" |
| 26 | + |
| 27 | +print(greet("Alice")) |
| 28 | + |
| 29 | +# ============================================================================= |
| 30 | +# 2. FUNCTION PARAMETERS AND DEFAULTS |
| 31 | +# ============================================================================= |
| 32 | + |
| 33 | +def power(base, exponent=2): |
| 34 | + """ |
| 35 | + Calculates the power of a base number. |
| 36 | +
|
| 37 | + Args: |
| 38 | + base (int or float): Base number. |
| 39 | + exponent (int or float): Exponent, default is 2. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + int or float: Result of base raised to exponent. |
| 43 | + """ |
| 44 | + return base ** exponent |
| 45 | + |
| 46 | +print(f"5^2 = {power(5)}") |
| 47 | +print(f"3^3 = {power(3, 3)}") |
| 48 | + |
| 49 | +# ============================================================================= |
| 50 | +# 3. RETURNING MULTIPLE VALUES |
| 51 | +# ============================================================================= |
| 52 | + |
| 53 | +def arithmetic_ops(a, b): |
| 54 | + """ |
| 55 | + Performs basic arithmetic operations on two numbers. |
| 56 | +
|
| 57 | + Args: |
| 58 | + a (int or float): First number. |
| 59 | + b (int or float): Second number. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + tuple: Sum, difference, product, and quotient. |
| 63 | + """ |
| 64 | + sum_ = a + b |
| 65 | + diff = a - b |
| 66 | + prod = a * b |
| 67 | + quo = a / b if b != 0 else None |
| 68 | + return sum_, diff, prod, quo |
| 69 | + |
| 70 | +s, d, p, q = arithmetic_ops(10, 5) |
| 71 | +print(f"Sum: {s}, Difference: {d}, Product: {p}, Quotient: {q}") |
| 72 | + |
| 73 | +# ============================================================================= |
| 74 | +# 4. LAMBDA (ANONYMOUS) FUNCTIONS |
| 75 | +# ============================================================================= |
| 76 | + |
| 77 | +add = lambda x, y: x + y |
| 78 | +square = lambda x: x * x |
| 79 | + |
| 80 | +print(f"Add 3 + 4 = {add(3, 4)}") |
| 81 | +print(f"Square of 6 = {square(6)}") |
| 82 | + |
| 83 | +# ============================================================================= |
| 84 | +# 5. FUNCTION SCOPE AND LOCAL VARIABLES |
| 85 | +# ============================================================================= |
| 86 | + |
| 87 | +def demonstrate_scope(): |
| 88 | + x = 10 # local variable |
| 89 | + print(f"Inside function, x = {x}") |
| 90 | + |
| 91 | +demonstrate_scope() |
| 92 | +# print(x) # This would raise an error because x is local |
| 93 | + |
| 94 | +print("\n=== End of Functions Tutorial ===") |
| 95 | +print("Practice tip: Try creating your own functions and experimenting with parameters and returns!") |
0 commit comments