Data Types — int, float, str, bool, None

ডেটা টাইপ — int, float, str, bool, None — Python-এর মৌলিক ভাষা

Read: ~28 min Intermediate 5 practice problems Live code runner

1. Everything Is an Object

In Python, every value — a number, a piece of text, a boolean, even None — is an object. Every object has a type. The built-in function type() reveals it. You will meet five fundamental types in this module: int, float, str, bool, and NoneType.

Python-এ প্রতিটি মান — সংখ্যা, string, bool, এমনকি None — একটি object। প্রতিটি object-এর একটি type আছে। type() function দিয়ে তা জানা যায়। এই module-এ আপনি ৫টি মৌলিক type শিখবেন।
types.py
print(type(42))
print(type(3.14))
print(type("hello"))
print(type(True))
print(type(None))

2. int — The Unbounded Integer

A Python int has no maximum. In C, a 32-bit integer overflows at 2,147,483,647. In Python, you can compute 2 ** 1000 instantly — it simply allocates more memory. This single design choice makes Python a favorite for cryptography, number theory, and scientific computing.

ints.py
a = 42
b = 2 ** 200           # absurdly large — no problem
c = 0b1010             # binary literal = 10
d = 0o17               # octal literal = 15
e = 0xff               # hex literal = 255

print(a, b)
print(f"binary={c}, octal={d}, hex={e}")
print(type(a), type(b))
Python-এর int-এর কোনো সর্বোচ্চ সীমা নেই। C-তে ৩২-বিট integer ২১৪৭৪৮৩৬৪৭-এ overflow করে, কিন্তু Python-এ 2 ** 1000-ও সহজেই গণনা হয়। এটি cryptography ও বৈজ্ঞানিক গণনায় একটি বড় সুবিধা।

3. float — IEEE 754 Double Precision

Floats in Python are 64-bit IEEE 754 doubles — the same standard used in C, Java, and hardware FPUs. They are fast, but they cannot represent every decimal number exactly. This is why 0.1 + 0.2 gives 0.30000000000000004 — a well-known surprise for beginners.

floats.py
print(0.1 + 0.2)                    # 0.30000000000000004
print(0.1 + 0.2 == 0.3)             # False!

# Safe way to compare floats — within a tolerance
import math
print(math.isclose(0.1 + 0.2, 0.3))  # True

# Scientific notation and infinity
print(6.022e23)                      # Avogadro's number
print(float("inf"), float("nan"))
Rule Never use == with floats. Use math.isclose(a, b) instead.

float-এ == ব্যবহার করবেন না। math.isclose(a, b) ব্যবহার করুন।

4. str — Immutable Unicode Text

A str in Python 3 is a sequence of Unicode code points. That means your strings can hold Bangla, Chinese, emoji, and English, all mixed together, without any encoding tricks. Strings are immutable — once created, you cannot change a character. Any method that looks like it modifies a string actually returns a new string.

strings.py
greeting = "হ্যালো, বাংলাদেশ!"
print(greeting)
print(len(greeting))                   # number of code points

name = "Ayesha"
print(name.upper())
print(name.replace("e", "3"))
print(name)                             # unchanged — strings are immutable

# Multi-line strings with triple quotes
poem = """Roses are red,
Violets are blue,
Python is awesome,
And so are you."""
print(poem)

5. bool — Truth Values (and a Sneaky Subclass)

bool has exactly two values: True and False. Surprisingly, bool is a subclass of int — so True + True == 2. That's not a bug; it's a historical feature.

bools.py
print(True + True)                   # 2 — bool is a subclass of int
print(isinstance(True, int))           # True

# Truthiness — Python converts many values to bool automatically
print(bool(0), bool(1))                 # False True
print(bool(""), bool("x"))               # False True
print(bool([]), bool([1]))                # False True
print(bool(None))                       # False
bool হলো int-এর subclass — তাই True + True = 2। Python-এ 0, "", [], None — সবই falsy; বাকি সব truthy। এই convention খুব কাজে লাগে।

6. None — Python's "Nothing"

None is a special object that represents the absence of a value. Functions that don't return anything return None implicitly. Use is None (never == None) to check for it.

