By WebNest Studio Engineering Team
Python Tutorial
Python Data Types
Every value in Python has a type, and that type determines what operations are valid on it and how it behaves. Python's built-in data types cover whole numbers, decimal numbers, text, true/false logic, and the special "no value" marker, along with more advanced numeric and collection types.
Because Python is dynamically typed, you rarely declare types explicitly, but understanding exactly which type a value has is essential for avoiding subtle bugs — especially when mixing numbers and text, or comparing values of different types.
Why it matters
What you are allowed to do with a value depends on its type: you can add two numbers, but adding a number to text is an error. Many beginner bugs come from having a string where a number was expected. Being able to check a type quickly, and knowing the handful of built-in types, lets you read error messages and fix them.
Core Built-in Types
Python provides several fundamental data types that cover the vast majority of everyday programming needs.
int— whole numbers of arbitrary size, e.g. 42, -7, 1_000_000 (Python ints do not overflow like fixed-size integers in other languages).float— decimal (floating-point) numbers, e.g. 3.14, -0.5, 2.0.str— text, written in single, double, or triple quotes, e.g. "hello", 'Python'.bool— the values True or False, used for logical conditions.None— a special singleton value representing "no value" or "nothing here," similar to null in other languages.complex— numbers with a real and imaginary part, written like 3+4j, used mainly in scientific computing.
Checking Types at Runtime
Because types are not declared, Python gives you two built-in tools to inspect them. type(value) returns the exact type of a value, which is useful for debugging and quick inspection. isinstance(value, Type) checks whether a value is an instance of a given type (or one of several types passed as a tuple), and is generally preferred in real code because it also correctly recognizes subclasses.
Implicit and Explicit Conversion
Python automatically converts (promotes) an int to a float when the two are combined in an arithmetic expression, such as 3 + 2.0 producing 5.0. Converting between unrelated types — such as text to a number — must be done explicitly using functions like int(), float(), and str(), and will raise a ValueError if the text cannot be interpreted as that type.
Example
Inspecting types with type() and isinstance()
age = 25
price = 19.99
name = "Alice"
is_member = True
address = None
print(type(age), type(price), type(name), type(is_member), type(address))
print(isinstance(age, int))
print(isinstance(price, (int, float)))
print(int("42") + 8)
print(str(3.14) + " is pi rounded")
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'>
True
True
50
3.14 is pi rounded
Common Mistakes
- Trying to combine a string and a number with + directly (e.g. "Age: " + 25), which raises a TypeError — the number must be converted with str() first.
- Using type(x) == int for type checks instead of isinstance(x, int), which fails to account for subclasses like bool (which is technically a subclass of int).
- Assuming int() on a non-numeric string like int("abc") will return 0 instead of raising a ValueError.
- Confusing None with False, 0, or an empty string — None specifically means "no value," and while all of these are "falsy," they are not equal to None.
Exercise
Print the name of the type of each value in the list, one per line.
Expected output: int, float, str, bool, NoneType (one per line)
values = [42, 3.14, "hi", True, None]
for value in values:
# TODO: print the name of the value's type
pass
Show hints
type(value)returns the type object.- The plain name of a type is in its
__name__attribute.
Show solution
values = [42, 3.14, "hi", True, None]
for value in values:
print(type(value).__name__)
Quick quiz
1. What is the type of 1 + 2.0?
- A. float
- B. int
- C. str
- D. It raises an error
Show answer
A. When an int and a float are combined, the result is a float.
2. What does isinstance(True, int) return?
- A. It raises an error
- B. False
- C. None
- D. True
Show answer
D. bool is a subclass of int, and True behaves as 1.
3. What does None == False evaluate to?
- A. False
- B. True
- C. None
- D. It raises an error
Show answer
A. None is falsy in a condition, but it is a distinct value and is equal only to itself.
Interview questions
What is the difference between type() and isinstance()?
Show answer
type(x) returns the exact class of the object. isinstance(x, SomeClass) returns true if the object is of that class or of any subclass, and it can test several classes at once with a tuple. isinstance is preferred for checks because code that works for a class should normally work for its subclasses too.
Which built-in types are mutable and which are immutable?
Show answer
Immutable types cannot be changed after creation: int, float, bool, str, tuple and frozenset. Mutable types can be changed in place: list, dict and set. The distinction matters when objects are shared between names or passed to functions, and only immutable objects can be dictionary keys.
Key Points to Remember
- Core built-in types: int, float, str, bool, None, and complex.
- type() reports the exact type; isinstance() checks membership, including subclasses, and is preferred for type checks.
- Numeric types mix automatically in expressions (int + float -> float); other conversions must be explicit.
- None is a distinct singleton value meaning "no value," not the same as 0, False, or an empty string.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering