Python Variables
What are Variables?
Variables are containers for storing data values. In Python, you don't need to declare a variable's type explicitly - Python figures it out automatically!
Creating Variables
To create a variable, you simply assign a value to a name using the = operator:
name = "Alice" age = 25 is_student = True
Variable Types
- String (str): Text data, wrapped in quotes
- Integer (int): Whole numbers
- Float: Decimal numbers
- Boolean (bool): True or False values
Interactive Example
Try editing the code below and click "Run" to see the output:
Initializing Python environment...
Output
(No output yet)
Naming Rules
- Variable names must start with a letter or underscore
- Can contain letters, numbers, and underscores
- Are case-sensitive (
nameandNameare different) - Cannot use Python keywords (like
if,for,while)
# Valid variable names user_name = "Alice" age1 = 25 _private = True # Invalid variable names (don't use these!) # 1name = "Alice" # Can't start with a number # user-name = "Alice" # Can't use hyphens # if = 5 # Can't use keywords