TutorialsPythonVariables and Data Types

Variables and Data Types

Python variables, int, float, str, bool — the building blocks of every script

Variables store values that your script works with. Unlike Excel cells (A1, B2), Python variables have names you choose — making your code readable and reusable. Python is dynamically typed: you do not declare the type — Python figures it out from the value you assign. The four basic data types you use constantly in data analysis: int (whole numbers), float (decimal numbers), str (text), and bool (True/False). Understanding them prevents type errors — one of the most common Python mistakes for beginners.

Examples

Variables and types
# Assigning variables
sales_amount = 45000        # int
profit_margin = 0.22        # float
region = "Delhi"            # str
is_active = True            # bool

# Check the type
print(type(sales_amount))   # <class 'int'>
print(type(profit_margin))  # <class 'float'>
print(type(region))         # <class 'str'>

# Type conversion
amount_str = str(45000)     # "45000"  (int → str)
amount_int = int("45000")   # 45000   (str → int)
amount_flt = float("45.5")  # 45.5    (str → float)
💡 Variable names are case-sensitive: sales_amount and Sales_Amount are two different variables.
Arithmetic operators
a = 100
b = 30

print(a + b)   # 130  addition
print(a - b)   # 70   subtraction
print(a * b)   # 3000 multiplication
print(a / b)   # 3.33 division (always float)
print(a // b)  # 3    floor division (integer result)
print(a % b)   # 10   modulo (remainder)
print(a ** 2)  # 10000 exponentiation

# Useful for data analysis
revenue = 5000
cost = 3200
profit = revenue - cost          # 1800
margin = (profit / revenue) * 100  # 36.0%
print(f"Margin: {margin:.1f}%")  # "Margin: 36.0%"

Key Points

  • Python variables need no declaration — just assign: x = 10
  • int / int gives float in Python 3: 7 / 2 = 3.5 (not 3)
  • f-strings (f"value is {variable}") are the best way to format output
  • Use snake_case for variable names: total_sales not totalSales
  • None is Python's equivalent of NULL — check with: value is None

Practice Question

What is the result of: 7 // 2 in Python?

Related Topics

String Operations in PythonSlice, split, replace, strip and format text data — essential for cleaning messy datasetsPython ListsStore, access and manipulate collections of data with Python listsDictionaries in PythonKey-value pairs for fast lookups, mappings and structured dataConditionals — if, elif, elseWrite if/elif/else logic to categorise, flag and filter data in Python