Contents
Roadmap info from roadmap website
Variables
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
Visit the following resources to learn more:
- @article@Variables in Python
- @article@W3Schools β Python Variables
- @article@Python Data Types
- @article@Basic Data Types in Python
- @article@Python for Beginners: Data Types
- @video@Python Variables - Python Tutorial for Beginners with Examples | Mosh
Characteristics
1. Dynamic Typing
- No Need for Explicit Type Declaration: Variables in Python do not require you to declare their data type. The type is determined automatically based on the value assigned.
- Type Flexibility: Variables can change types during execution. For example, you can assign an integer to a variable and later assign a string to the same variable.
2. No Need for Variable Declaration
- Direct Assignment: You can create a variable simply by assigning a value to it. Thereβs no need for a separate declaration statement.
3. References and Objects
- Everything is an Object: In Python, all data types are objects, including primitive types like integers and strings.
- Variables as References: Variables in Python act as references to objects in memory, not as containers for the objects themselves. Multiple variables can reference the same object.
4. No Explicit Pointer Arithmetic
- No Pointers: Python does not have pointers or explicit pointer arithmetic, unlike languages such as C or C++.
5. Naming Conventions
-
No Special Characters for Variable Naming: Unlike some languages that require special characters (like
$
in PHP or@
in Java), Python variables are named using simple alphanumeric characters and underscores (_
). -
Case Sensitivity: Python variable names are case-sensitive, meaning
myVar
andmyvar
are considered different variables.
6. Variable Scope
-
Global and Local Scope: Variables can be defined in a global or local scope. Global variables can be accessed inside functions using the
global
keyword, and nonlocal variables in nested functions using thenonlocal
keyword. - No Block Scope: Unlike languages like Java or C++, Python does not have block scope within loops or conditionals. Variables defined within such blocks are still accessible outside them, as long as they are in the same function or module.
7. Immutable vs. Mutable Objects
- Immutable Variables: Types like integers, strings, and tuples are immutable, meaning their value cannot be changed after creation. Assigning a new value to an immutable variable actually creates a new object.
- Mutable Variables: Types like lists, dictionaries, and sets are mutable, allowing their contents to be changed in place.
8. Multiple Assignment
-
Tuple Unpacking: Python allows multiple variables to be assigned values in a single statement, often using tuples or lists (e.g.,
a, b, c = 1, 2, 3
). -
Swapping Variables: You can swap the values of two variables in a single line without needing a temporary variable (e.g.,
a, b = b, a
).
9. Global, Local, and Nonlocal Keywords
-
global
Keyword: Used to modify a global variable inside a function. -
nonlocal
Keyword: Used in nested functions to modify a variable from the nearest enclosing scope that is not global.
10. Garbage Collection
- Automatic Memory Management: Python handles memory management automatically using a garbage collector that recycles objects no longer in use. This is transparent to the user, meaning you donβt have to manually free memory.
11. Type Hints (Optional)
-
Optional Type Annotations: Python allows for optional type hints to specify the expected data type of variables, but these are not enforced at runtime (e.g.,
x: int = 10
).
12. No Constant Variables by Default
-
No Built-in Constant Keyword: Python does not have a built-in way to define constants. By convention, variables meant to be constant are named using all uppercase letters (e.g.,
PI = 3.14
), but this is not enforced by the language.
13. Variable Lifetime
- Lifetime Depends on Scope: The lifetime of a variable is determined by its scope. Local variables are destroyed after the function exits, whereas global variables persist as long as the program is running.