Python Variables: naming, dynamic declaration, comments, multiple assignment, global variables

Python variables act as flexible name-tags for data, letting you declare them on the fly, link multiple values simultaneously, or share them globally across functions.

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The Disappearing Data Type Declaration

In your Semester 1 BCA104 C language labs, writing int total = 100; was mandatory. If you later tried to assign total = "Overspent";, the compiler would throw a fit. Why does Python let you write balance = 500 instantly without any datatype prefix, and then cleanly change that same variable to a word or a decimal on the very next line? Let us uncover how variables work behind the scenes in Python.

Theory

Sticky Labels vs Ice Trays

Think of a variable in C like an ice tray slot: it has a fixed size and shape molded directly into memory. You can only pour water (integers) into it. A Python variable is like a sticky paper label with a name written on it. You can peel the label off a whole number block and stick it directly onto a text string block. The data has a type, but the label itself can stick to anything!

Theory

The Rules of the Name Game

A Python variable is a named reference pointing to an object in computer memory. Dynamic declaration means a variable is created the exact moment you assign a value to it using the = operator, with no data type specified beforehand. Python variables follow strict identifier naming rules: they must start with a letter or an underscore, cannot start with a digit, and are highly case-sensitive (balance and Balance are distinct).

At a glance

Table 1: Essential variable syntax and scope modifiers in Python.

Variable Syntax FeatureHow It Works in PythonPocketMoney Example
Dynamic DeclarationNo type initialization, created on assignment.balance = 1000
Multiple AssignmentAssign one value to many, or unpack many at once.init_bal, reward = 500, 50
CommentsUses the hash symbol # for documentation.# Updates monthly expense
Global KeywordDeclares a local block variable belongs to top scope.global balance

Theory

A Worked Example: Managing the PocketMoney Ledger

Let us implement multiple assignments and functional modifications on our running project data. We will initialize values simultaneously, document our actions using clear comments, and modify a global tracker inside a functional scope.

Practical

PocketMoney Variables and Scope Ledger

# Initialize multi-variable transactions on one line
samosa_cost, chai_cost = 30, 15
balance = 500  # Top-level global balance

def record_spend():
    global balance  # Gain permission to modify top-level variable
    total_spend = samosa_cost + chai_cost
    balance = balance - total_spend

record_spend()
print(balance)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Trace the Variable Modifications

Analyze the Python script above mentally. What exact value remains inside the balance variable at the end of execution? Trace the lines before tapping.

Show the answer

The output will be:

455

Why? Line 2 assigns 30 to samosa_cost and 15 to chai_cost. Line 3 creates the global balance at 500. Inside the function, total_spend becomes 45 (30 + 15). The 'global balance' keyword links the inner balance modification directly to the outside variable, updating 500 - 45 to leave 455.

Quiz

Which of the following is an invalid variable identifier name according to university exam standards?

  1. _monthly_spend
  2. pocketMoney2026
  3. 2way_split
  4. Total_Amount
Show the answer

2way_split

Variable names can contain numbers but must never begin with a digit. Therefore, '2way_split' violates naming rules and causes a syntax error.

Quiz

What happens if you modify an outer variable inside a function without writing the 'global' keyword first?

  1. Python changes the outer variable normally.
  2. Python creates a new local variable with the same name, leaving the outer variable unchanged.
  3. The script triggers a compiler crash before starting.
  4. The variable is converted into a string type automatically.
Show the answer

Python creates a new local variable with the same name, leaving the outer variable unchanged.

Without the global keyword, Python assumes any variable assigned inside a function is a brand new local variable. The external variable remains completely untouched, which often leads to logic bugs.

Watch out

The Classic Trap: Unbound Local Errors

A major mark-losing mistake in university laboratory exams is writing code like: balance = 500 and then inside a def block writing balance = balance - 50 without declaring global balance. Python sees the assignment, marks balance as local to that function, and then throws an UnboundLocalError because it tries to read the local value before it has been initialized!

Theory

Connecting Variables to Semester 3 Modules

Dynamic assignment is powerful but demands clean habits. In Semester 3 (BCA303), when unpacking dataset rows from an SQL query via Python loops, you will use multiple assignments like date, category, price = row to instantly parse complex data points into neat single variables on a single line.

Summary

Key takeaways

  • Variables do not require explicit type declarations and are created upon assignment.
  • Identifiers are case-sensitive and must never start with numeric digits.
  • Multiple variables can be initialized simultaneously using commas on a single line.
  • The hash character # creates single-line descriptions that the interpreter ignores completely.
  • Modifying global variables inside user-defined functions requires an explicit global declaration.
  • Memory Hook: Labels shift scopes consciously!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Python Variables: naming, dynamic declaration, comments, multiple assignment, global variables · Programming Skills · Gri-Learn