Hackforge Academy

Category: python

Variable Scope

Published on 24 Jul 2026

Explanation

Variable scope determines where a variable can be accessed within a program. Python mainly has two types of scope: local

Code:

company = 'HackForge'  # Global Variable


def display_company():
    company = 'OpenAI'  # Local Variable
    print('Inside Function:', company)


display_company()
print('Outside Function:', company)

count = 10


def update_count():
    global count
    count += 5


update_count()
print('Updated Count:', count)

Explanation

Sometimes we do not know how many arguments will be passed to a function. Python provides *args to accept a

Code:

def add_numbers(*args):
    total = 0

    for number in args:
        total += number

    return total

print(add_numbers(10, 20))
print(add_numbers(5, 10, 15, 20))
print(add_numbers(1, 2, 3, 4, 5, 6))

Explanation

The **kwargs parameter allows a function to accept any number of keyword arguments. Inside the function, **kwargs behaves like a

Code:

def display_employee(**kwargs):
    for key, value in kwargs.items():
        print(key, ':', value)


display_employee(
    id=101,
    name='John',
    department='IT',
    salary=55000
)

print('----------------')

display_employee(name='Alice', city='Chennai')

Explanation

Recursion is a programming technique in which a function calls itself to solve a problem. Every recursive function must have

Code:

def factorial(n):
    if n == 1:
        return 1

    return n * factorial(n - 1)

number = 5
result = factorial(number)

print('Factorial of', number, 'is', result)

Explanation

These concepts are frequently combined in real-world applications. Global variables can store application settings, *args can process an unknown number

Code:

tax_rate = 18


def calculate_total(*prices, **details):
    subtotal = sum(prices)
    tax = subtotal * tax_rate / 100
    total = subtotal + tax

    print('Customer:', details.get('customer'))
    print('City:', details.get('city'))
    print('Subtotal:', subtotal)
    print('Tax:', tax)
    print('Total:', total)


calculate_total(
    1200,
    800,
    500,
    customer='Praveen',
    city='Chennai'
)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community