Data Types in Python

Data types in Python specify the kind of data that variables can hold and how it is stored. Python has various data types that include numbers (integers, floats), booleans, strings, and more complex types like lists, tuples, dictionaries, and sets.

Common Data Types

Numbers

Integers (int), floating point numbers (float), and complex numbers (complex).

x = 5    # int
y = 3.14 # float
z = 2j   # complex

Boolean

Represents true or false and is used in conditional expressions.

x = True  # bool

String

Used to represent text data.

x = "Hello, World!"  # str

Collections

List

Ordered and mutable collection of items.

x = [1, 2.2, 'python']  # list

Tuple

Ordered and immutable collection of items.

x = (1, 2.2, 'python')  # tuple

Dictionary

Unordered collection of key-value pairs.

x = {'key': 'value'}  # dict

Set

Unordered collection of unique items.

x = {1, 2.2, 'python'}  # set

Type Conversion

Python allows you to convert from one type to another using the int(), float(), str(), and similar functions.

x = 10      # int
y = str(x)  # now y is '10'