Variables & Data Types
Learn the fundamental building blocks of Python.
Topic 1: What Are Variables in Python?
A variable in Python is a name used to store a value in memory. You can think of a variable as a labelled container that holds data needed for calculations, logic, and program execution. Python handles memory allocation automatically, meaning you do not need to specify where or how the value will be stored.
Python is a dynamically typed language, which means variable types are determined at runtime, based on the value assigned. This flexibility makes Python highly convenient for rapid development, especially in machine learning, automation, and web development.
variable_name = value
# Example:
x = 10
name = "Vinay"
pi = 3.14
is_active = True
In Python, everything is an object. When we write x = 10, an integer object is created behind the
scenes and x references that object. You can reassign different types to the same variable:
x = 10
x = "Now I store text"
This feature is powerful, but it requires developers to stay aware of how types change during execution to avoid errors.
Topic 2: Variable Naming Rules & Best Practices
Well-structured variable names make your code readable and maintainable. Python follows simple naming rules:
- Must start with a letter or underscore.
- Cannot start with a number.
- Can contain only letters, digits, and underscores.
- Names are case-sensitive.
- Must not use Python keywords.
# Valid Variables
user_name = "Alex"
price = 250
_name = "Internal"
# Invalid
2name = "Hi" # ❌ starts with digit
class = "Python" # ❌ keyword
Best Practices
- Use descriptive names like
total_amount, notta. - Follow snake_case formatting.
- Avoid unnecessary abbreviations.
Topic 3: Python Data Types
A data type defines the kind of value stored in a variable and the operations allowed on that value. Python assigns a data type dynamically based on the assigned value.
Common Primitive Types
name = "Python" # String
age = 21 # Integer
rating = 4.5 # Float
is_active = True # Boolean
Collection Types
fruits = ["apple", "banana"] # list
coordinates = (10, 20) # tuple
unique_numbers = {1, 2, 3} # set
student = {"name": "Teja", "score": 95} # dictionary
print(type(10)) #
print(type("Hello")) #
Topic 4: Type Casting & Printing Output
Type casting means converting a variable from one type to another. This is useful when receiving user input, performing numeric operations, or formatting results.
price_str = "150"
price_int = int(price_str)
print(price_int + 50) # 200
Printing Output
name = "Vinay"
age = 25
print("Name:", name)
print(f"My name is {name} and I am {age} years old")
Formatting output is especially useful in dashboards, logs, reports, and ML results.