Variables are fundamental in programming. In Python, you can assign values to variables using the =
operator. Here's how it works:
Basic Syntax
variable_name = value
- Example:
x = 10 name = "Python" is_active = True
Data Types
Python automatically infers the data type based on the value:
- Integer:
age = 25
- String:
message = "Hello, world!"
- Boolean:
flag = False
Variable Rules
- Naming: Use letters, underscores (
_
), or numbers (but not starting with a number).
✅valid_var
❌1invalid_var
- Scope: Variables defined inside a function are local to that function.
Practice
Try modifying these variables in your code:
count = 5
count += 1 # Now count is 6
For more details on data types, visit our Python Data Types Tutorial.