# Nakafa Framework: LLM
URL: /en/subject/university/bachelor/ai-ds/ai-programming/variable
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/variable/en.mdx
Output docs content for large language models.
---
export const metadata = {
    title: "Variables",
    description: "Learn Python variable concepts, naming rules, keywords that cannot be used, and how memory works. Complete guide with practical examples.",
    authors: [{ name: "Nabil Akbarazzima Fatih" }],
    date: "09/16/2025",
    subject: "AI Programming",
};
## Basic Concept of Variables in Python
Variables in Python are names that refer to specific objects in computer memory. Think of variables like labels or nicknames that you give to a storage box. The box contains values or data, while the label makes it easy for you to find and use the contents of the box whenever needed.
When you write `x = 2`, Python does three important things. First, Python creates an object with the value 2 in memory. Second, Python stores that object at a specific memory address. Third, Python connects the variable name `x` to that memory address.
## How Variables and Memory Work
Every time you create a new variable, Python allocates space in computer memory to store that value. Each memory location has a unique address that you can see using the `id()` function.
Value: 2
Type: int"]
        M2["Address: 2008
Value: 'Hello'
Type: str"]
        M3["Address: 3012
Value: [1,2,3]
Type: list"]
    end
    
    subgraph "Variables"
        V1["x"]
        V2["y"] 
        V3["z"]
        V4["text"]
        V5["numbers"]
    end
    
    V1 --> M1
    V2 --> M1
    V3 --> M1
    V4 --> M2
    V5 --> M3`}
/>
The example above shows an important concept in Python. When multiple variables have the same value, Python often makes them refer to the same object in memory. This is an optimization that Python performs to save memory usage, especially for frequently used objects like small numbers and short strings.
## Python Variable Characteristics
Python has several unique characteristics in variable handling that distinguish it from other programming languages.
### No Variable Declaration
Unlike programming languages such as C or Java, Python does not require explicit variable declaration. You don't need to specify the variable's data type before using it. Python will automatically determine the data type based on the given value.
print(type(umur))       # Output: 
print(type(tinggi))     # Output: 
print(type(aktif))      # Output: `
    }
  ]}
/>
### Dynamic Data Types
Python uses a dynamic data type system, which means variable data types can change while the program is running. One variable can store an integer at one time, then be changed to a string at another time.
## Variable Naming Rules
Python has strict rules for variable naming that you must follow for code to run correctly.
### Variable Name Components
Variable names in Python can consist of three main components. First are letters, both lowercase (a, b, c, ..., z) and uppercase (A, B, C, ..., Z). Second are numbers (0, 1, 2, ..., 9). Third is the underscore character (_).
### Mandatory Rules
Several mandatory rules that you must follow in Python variable naming. Variable names cannot start with a number. Python will generate a syntax error if you try to create a variable that starts with a number.
### Case Sensitivity
Python distinguishes between uppercase and lowercase letters in variable names. This means `nama`, `Nama`, and `NAMA` are three different variables.
## Keywords That Cannot Be Used
Python has a list of keywords that cannot be used as variable names because these words have special functions in the Python language.
### Python Keywords List
Here are the keywords that cannot be used as variable names. The first group includes `and`, `as`, `assert`, `async`, `await`, `break`, `class`, `continue`, `def`, `del`, `elif`, `else`. The second group covers `except`, `finally`, `for`, `from`, `global`, `if`, `import`, `in`, `is`, `lambda`, `nonlocal`, `not`. The third group consists of `or`, `pass`, `raise`, `return`, `try`, `while`, `with`, `yield`, `False`, `True`, `None`.
### Checking Keywords
Python provides the `keyword` module to check whether a word is a keyword or not.
## Variable Naming Best Practices
Although Python provides flexibility in variable naming, there are several best practices you should follow to make code more readable and maintainable.
### Meaningful Names
Use variable names that clearly explain what is stored in the variable. Meaningful names make code easier to understand, both for yourself and others.
### Appropriate Name Length
Avoid variable names that are too long as they will make code hard to read. Conversely, names that are too short may not provide enough information.
### Consistent Writing Style
Python uses snake_case style for variable names, where words are separated by underscores and all letters use lowercase.
### Avoiding Confusion
Avoid using characters that can cause confusion, especially letters that look similar to numbers.
## Example of Variable Usage in Programs
Let's look at how variables are used in a simple program that calculates the area and perimeter of a rectangle.
The example program above shows the use of variables with meaningful names, mathematical calculations, and data validation. Each variable has a clear purpose and a name that explains its contents.
Understanding variable concepts, naming rules, and best practices will help you write cleaner, more readable, and maintainable Python code. Variables are the basic foundation in programming, and good mastery of this concept will facilitate learning more advanced programming concepts.