Python Data Types

🧱 Python Data Types

Data types define the kind of value stored in a variable. Python is dynamically typed, meaning you don’t need to declare a type—the interpreter detects it automatically.

Example:

x = 10 # int
name = "Raj" # string
price = 99.5 # float

🔍 How to Check Data Type

Use the type() function:

x = 10
print(type(x))

🏷 Categories of Python Data Types

Category Data Type
Text str (String)
Numeric int, float, complex
Boolean bool
Sequence list, tuple, range
Mapping dict
Set set, frozenset
Binary bytes, bytearray, memoryview
None Type NoneType

📌 Python Built-in Data Types Explained


1️⃣ String (str)

Used for text data.

name = "Python"
print(type(name))

Strings can be in single ' ', double " ", or triple quotes """ """.


2️⃣ Numeric Types

🔹 int (Integer)

Whole numbers:

age = 25

🔹 float (Decimal Numbers)

price = 99.99

🔹 complex (Numbers with imaginary part)

z = 3 + 5j

3️⃣ Boolean (bool)

Stores True or False.

isActive = True
print(type(isActive))

4️⃣ List (list)

Ordered, changeable, allows duplicates.

fruits = ["apple", "banana", "orange"]
print(type(fruits))

5️⃣ Tuple (tuple)

Ordered, unchangeable, allows duplicates.

colors = ("red", "green", "blue")

6️⃣ Range (range)

Used in loops.

numbers = range(5)
print(list(numbers))

7️⃣ Dictionary (dict)

Stores data in key: value pairs.

student = {"name": "Vipul", "age": 25}

8️⃣ Set (set)

Unordered, no duplicate items.

nums = {10, 20, 30}

9️⃣ Frozen Set (frozenset)

Immutable (unchangeable) version of set.

fs = frozenset({1, 2, 3})

🔟 Binary Types

Type Description
bytes Immutable binary data
bytearray Mutable binary data
memoryview Access memory of binary objects

Example:

data = bytes([10, 20, 30])
print(type(data))

1️⃣1️⃣ None Type (NoneType)

Represents empty or no value.

x = None
print(type(x))

🧪 Example Program Using All Types

name = "Vipul"
age = 25
height = 5.11
is_student = False
skills = ["Python", "SQL"]
details = {"city": "Surat", "country": "India"}
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
print(type(skills))
print(type(details))


🏁 Summary Table

Data Type Example
str "Hello"
int 10
float 10.5
bool True
list [1,2,3]
tuple (1,2,3)
dict {"a":1}
set {1,2,3}
complex 3+5j
NoneType None

🧠 Practice Tasks

Try these:

  1. Create a variable of each data type and print its type.

  2. Convert integer to float and float to integer.

  3. Create a list and modify one value.

  4. Create a tuple and try modifying it — notice the error.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *