Python Data Types Explained – A Beginner’s Guide
Python tutorial · PySpark.in
Understanding data types is one of the first and most essential steps in learning Python. Every piece of data in Python—whether it’s a number, text, or even a collection of values—belongs to a specific data type. Knowing these types and how to work with them will set a strong foundation for data science, machine learning, and general programming.
1. Numbers
Python supports different types of numbers:
int → Integers (whole numbers, positive or negative).
age = 30 # intfloat → Decimal numbers.
pi = 3.14159 # floatcomplex → Numbers with a real and imaginary part.
z = 2 + 3j # complex number
Use case in Data Science: Representing numerical values, calculations, and matrix operations.
2. Strings
Strings are sequences of characters, enclosed in single, double, or triple quotes.
name = "Python"
quote = 'Data Science is fun!'
multi_line = """This is a multi-line string"""Common operations:
Concatenation →
'Hello' + ' World'Repetition →
'Hi' * 3Indexing →
word[0]Slicing →
word[1:4]
Use case in Data Science: Text analysis, Natural Language Processing (NLP).
3. Boolean (bool)
Represents True or False values.
is_active = True
is_data_clean = FalseThey are often results of comparisons:
print(10 > 5) # True
print(2 == 3) # FalseUse case in Data Science: Filtering datasets, applying conditions in analysis.
4. Lists
Lists are ordered, mutable collections of items.
numbers = [1, 2, 3, 4]
mixed = [1, "Python", 3.14, True]Common operations:
Indexing:
numbers[0]Appending:
numbers.append(5)Removing:
numbers.remove(2)
Use case in Data Science: Storing records, iterating over datasets.
5. Tuples
Tuples are ordered, immutable collections.
coordinates = (10.5, 20.3)Cannot be changed once created.
Faster than lists.
Use case: Representing fixed data like geographic coordinates.
6. Sets
Sets are unordered collections of unique elements.
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")No duplicates allowed.
Useful for set operations: union, intersection, difference.
Use case: Removing duplicates from datasets.
7. Dictionaries
Dictionaries are key-value pairs.
student = { "name": "Alice", "age": 24, "grade": "A"
}
print(student["name"]) # AliceKeys must be unique.
Values can be any data type.
👉 Use case: Storing dataset metadata, mappings.
8. None Type
None represents the absence of a value.
result = None
if result is None: print("No value assigned yet")👉 Use case: Handling missing values in datasets.
9. Type Conversion (Casting)
You can convert between data types:
x = int(3.5) # 3
y = float(5) # 5.0
z = str(10) # "10"10. Checking Data Types
Use the type() function:
print(type(10)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hi")) # <class 'str'>Final Thoughts
Mastering Python data types is the first step toward working with data effectively. Once you are comfortable with these, you’ll find it easier to manipulate datasets, build models, and solve complex problems in Data Science and Machine Learning.
Pro Tip: Practice by creating small programs that use each data type—for example, a student record system (dictionary), shopping cart (list), or text analyzer (strings).
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- test
- test
All tutorials · Try the free PySpark compiler · Practice challenges