← Back to Chapters

Python Variables

? Python Variables

⚡ Quick Overview

A variable in Python is a name that refers to a value stored in memory. You can use variables to store data such as numbers, strings, lists, and more. Variables let you reuse values, perform calculations, and pass information around in your programs.

? Key Concepts

  • Variable: A named reference to a value in memory.
  • Assignment: Use the = operator to store a value in a variable.
  • Naming rules: Start with a letter or underscore, no spaces, no starting digits.
  • Case-sensitive: myVar and myvar are different variables.
  • Multiple assignment: Assign several variables in a single line.
  • Global variables: Variables that live outside functions and can be accessed inside them.

? Syntax and Theory

In Python, you create a variable the moment you first assign a value to a name using the = operator. There is no need to declare the type; Python infers it from the value.

Variable names must:

  • Start with a letter (A–Z or a–z) or an underscore _.
  • Not start with a digit.
  • Contain only letters, digits, and underscores.
  • Respect case-sensitivity.

You can also assign multiple values in a single statement or assign the same value to multiple variables. To display values, use the built-in print() function, often combined with formatted strings (f-strings) for cleaner output.

A global variable is defined outside functions. Inside a function, you can modify it using the global keyword.

? Code Examples

? Basic variable declarations
x = 10
name = "Alice"
price = 99.99
? Valid and invalid variable names
valid_name = "Python"
_name = "Valid"
name1 = "Also valid"
# 1name = "Invalid"  # ❌ starts with number
? Assign multiple values
a, b, c = 1, 2, 3
print(a, b, c)

x = y = z = 100
print(x, y, z)
?️ Output variables with print() and f-strings
name = "Alice"
age = 25

print("Name:", name)
print("Age:", age)

print(f"My name is {name} and I am {age} years old.")
? Global variables inside functions
x = "global"

def show():
    global x
    x = "modified"
    print("Inside:", x)

show()
print("Outside:", x)

? Output and Explanation

? What the code prints

For the multiple assignment example:

a, b, c = 1, 2, 3
print(a, b, c)

x = y = z = 100
print(x, y, z)

The output will be:

1 2 3
100 100 100

For the global variable example, the function changes the value of x for the entire program:

x = "global"

def show():
    global x
    x = "modified"
    print("Inside:", x)

show()
print("Outside:", x)

The output will be:

Inside: modified
Outside: modified

Because of the global keyword, the assignment inside show() updates the same x that is used outside the function.

? Tips and Best Practices

  • Use descriptive variable names — prefer user_name over just x or y.
  • Stick to lowercase letters and underscores for readability (e.g. total_price).
  • Use f-strings for clean, readable string output with variables.
  • Remember that Python is case-sensitive: Name and name are different.
  • Avoid using a variable before assigning a value; this causes a NameError.
  • Do not reuse the same variable name for unrelated purposes; it makes code hard to read.
  • Use global variables sparingly; prefer passing values as function parameters when possible.

? Try It Yourself

  • Create three variables: name, age, and city. Print them in one line using an f-string.
  • Assign values to a, b, and c in a single line and print their sum.
  • Write a function that modifies a global variable and prints the result from both inside and outside the function.
  • Experiment with different variable names to see which ones are valid and which ones cause errors.