none_demo.py
def shout(msg):
    print(msg.upper())
    # no return statement — returns None

result = shout("hello")
print(result)                   # None
print(result is None)           # True — the canonical check

7. Type Conversion (type casting)

Convert between types using the type's constructor: int(x), float(x), str(x), bool(x). If the conversion makes no sense, Python raises a ValueError.

conversions.py
print(int("42"))            # 42
print(int(3.9))             # 3 — truncates toward zero
print(float("3.14"))        # 3.14
print(str(255))              # "255"
print(bool(""))             # False

# Mix string and int — error without conversion
age = 25
print("Age is " + str(age))

8. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
intArbitrary-precision signed integer.যেকোনো বড় পূর্ণসংখ্যা।
float64-bit IEEE 754 double precision number.৬৪-বিট দশমিক সংখ্যা।
strImmutable sequence of Unicode code points.পরিবর্তনযোগ্য-নয় Unicode টেক্সট।
boolTrue or False — subclass of int.True/False — int-এর subclass।
NoneThe unique object representing "no value".কোনো মান নেই — বিশেষ object।
ImmutableAn object whose state cannot change after creation.তৈরি হওয়ার পরে পরিবর্তন করা যায় না।

9. Practice Problems

  1. Write a program that computes and prints 2 to the power 300.
    ২-এর ৩০০ power গণনা করে প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    a1.py
    print(2 ** 300)

    Python-এ কোনো overflow নেই — যে কোনো বড় সংখ্যাই সহজে হ্যান্ডল করা যায়।

  2. Demonstrate that 0.1 + 0.2 == 0.3 is False in Python. Then show the correct way to compare.
    দেখান যে 0.1 + 0.2 == 0.3 Python-এ False। তারপর সঠিক তুলনার উপায় দেখান।
    ✨ Show Answer (উত্তর দেখুন)
    a2.py
    import math
    
    print(0.1 + 0.2)
    print(0.1 + 0.2 == 0.3)
    print(math.isclose(0.1 + 0.2, 0.3))
  3. Write a program that reverses a string using slicing (we'll cover slicing deeply later — for now, use s[::-1]).
    slicing দিয়ে একটি string উল্টে প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    a3.py
    s = "Bangladesh"
    print(s[::-1])
  4. Explain why bool(0) is False but bool("False") is True.
    ব্যাখ্যা করুন কেন bool(0) False কিন্তু bool("False") True।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: bool() does not parse the content of a string — it only asks "is this object empty?". The integer 0 is the numeric zero, and numeric zero is defined as falsy. But "False" is a non-empty string (it has 5 characters). Any non-empty string is truthy, regardless of what the characters spell. If you want to parse the word "False", do it explicitly with a comparison.

    bool() string-এর content পড়ে না — শুধু empty কিনা দেখে। 0 — সংখ্যা zero, falsy। "False" — ৫ অক্ষরের non-empty string, truthy।

  5. Write a program that asks for two numbers and prints the remainder when the first is divided by the second. Assume input is numeric.
    একটি program লিখুন যা দুটি সংখ্যা নিয়ে প্রথমটিকে দ্বিতীয়টি দিয়ে ভাগ করে ভাগশেষ দেখাবে।
    ✨ Show Answer (উত্তর দেখুন)
    a5.py
    # Hard-coded values so the live runner works without stdin
    a = 127
    b = 13
    print(f"{a} mod {b} = {a % b}")

Summary — Module 05

Python's five basic types are int (unbounded), float (IEEE 754 double), str (immutable Unicode), bool (subclass of int), and NoneType (the singleton None). Every value is an object; every object has a type; type() and isinstance() tell you which. Be careful with float equality — always use math.isclose(). Use is None to check for None.

Python-এর ৫টি মৌলিক type: int, float, str, bool, NoneType। প্রতিটি মান object, প্রতিটি object-এর type আছে। float তুলনায় math.isclose, None-এ is None ব্যবহার করুন।

Next Module → Variables, Assignment & Dynamic Typing — নাম, বাইন্ডিং এবং রেফারেন্স মডেল।