Variables & data types

A variable is a name that refers to a value. Python is dynamically typed, so you never declare a type — the value carries its own type, and a name can be pointed at something new at any time.

age = 25            # int
price = 499.99      # float
city = "Jaipur"     # str
is_member = True    # bool
gst_rate = None     # NoneType: "no value yet"

Names may contain letters, digits and underscores, must not start with a digit, and are case sensitive (Total and total are different names). Convention is snake_case for variables and UPPER_CASE for constants.

The core scalar types

Type Example Notes
int 25, 1_00_000 Unlimited size; underscores are ignored
float 499.99 Binary approximation, so 0.1 + 0.2 != 0.3
str "Jaipur" Immutable text, Unicode by default
bool True / False A subclass of int: True + True == 2
NoneType None The deliberate absence of a value

type(x) shows a value's type and isinstance(x, int) tests it. For money in billing code, decimal.Decimal("499.99") avoids float rounding surprises.

Collections

Type Example Ordered Mutable Duplicates
list [1, 2, 2] ✅ ✅ ✅
tuple (1, 2) ✅ ❌ ✅
set {1, 2} ❌ ✅ ❌
dict {"a": 1} ✅ (insertion) ✅ keys unique
skills = ["python", "sql"]
skills.append("pandas")
print(len(skills), skills[0], skills[-1])    # 3 python pandas

student = {"name": "Asha", "marks": 91}
student["city"] = "Indore"
print(student["name"], student.get("grade", "N/A"))   # Asha N/A

location = (26.91, 75.79)        # tuple: a fixed pair, and usable as a dict key
cities = {"Delhi", "Pune", "Delhi"}
print(cities)                    # {'Delhi', 'Pune'} - duplicates dropped

Choose a list when order matters and items change, a tuple for a fixed record, a set for membership tests and de-duplication, and a dict when every value needs a label.

Mutability matters

a = [1, 2]
b = a          # the same list with two names
b.append(3)
print(a)       # [1, 2, 3]

c = a.copy()   # an independent copy
c.append(4)
print(a, c)    # [1, 2, 3] [1, 2, 3, 4]

Type conversion

int("42") + 8        # 50
float("499.99")      # 499.99
str(3.14)            # '3.14'
list("abc")          # ['a', 'b', 'c']
int(7 / 2)           # 3  - int() truncates towards zero

Values from input() and from CSV files are always strings, so convert before doing arithmetic.

qty = int(input("Boxes of laddu: "))
total = qty * 250
print(f"Bill: Rs. {total:,}")

Common mistakes

  • Writing "10" + 5 and getting a TypeError; convert with int() first.
  • Assuming b = a copies a list — it does not; use a.copy() or list(a).
  • Comparing floats with ==; use round(x, 2) == round(y, 2) or math.isclose.
  • Overwriting builtins such as list, sum or str by using them as variable names.

Practice

Build a dict for one sweet-shop order with the keys item, qty and price_per_unit. Read the quantity from input(), convert it, add a total key, and print a formatted bill line. Then store three such orders in a list and print the grand total with thousands separators.

B

Written by Bablu Kumar Jha

Founder, BeeProton AI · Mumbai, Maharashtra, India