What are Variables in Python?
A variable is a named storage location in memory used to store data.
In the real world, we identify humans by their names; similarly, a variable name in Python is used to identify a value stored in memory.
Storing a value in a variable allows us to reuse, modify, and process data efficiently in a program.

Variable Declaration:
Variable declaration refers to creating a variable and assigning a value to it. In Python, you do not need to declare the type of a variable explicitly; the interpreter determines the type based on the value assigned.
Syntax:
variable_name = value
- variable_name: The name of the variable.
- = (assignment operator): Used to assign a value to the variable.
- value: The data stored in the variable.

Example:
x = 10 # Integer
name = "Alice" # String
price = 99.99 # Float
is_active = True # Boolean

Rules for Naming Variables:
Variable names must start with a letter or an underscore (_).
Variable names cannot start with a number.
The name can contain letters, numbers, and underscores.
Variable names are case-sensitive (age and Age are different).
Avoid using Python reserved keywords (e.g., class, def, import).
Key Points About Variable Declaration in Python:
1. No Explicit Type Declaration:
Python automatically determines the type based on the assigned value.
You can check the type using type():
x = 10
print(type(x)) # Output: <class 'int'>
2. Variables are Mutable:
You can reassign values to a variable at any time.
x = 10
x = "Hello"
print(x) # Output: Hello
3. Multiple Assignments:
You can assign multiple variables in one line.
a, b, c = 1, 2, 3 #Parallel Assignment
Assign the same value to multiple variables.
x = y = z = 10 #Chained Assignment
What are the Data Types in Python?
Data types are the classification or categorization of data items. A data type defines the characteristics of the value and the operations that can be performed on it. Python provides a rich set of built-in data types such as integers, floating-point numbers, strings, lists, tuples, dictionaries, sets, and booleans to handle different kinds of data efficiently.
Python has several built-in data types:
1. Numeric Types
Integer (int): Whole numbers (e.g., 10, -5).
Floating-point (float): Decimal numbers (e.g., 3.14, -0.001).
Complex (complex): Numbers with a real and imaginary part (e.g., 2 + 3j).
Example:
a= 42
b= 3.14
c= 1 + 2j
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
2. String Type
String (str): A sequence of characters enclosed in quotes.
Example:
text = "Hello, Python!"
print(type(text)) # <class 'str'>
String Operations:
name = "Alice"
print(name.upper()) # "ALICE"
print(name.lower()) # "alice"
print(name[0]) # "A" (Indexing)
print(name[1:4]) # "lic" (Slicing)
3. Boolean Type
Boolean (bool): Represents True or False values.
Example:
is_valid = True
is_empty = False
print(type(is_valid)) # <class 'bool'>
4. Sequence Types
List (list): Ordered, mutable(changeable) collection.
Tuple (tuple): Ordered, immutable collection.
Range (range): Sequence of numbers.
my_list = [1, 2, 3, "Python"] # Mutable
my_tuple = (10, 20, 30) # Immutable
my_range = range(5) # 0 to 4
5. Set Types
- Set (set): Unordered collection of unique items.
FrozenSet (frozenset): Immutable version of a set.
my_set = {1, 2, 3, 3, 4} # {1, 2, 3, 4}
my_frozenset = frozenset({1, 2, 3})
6. Dictionary Type
Dictionary (dict): Key-value pairs.
person = {"name": "Alice", "age": 25, "city": "New York"}
Summary Table
Data Type | Description | Example |
int | Integer values | 42 |
float | Decimal numbers | 3.14 |
complex | Complex numbers | 1+2j |
str | Text data | ‘Hello’ |
bool | Boolean values | True/False |
list | Ordered, mutable collection | [1,2,3] |
tuple | Ordered, immutable collection | (1,2,3) |
set | Unordered, unique elements | {1,2,3} |
frozenset | Immutable set | frozenset({1,2,3}) |
dict | Key-value pairs | {“name”: “Alice”} |
Type Conversion
Type conversion refers to changing a variable’s data type from one to another. Python provides two types of type conversion:
Implicit Type Conversion (Automatic)
Explicit Type Conversion (Type Casting)
1. Implicit Type Conversion (Automatic)
Python automatically converts one data type to another when no data loss occurs.
🔹 Example: Converting int to float
num_int = 10 # Integer
num_float = num_int + 5.5 # Python converts int to float automatically
print(num_float) # Output: 15.5
print(type(num_float)) # Output: <class 'float'>
Python performs implicit conversion only when there is no risk of losing data.
2. Explicit Type Conversion (Type Casting)
When Python does not automatically convert a type, we need to manually convert it using built-in functions like int(), float(), str(), list(), tuple(), etc.
Common Type Casting Functions in Python
Function | Converts To |
int(x) | Integer |
float(x) | Floating-point number |
str(x) | String |
list(x) | List |
tuple(x) | Tuple |
set(x) | Set |
dict(x) | Dictionary |
Examples of Explicit Type Conversion:
x = 10 # int
y = str(x) # Convert to string
z = float(x) # Convert to float
print(type(y)) # Output: <class 'str'>
print(type(z)) # Output: <class 'float'>
Common Conversions:
int(“100”) → 100
float(10) → 10.0
bool(0) → False
list(“hello”) → [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
Be careful! Not all values can be converted (e.g., “Hello” to int will fail).
Understanding Python variables and data types is fundamental for writing efficient code. Python dynamically assigns types, making it flexible and easy to use.