Python for Beginners: Variable Basics — Definition, Assignment, and Usage

What is a Variable?

In programming, a variable is like a “container” for storing data. Imagine that every day we need to record information such as our age, weight, and bank balance—variables allow us to “remember” this information. For example, you can think of the variable “age” as a box that holds your age (like 18 years old). When you need it later, you can simply open the box to see or modify the number inside.

Why Use Variables?

Without variables, you would have to repeatedly write specific values every time you use data. For example, to calculate “Xiaoming’s age + 5”, writing 18 + 5 every time would be cumbersome. Using a variable age = 18, you can directly use age + 5 for quick calculations, and you can also modify the value of age at any time (e.g., age = 20), making it very flexible.

Definition and Assignment: Filling “Data” into Variables

In Python, defining a variable is straightforward—simply assign a value to it. The assignment operator is = (note: this = is not the same as the mathematical “equals”; it means “putting the value on the right into the variable on the left”).

Syntax:
variable_name = value

Examples:

age = 18       # Define variable 'age' and assign it 18 (integer)
name = "Xiaoming"  # Define variable 'name' and assign it the string "Xiaoming"
score = 95.5   # Define variable 'score' and assign it 95.5 (float)

Here, it’s important to note that Python is a dynamically typed language. The type of a variable is determined by its assignment, and you don’t need to declare the type in advance. For example, a = 10 makes a an integer, but later you can reassign it to a string: a = "hello", and now a is a string.

Variable Naming Rules: Giving Variables Good Names

Variable names must follow certain rules; otherwise, you’ll get an error. The rules are:
1. Only letters, numbers, and underscores (_) are allowed—no spaces or special symbols (e.g., -, @, #).
2. Cannot start with a number (e.g., 1name is invalid, but name1 is valid).
3. Cannot use Python keywords (e.g., if, for, while, print—these have special functions and can’t be used as variable names).
4. Case-sensitive: age and Age are two different variables.

Examples of valid variable names:
student_name, score1, _count, age2023

Examples of invalid variable names:
2score (starts with a number), my-name (contains a hyphen), if (keyword), my score (contains a space)

Using Variables: How to “Use” Data in Variables?

After defining a variable, you can access or modify its data using the variable name. The most common ways are printing the variable or using it in calculations.

1. Printing Variables (Viewing Values)

Use the print() function to output the variable’s value:

age = 18
print(age)  # Output: 18

name = "Xiaoming"
print(name)  # Output: Xiaoming

2. Using Variables in Operations

Variables can participate in mathematical operations or string concatenation like regular values:

# Numerical operations
x = 10
y = 20
z = x + y
print(z)  # Output: 30 (x + y = 30, so z = 30)

# String concatenation
a = "Hello, "
b = "Python"
c = a + b
print(c)  # Output: Hello, Python

Variable Types: Python’s “Flexible” Feature

Python variables are dynamically typed, meaning their type changes automatically based on their assignment. For example:

a = 10   # 'a' is an integer (int)
a = "Python"  # After reassignment, 'a' becomes a string (str)
a = True       # After another assignment, 'a' becomes a boolean (bool)
print(a)  # Output: True

This means the same variable can first store an integer, then a string, making it very flexible. However, be careful: the variable’s type is determined by its last assignment. Avoid type confusion (e.g., when concatenating numbers and strings).

Notes on Using Variables

  1. Variables must be assigned before use
    If you use an unassigned variable, you’ll get an error: NameError: name 'xxx' is not defined.
    Invalid example:
   print(age)  # Error! 'age' was never assigned

Valid example:

   age = 18
   print(age)  # Correct, outputs: 18
  1. Variable names should be meaningful
    Good variable names make code more readable. For example, use student_count instead of sc, or user_age instead of a.

  2. Avoid overwriting with repeated assignments
    Repeated assignments will overwrite the original value:

   score = 90
   score = 95  # Original value 90 is overwritten; now score = 95
   print(score)  # Output: 95

Summary

Variables are the core tool for storing data in Python. Defining and assigning them is simple (just variable_name = value), and they follow naming rules. Python’s dynamic typing makes variables flexible, so focus on the “value” and “purpose” of the variable. Practice assigning, modifying, and using variables in operations to master them quickly!

Small Exercise: Try defining a variable height to store your height (e.g., 170), and a variable weight to store your weight (e.g., 60). Then calculate height + weight and print the result!

Xiaoye