Python Data Types — Complete Beginner-to-Interview Guide

 

Python Data Types — Complete Beginner-to-Interview Guide

Introduction

Python is a dynamically typed language, meaning:

  • You don’t need to declare data types explicitly

  • The type is decided at runtime

Example:

x = 10 # int x = "Hi" # str

Python provides built-in data types to store and manipulate different kinds of data efficiently.


Categories of Python Data Types

Python data types are commonly grouped into:

  1. Numeric Types

  2. Text Type

  3. Sequence Types

  4. Set Types

  5. Mapping Type

  6. Boolean Type

  7. None Type


1️⃣ Numeric Data Types

a) int (Integer)

Stores whole numbers (positive, negative, zero).

age = 30 count = -5

Retrieve / Use

print(age) print(age + 5)

b) float

Stores decimal numbers.

price = 99.99 temperature = -10.5
print(price) print(price * 2)

c) complex

Stores complex numbers.

z = 3 + 4j print(z.real) print(z.imag)

2️⃣ Text Data Type — str

Stores text (immutable).

name = "Vinod" message = 'Hello Python'

Access characters

print(name[0]) # V print(name[-1]) # d

String operations

print(name.upper()) print(name.lower()) print(len(name))

3️⃣ Sequence Data Types

a) list — Ordered & Mutable

Used when:

  • Data can change

  • Order matters

fruits = ["apple", "banana", "orange"]

Access

print(fruits[0])

Add

fruits.append("mango")

Update

fruits[1] = "grape"

Remove

fruits.remove("apple")

b) tuple — Ordered & Immutable

Used when:

  • Data should not change

coordinates = (10, 20)
print(coordinates[0])

⚠️ Cannot modify:

# coordinates[0] = 50 ❌ error

c) range

Used for sequences of numbers.

nums = range(1, 6)
for i in nums: print(i)

4️⃣ Set Data Types

a) set — Unordered & Unique

Used when:

  • You want unique values

  • Order doesn’t matter

ids = {1, 2, 3, 3, 4} print(ids) # duplicates removed

Add / Remove

ids.add(5) ids.remove(2)

b) frozenset — Immutable Set

fs = frozenset([1, 2, 3])

Cannot modify:

# fs.add(4) ❌

5️⃣ Mapping Data Type — dict

Stores key–value pairs.

user = { "name": "Vinod", "age": 35, "city": "Cupertino" }

Access

print(user["name"]) print(user.get("age"))

Add / Update

user["country"] = "USA" user["age"] = 36

Remove

del user["city"]

Iterate

for key, value in user.items(): print(key, value)

6️⃣ Boolean Data Type — bool

Stores True or False.

is_active = True is_admin = False

Used in conditions:

if is_active: print("User is active")

7️⃣ None Data Type — None

Represents absence of value.

result = None
if result is None: print("No result found")

8️⃣ Checking Data Types

Use type():

x = 10 print(type(x))

Use isinstance() (recommended):

isinstance(x, int)

9️⃣ Mutable vs Immutable (Very Important)

TypeMutable
int, float, str
list, dict, set
tuple, frozenset

Example:

a = 10 b = a b = 20 # a is still 10
lst1 = [1, 2] lst2 = lst1 lst2.append(3) # lst1 is also changed

🔟 Common Interview Examples

Store user info

user = {"name": "Vinod", "age": 35}

Store multiple users

users = [ {"name": "A", "age": 30}, {"name": "B", "age": 25} ]

Unique values

unique_ids = set([1, 2, 2, 3])

1️⃣1️⃣ Summary Table

Data TypeUse Case
intWhole numbers
floatDecimal values
strText
listOrdered, changeable data
tupleFixed data
setUnique values
dictKey–value data
boolConditions
NoneNo value

1️⃣2️⃣ Interview One-Line Summary ⭐

Python data types define how data is stored and manipulated, ranging from numeric and text types to collections like lists, sets, and dictionaries, with mutable and immutable behaviors.

No comments:

Post a Comment

Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts