Python Data Types – Easy Explanation with Examples

In this chapter, we will break down the different Python Data Types one by one and understand how each type works with simple, practical examples. A clear understanding of Data Types in Python is essential because they determine the kind of data a variable can store and the operations Python can perform on it.

Data Types in Python

  • A data type determines how Python stores and processes a particular kind of data.
  • Data types help us understand:
    • The type of values that can be stored.
    • The memory allocated for storing data.
    • The range of values supported by the data type.
  • Python provides the following built-in core data types:
    • Numbers – used for numeric values.
    • String – used for text data.
    • List – used to store a collection of items.
    • Tuple – used to store an ordered collection of items.
    • Dictionary – used to store data in key–value pairs.

Numbers in Python

  • Numbers are used to store numeric values in Python.
  • Python supports three main number data types:
    • Integers
    • Floating-Point Numbers
    • Complex Numbers

Integers

  • Integers are whole numbers without any fractional or decimal part, such as 5, 39, 1917, and 0.
  • Integers can be positive or negative, for example +12, -15, 3000. If no sign is written, the number is considered positive.
  • Python has two types of integers:
    • Signed Integers – Normal whole numbers that can be positive or negative.
    • Booleans – Represent the truth values True and False
  • Boolean values True and False are a subtype of integers.
  • In Python, False behaves like 0 and True behaves like 1.
  • False and True (with capital first letters) are the only Boolean objects in Python. The forms false and true are not valid Boolean values.

Floating-Point Numbers

  • A floating-point number (float) is a number that contains a decimal or fractional part, such as 3.14159 or 12.0.
  • Floating-point numbers can be written in two forms:
    • Fractional Form: 3500.75, 0.00005, 147.9101
    • Exponent Notation: 3.50075E03, 0.5E-04, 1.479101E02
  • Floating-point variables represent real numbers and are commonly used for measurable quantities such as distance, area, and temperature.
  • The range of floating-point numbers depends on the underlying machine architecture and available memory.

Complex Numbers in Python

  • Python provides a Complex Number data type to represent numbers having both real and imaginary parts.
  • A complex number is of the form A + Bi, where A is the real part, B is the imaginary part, and i² = -1.
  • In Python, the imaginary unit is represented by j instead of i.
  • Examples of complex numbers:
    • 0 + 3.1j
    • 1.5 + 2j
  • Python internally stores complex numbers as a pair of floating-point numbers (real part and imaginary part).

Strings in Python

  • A string is a sequence of characters enclosed within quotation marks.
  • A string can contain letters, digits, spaces, and special characters.
  • Python does not have a separate character data type. A single character is also treated as a string.
  • Strings are stored as a sequence of individual characters, and each character can be accessed using its index.
  • Example: “LearAIhindi”
  • Python provides two-way indexing for strings:
    • Forward indexing: 0, 1, 2, …
    • Backward indexing: -1, -2, -3, …

Lists in Python

  • Lists are a compound data type used to store multiple values in a single variable.
  • A list contains comma-separated values enclosed within square brackets [ ].
  • List elements can be of the same or different data types.
  • Examples of lists:

[1, 2, 3, 4, 5]
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
[‘Neha’, 102, 79.5]

  • Lists are mutable, which means their elements can be changed after creation.
  • Each element in a list has an index number, starting from 0.

Tuples in Python

  • Tuples are collections of comma-separated values enclosed within parentheses ( ).
  • A tuple can store elements of different data types.
  • Examples:

(1, 2, 3, 4, 5)
(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
(7, 8, 9, ‘A’, ‘B’, ‘C’)

  • Tuples are immutable, which means their elements cannot be changed, added, or deleted after creation.
  • Tuples are similar to lists, but unlike lists, they are non-modifiable.

Sets in Python

  • Sets are collections of values enclosed within curly brackets { }.
  • Set elements are unordered and unindexed.
  • Sets do not allow duplicate values. Duplicate entries are automatically removed.
  • A set itself is mutable, meaning items can be added or removed from it.
  • Example:
    myset = {1, 2, 3, 4}

Dictionary in Python

  • A dictionary is an unordered collection of key : value pairs enclosed within curly brackets { }.
  • Each key in a dictionary must be unique; no two keys can be the same.
  • Dictionaries store data in the form: key : value
  • Example:
    {‘a’: 1, ‘e’: 2, ‘i’: 3, ‘o’: 4, ‘u’: 5}
TypeData TypeUse / DescriptionExample
NumberInteger (int)Stores whole numbers without a decimal point.age = 15
Float (float)Stores numbers with a decimal point.marks = 89.5
Missing valueNoneRepresents the absence of a value.result = None
SequenceString (str)Stores text or a sequence of characters enclosed in quotes.name = “Sanjay”
List (list)Stores an ordered collection of multiple values. Lists are mutable (can be changed).subjects = [“Python”, “AI”, “Math”]
Tuple (tuple)Stores an ordered collection of values that cannot be changed.student = (101, “Sanjay”, 15)
MappingSet (set)Stores an unordered collection of unique values. Duplicate values are automatically removed.numbers = {1, 2, 2, 3, 3}
Dictionary (dict)Stores data as key-value pairs for quick retrieval.student = {“name”:”Sanjay”, “age”:15}

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top