What is a Variable?
A variable is a reserved memory location to store values. In Python, you don't need to declare the type of a variable explicitly; it is created the moment you assign a value to it. This is known as Dynamic Typing.
Core Data Types
Python has several built-in data types categorized into Numbers, Sequences, and Mappings.
- int: Whole numbers, positive or negative, without decimals (e.g.,
10,-5). - float: Floating point numbers, containing one or more decimals (e.g.,
10.5,-0.1). - str: Textual data enclosed in single or double quotes (e.g.,
'Hello',"Python"). - bool: Represents logical values:
TrueorFalse.
Naming Conventions
Python follows the snake_case convention for variable names. Names must start with a letter or underscore and are case-sensitive.
Dynamic vs. Static Typing
Unlike Dart or Java, Python allows a variable to change its type during execution.
| Feature | Python (Dynamic) | Dart/C++ (Static) |
|---|---|---|
| Declaration | x = 5 | int x = 5; |
| Type Change | x = 'Hi' (Valid) | x = 'Hi' (Error) |
| Flexibility | High | Low |
| Safety | Low (Runtime errors) | High (Compile errors) |
Checking Types
You can use the built-in type() function to find out the data type of any variable.