Python Data Types

Python 3 offers a rich set of data types that are used to store various kinds of data. Here’s an overview of the fundamental data types in Python 3:

1. Numeric Types


  1. int (Integer)
    • Represents whole numbers, positive or negative, without a fractional component.
    • Example: 5, -42, 0
  2. float (Floating-Point Number)
    • Represents real numbers with a fractional component, stored as a decimal point.
    • Example: 3.14, -0.001, 2.0
  3. complex
    • Represents complex numbers, which are numbers with a real and imaginary part.
    • Example: 3 + 5j, where 3 is the real part and 5j is the imaginary part.

2. Sequence Types


  1. str (String)
    • Represents a sequence of characters (text). Strings are immutable, meaning they cannot be changed after they are created.
    • Example: "Hello, World!", 'Python 3'
  2. list
    • Represents an ordered collection of items, which can be of different data types. Lists are mutable, meaning their content can be changed.
    • Example: [1, 2, 3], ["apple", "banana", "cherry"]
  3. tuple
    • Similar to a list, but tuples are immutable. Once a tuple is created, its content cannot be modified.
    • Example: (1, 2, 3), ("apple", "banana", "cherry")
  4. range
    • Represents an immutable sequence of numbers, typically used for looping a specific number of times.
    • Example: range(5) generates the numbers 0, 1, 2, 3, 4.

3. Set Types


  1. set
    • Represents an unordered collection of unique items. Sets are mutable.
    • Example: {1, 2, 3}, {"apple", "banana", "cherry"}
  2. frozenset
    • Represents an immutable version of a set. Once created, the items in a frozenset cannot be modified.
    • Example: frozenset([1, 2, 3]), frozenset({"apple", "banana", "cherry"})

4. Mapping Types


  1. dict (Dictionary)
    • Represents a collection of key-value pairs, where each key is unique. Dictionaries are mutable.
    • Example: {"name": "Alice", "age": 25}, {1: "one", 2: "two", 3: "three"}

5. Boolean Type


  1. bool
    • Represents a logical value, either True or False.
    • Example: True, False

6. Binary Types


  1. bytes
    • Represents an immutable sequence of bytes (8-bit values).
    • Example: b"Hello, World!"
  2. bytearray
    • Represents a mutable sequence of bytes.
    • Example: bytearray(b"Hello, World!")
  3. memoryview
    • Provides a view of another binary sequence, such as bytes or bytearray, without copying the data.
    • Example: memoryview(b"Hello, World!")

7. None Type


  1. NoneType
    • Represents the absence of a value or a null value. There is only one value of this type, which is None.
    • Example: None

8. User-Defined Types


  1. Classes: In Python, you can define your own data types using classes. These custom types can be as simple or complex as needed for your application.

Type Checking and Conversion


  1. Type Checking: You can check the type of a variable using the type() function.
    • Example: type(5) returns <class 'int'>.
  2. Type Conversion: Python provides functions to convert between data types.
    • Example: int("123") converts a string to an integer, str(123) converts an integer to a string.

Data Type Description Example
int Integer (whole number) x = 10
float Floating-point number (decimal) y = 10.5
complex Complex number z = 3 + 5j
bool Boolean (True or False) is_valid = True
str String (text) name = “Alice”
list List (ordered, mutable collection) fruits = [“apple”, “banana”]
tuple Tuple (ordered, immutable collection) coordinates = (10, 20, 30)
dict Dictionary (key-value pairs) person = {“name”: “Alice”}
set Set (unordered, unique items) unique_numbers = {1, 2, 3}
frozenset Immutable version of a set frozen_numbers = frozenset([1, 2])
bytes Immutable sequence of bytes byte_data = b”Hello”
bytearray Mutable sequence of bytes byte_array_data = bytearray(5)
memoryview View on binary data memory_view = memoryview(b”Hello”)
NoneType Absence of value value = None

These are the fundamental data types in Python 3. Understanding them is crucial for writing efficient and effective Python code.

1 Comment

  1. You made certain fine points there. I did a search on the matter and found a good number of people will have the same opinion with your blog.

Leave a Reply

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