Introduction π
Data types tell Python what kind of value a variable holds β text, numbers, lists, booleans, and more. Understanding data types is essential because it helps you store, manage, and manipulate information correctly.
Note
1. What Are Data Types? π€
Data types define the nature of data. Each type supports specific operations. For example, you can add integers, but you cannot add an integer to a string without converting types.
2. Basic Built-in Data Types π
| Type | Class Name | Example |
|---|---|---|
| String | str | "hello" |
| Integer | int | 42 |
| Float | float | 3.14 |
| Boolean | bool | True / False |
| List | list | [1, 2, 3] |
| Tuple | tuple | (1, 2, 3) |
| Dictionary | dict | {"name": "Sathish"} |
| Set | set | {1, 2, 3} |
3. String (str) β¨
Strings represent text. They must be enclosed in quotes β single, double, or triple quotes.
string_example.py
name = "Sathish"
msg = 'Hello Python'
multiline = """This is
a multi-line string"""
print(name)
print(multiline)Note
4. Numbers βββοΈβ
Integer (int)
int_example.py
age = 25
points = -100
print(age, points)Float (float)
float_example.py
pi = 3.14159
temperature = 36.6
print(pi, temperature)Complex (complex)
complex_example.py
z = 2 + 3j
print(z.real, z.imag)5. Boolean (bool) π₯
Boolean values represent truth values.
bool_example.py
is_active = True
is_admin = False
print(is_active, is_admin)
print(type(is_active))Note
6. List (list) π¦
Lists are ordered, changeable, and allow duplicate values. They are one of the most used data structures in Python.
list_example.py
numbers = [10, 20, 30, 40]
mixed = ["Sathish", 25, True]
print(numbers)
print(mixed)Note
7. Tuple (tuple) π
Tuples are ordered but immutable β once created, they cannot be changed.
tuple_example.py
coordinates = (10.5, 20.3)
print(coordinates)Note
8. Dictionary (dict) ποΈ
Dictionaries store data as keyβvalue pairs.
dict_example.py
person = {
"name": "Sathish",
"age": 25,
"is_active": True
}
print(person["name"])
print(person.get("age"))Note
9. Set (set) π―
Sets are unordered collections of unique items β duplicates are automatically removed.
set_example.py
unique_numbers = {1, 2, 3, 3, 2}
print(unique_numbers) # Output: {1, 2, 3}Note
10. Checking Data Type π§ͺ
Use the type() function to check what data type a variable holds.
type_check.py
x = 100
print(type(x))
y = "Hello"
print(type(y))11. Type Casting (Converting Types) π
type_cast.py
x = "10"
y = int(x) # convert string to int
z = float(x) # convert string to float
print(y, z)Note
Conclusion π
You now understand all major Python data types. Ready for the next step? Ask for a tutorial on operators, type casting, expressions, loops, or functions!