PythonVariables and Basic Databeginner22 min read

Data Types in Python

Recognize common Python types, inspect values with type(), and understand mutable versus immutable objects.

In this lesson, you will learn:
  • Identify int, float, str, bool, and NoneType values.
  • Use type() to inspect an object’s type.
  • Distinguish a name from the object it refers to.
  • Recognize basic mutable and immutable types.
  • Avoid confusing equality with identity.

1. Integers and floating-point numbers

int represents integers such as -4, 0, and 120. Python integers can represent arbitrarily large integers subject to available memory.

float represents floating-point numbers such as 3.5 or -0.25. Floating-point values use a finite binary representation, so some decimal fractions cannot be represented exactly.

Integer arithmetic

Integer arithmetic
python
items = 4
price = 25
total = items * price
print(total)
print(type(total))
Output
100 <class 'int'>

Both operands are integers, and their product is an integer. type(total) returns the type object for the value referred to by total.

Key Point

For money calculations requiring exact decimal arithmetic, consider decimal.Decimal rather than relying on binary floating-point.

Floating-point values

Floating-point values
python
temperature = 36.5
print(temperature)
print(type(temperature))
Output
36.5 <class 'float'>

The literal 36.5 creates a float object. A float is useful for fractional values, but should not be assumed to store every decimal fraction exactly.

2. Strings

str represents text. String literals are commonly written inside single or double quotation marks. The quotation marks mark the string in source code; they are not part of the value itself.

Strings are immutable. Operations such as concatenation produce a new string value rather than modifying an existing string in place.

Working with strings

Working with strings
python
first_name = "Mira"
message = "Hello, " + first_name
print(message)
print(type(message))
Output
Hello, Mira <class 'str'>

The + operator concatenates two strings. The result is a new string bound to message.

3. Booleans

bool has two values: True and False. They are commonly produced by comparisons and used in conditions. Capitalization matters: the Boolean constants are True and False, not true and false.

A comparison produces a Boolean

A comparison produces a Boolean
python
age = 20
can_register = age >= 18
print(can_register)
print(type(can_register))
Output
True <class 'bool'>

The comparison age >= 18 evaluates to True. That Boolean result is assigned to can_register.

4. None and NoneType

None is a special singleton object commonly used to represent the absence of a value or a missing result. Its type is NoneType. None is not the same as 0, False, or an empty string.

When checking specifically for None, use is None or is not None. This is a standard use of identity comparison because None is a singleton.

A missing value

A missing value
python
result = None
print(result)
print(type(result))
print(result is None)
Output
None <class 'NoneType'> True

The name result refers to the None singleton. type(result) displays NoneType. is None checks whether result refers to that exact singleton.

5. Inspecting types with type()

Call type(value) to inspect the type of an object. The result is a type object, and printing it usually displays a form such as <class 'int'>. In beginner code, this is a useful way to explore values.

A name does not permanently belong to one type. If you reassign the name to a different object, type(name) can produce a different result.

One name, different object types

One name, different object types
python
value = 10
print(type(value))
value = "ten"
print(type(value))
Output
<class 'int'> <class 'str'>

Initially value refers to an integer. After reassignment, value refers to a string.

6. Mutable and immutable objects

A mutable object can be changed after it is created. A list is a common mutable type. An immutable object cannot be changed in place; int, float, bool, str, and None are immutable.

This distinction matters when two names refer to the same object. Mutating a shared list is visible through both names, but rebinding one name does not change the other name’s binding.

List mutation

List mutation
python
a = [1, 2]
b = a
b.append(3)
print(a)
print(b)
Output
[1, 2, 3] [1, 2, 3]

Both names refer to the same list. append changes the existing list object.

String immutability

String immutability
python
word = "cat"
word = word + "s"
print(word)
Output
cats

Concatenation creates a new string value. The name word is rebound to the resulting string; the original string was not modified.

7. Equality and identity

The == operator asks whether two objects compare equal in value. The is operator asks whether two references point to the same object. Do not use is as a general replacement for ==.

Use == for ordinary value comparisons. Use is None to check for None. Do not rely on implementation-specific object reuse, such as whether two equal small integers happen to be the same object.

Equal values, distinct lists

Equal values, distinct lists
python
first = [1, 2]
second = [1, 2]
print(first == second)
print(first is second)
Output
True False

The lists contain equal values, so == is True. They are separate list objects, so is is False.

Common Pitfalls & Mistakes to Avoid

#1 Saying a variable has a permanent type.

Explanation: A name can be rebound to an object of another type; inspect the current object with type(name).

#2 Assuming 0, False, None, and an empty string mean the same thing.

Explanation: They are different values and types, even though several are false in a Boolean context.

#3 Using is to compare ordinary values.

Explanation: Use == for value equality. Use is when identity is what you need, especially value is None.

#4 Assuming a list assignment copies the list.

Explanation: Assignment binds another name to the same list unless a copy is explicitly made.

Practice Questions

1

Create variables count = 7, ratio = 2.5, label = "ready", and active = True. Print the type of each value on its own line.

2

Create two separate lists containing 4 and 5. Print whether they are equal and whether they are identical objects.

Data Types in Python Knowledge Check

1. What is the type of the value 4.0?

2. Which operator should usually be used to compare two values for equality?

3. What does type(None) return in Python?

Technical & Placement Interview Questions

Key Takeaways & Summary

  • Common built-in types include int, float, str, bool, and NoneType.
  • Use type() to inspect an object’s type.
  • Python names can be rebound to objects of different types.
  • Lists are mutable; strings and numeric scalar types are immutable.
  • Use == for value equality and is None for a None check.

Finished reading this lesson?

Track your course progress and update your learning dashboard.

Related Topics & Next Steps

Strengthen your programming fundamentals with these related lessons: