Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

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.

Python Package Managers — pip, pip3, poetry, uv

 

Python Package Managers — pip, pip3, poetry, uv

A Complete Guide for Backend Engineers (2025)

Python has multiple package managers, and developers often wonder:

✅ What’s the difference between pip, pip3, poetry, and uv?
✅ When should I use which?
✅ What are the advantages for backend engineering?

This guide gives a crisp, interview-friendly, backend-engineer-focused explanation.


✅ 1. pip — The Classic Python Package Manager

✅ What is pip?

pip is the default package manager for Python.
It installs packages from PyPI, manages dependencies, and works with virtual environments.

✅ Why do we need pip?

  • Install third-party libraries (fastapi, pandas, requests)

  • Upgrade packages

  • List/remove dependencies

✅ Common commands

pip install requests pip list pip uninstall requests pip install -r requirements.txt

✅ Limitations

  • No dependency resolution (older versions installed conflicting packages)

  • Requires manual virtual environment setup

  • Not deterministic builds

  • Dependency conflicts common in large projects

Best for: Small projects, simple scripts, educational usage.


✅ 2. pip3 — Just pip for Python 3

✅ Why pip vs pip3 exists?

Some systems (especially Linux/macOS) have both Python 2 and Python 3 installed.

Therefore:

  • pip → installs for Python 2

  • pip3 → installs for Python 3

✅ Today (2025)

Python 2 is dead → pip3 is usually the same as pip.

Best for: Linux systems where Python 2 still exists (rare).


✅ 3. Poetry — Modern Dependency & Project Manager

✅ What is Poetry?

A full project & dependency manager that replaces:

pip
virtualenv
setup.py
requirements.txt

✅ Why Poetry?

  • Creates isolated virtual environments automatically

  • Deterministic lock files (poetry.lock)

  • Semantic versioning out of the box

  • Publishing packages is easier

  • Great for large backend projects

✅ Common commands

poetry new myapp poetry add fastapi poetry install poetry run uvicorn main:app

Best for: Production apps, large backend systems, microservices.

✅ Weakness

  • Slower than new modern tools like uv

  • More complex for beginners


✅ 4. uv — The Fastest Python Package Manager (2025) 🔥

Created by Astral, known for being ultra-fast (100x faster than pip).

✅ Why uv is exploding in popularity?

  • Replaces pip

  • Replaces venv

  • Replaces virtualenv

  • Replaces poetry (some parts)

  • Built in Rust → ultra-fast

  • Creates projects instantly

  • Perfect for backend engineers, ML engineers, API developers


✅ 4.1 Creating a Project with uv

✅ Correct workflow (YOUR CORRECTED VERSION)

uv init fastapi-app cd fastapi-app uv add fastapi uvicorn uv run main.py

Note: uv init creates main.py, not app.py.


✅ 4.2 Folder Structure uv creates

fastapi-app/ │── pyproject.toml ✅ Project metadata │── uv.lock ✅ Locked dependencies │── main.py ✅ Entry point

✅ 4.3 Example FastAPI app created using uv

main.py

from fastapi import FastAPI app = FastAPI() @app.get("/") def home(): return {"message": "Hello from FastAPI using uv!"}

Run it:

uv run main.py

✅ 4.4 Benefits of uv

100× faster than pip & Poetry
✅ Built-in virtual environments
✅ Supports pyproject.toml
✅ Handles dependency resolution
✅ Perfect for Docker + CI/CD
✅ Excellent for FastAPI, ML, API services
✅ Zero-config experience


✅ 5. Comparison Table (Interview Ready)

Featurepippip3poetryuv
SpeedSlowSlowMediumFastest
Virtual envManualManualBuilt-in✅ Built-in
Dependency resolverWeakWeakStrong✅ Strongest
Project creationNoNoYes✅ Yes (uv init)
Lock fileNoNoYes (poetry.lock)✅ uv.lock
Suitable for backendMediumMedium✅ HighVery High
Publish packagesManualManual✅ EasyComing soon
Learning curveLowLowMediumLow

✅ 6. Which One Should Backend Engineers Use?

Use uv → for modern backend development (FastAPI, ML, microservices)

Use Poetry → for enterprise, packaging, ML model deployment

Use pip/pip3 → for tiny scripts or legacy environments


✅ Final Recommendation (2025)

🔥 The future is uv.
It replaces pip, virtualenv, poetry (partially), and is the fastest Python tool available.

If you're starting a new backend service → use uv.

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