Python Data Types

In programming, a data type defines the type of value a variable can store. Python has several built-in data types that are used to store different kinds of data. Getting the Data Type You can use the type() function to find the data type of a variable. Example: Output: Python Built-in Data Types Python has […]

2 mins read Lesson 8 of 43 19% through track

In programming, a data type defines the type of value a variable can store.

Python has several built-in data types that are used to store different kinds of data.

Getting the Data Type

You can use the type() function to find the data type of a variable.

Example:

x = 5

print(type(x))

Output:

<class 'int'>

Python Built-in Data Types

Python has the following data types:

CategoryData Types
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNoneType

String Type

Strings are used to store text.

Example:

name = "John"

print(type(name))

Output:

<class 'str'>

Integer Type

Integers are whole numbers without decimals.

Example:

age = 25

print(type(age))

Output:

<class 'int'>

Float Type

Floats are numbers with decimal points.

Example:

price = 99.99

print(type(price))

Output:

<class 'float'>

Complex Type

Complex numbers contain a real and imaginary part.

Example:

x = 3 + 5j

print(type(x))

Output:

<class 'complex'>

List Type

Lists store multiple items in a single variable.

Example:

fruits = ["Apple", "Banana", "Cherry"]

print(type(fruits))

Output:

<class 'list'>

Tuple Type

Tuples are ordered and unchangeable collections.

Example:

colors = ("Red", "Green", "Blue")

print(type(colors))

Output:

<class 'tuple'>

Range Type

The range() The function generates a sequence of numbers.

Example:

x = range(5)

print(type(x))

Output:

<class 'range'>

Dictionary Type

Dictionaries store data in key-value pairs.

Example:

person = {
    "name": "John",
    "age": 25
}

print(type(person))

Output:

<class 'dict'>

Set Type

Sets store unique values.

Example:

numbers = {1, 2, 3, 4}

print(type(numbers))

Output:

<class 'set'>

Boolean Type

Booleans represent one of two values: True or False.

Example:

is_active = True

print(type(is_active))

Output:

<class 'bool'>

None Type

The None value represents no value or a null value.

Example:

x = None

print(type(x))

Output:

<class 'NoneType'>

Setting a Specific Data Type

Python automatically assigns data types, but you can also specify them using constructor functions.

Example:

x = str("Hello")
y = int(5)
z = float(5.5)

print(type(x))
print(type(y))
print(type(z))

Output:

<class 'str'>
<class 'int'>
<class 'float'>

Summary

  • Data types define the kind of value a variable stores.
  • Use type() to check a variable’s data type.
  • Common data types include str, int, float, bool, list, tuple, set, and dict.
  • Python automatically determines the data type when a value is assigned.