Python Datatypes


 The various values that a variable might store are represented by data types in Python. Since Python is a dynamically-typed language, variables don't require their data type to be declared explicitly. Python uses the value that is assigned to the data to identify its type.

Numeric Data Types:

Numerous numeric kinds, such as complex numbers, floating-point numbers, and integers, are supported by Python.

int (Integer): represents complete numbers without a fractional part, both positive and negative.

Examples:

x = 10       # Positive integer
y = -5       # Negative integer
z = 0        # Zero

float (Floating-Point Number): Represents real numbers with a fractional part (decimal point).

Examples:

x = 10.5     # Positive float
y = -3.14    # Negative float
z = 0.0      # Zero as a float

complex (Complex Number): represents numbers that have two parts: an imaginary part (represented by j for the imaginary unit) and a real part.

Examples:

x = 2 + 3j   # Complex number (2 is the real part, 3j is the imaginary part)
y = -1j      # Imaginary number

Sequence Data Types:

In Python, sequences represent ordered collections of objects.

String:

A string, denoted by str, is a string that has one, two, or three quotations around it.
Strings are unchangeable once they are created; they cannot be altered.

Example:

name = "Alice"      # Using double quotes
greeting = 'Hello!' # Using single quotes
multi_line = '''This is
a multi-line string.'''  # Using triple quotes

List:

Describes a mutable, ordered set of elements. Elements in lists can be of many sorts, and their index starts at 0.

numbers = [1, 2, 3, 4]    # List of integers
mixed = [1, "apple", 3.14] # List with mixed data types
nested = [[1, 2], [3, 4]] # Nested list

Tuples:

 Denotes an immutable, ordered set of objects. A tuple's elements cannot be altered once it has been constructed.

point = (3, 4)           # Tuple of integers
info = ("Alice", 25, 5.6) # Tuple with mixed data types
single = (42,)           # Single-element tuple (note the comma)

Mapping Data Types:

Mappings are collections of key-value pairs.

Dictionary:

Indicates a mutable, unordered set of key-value pairs. Values can be any kind, but keys (such as texts, numbers, or tuples) have to be distinct and unchangeable.

person = {
    "name": "Alice",
    "age": 25,
    "is_student": False
}
print(person["name"])  # Access value by key

Set Data Types:

Sets are unordered collections of unique elements.

Set: represents a changeable, unordered set of individual objects. The curly braces {} are used to define sets; duplicates are not permitted.

Example:
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")  # Add an element

frozenset: represents a set's unchangeable version. Elements cannot be added to or deleted after they are created.

Example:

frozen_fruits = frozenset(["apple", "banana", "cherry"])

Boolean Data Type: Booleans represent one of two values: True or False. The truth value of an expression is represented by the Boolean type bool. Frequently employed in flow control and conditions

Example:

is_student = True
is_adult = False

Binary Data Types: In order to handle files, pictures, and other sorts of data, these data types are necessary for working with binary data.

bytes: represents a byte sequence that is unchangeable. An integer between 0 and 255 makes up each byte.

b = b"hello"  # A byte string

Bytearray: represents a changeable byte sequence.

b_arr = bytearray(b"hello")
b_arr[0] = 72  # Modifying the first byte

memoryview: useful for big datasets, it represents a view of another binary object without transferring any data.

b = bytearray("hello", "utf-8")
mem_view = memoryview(b)


None Type: Represents the absence of a value. A null or no value is defined by using none.
frequently used to denote "nothing" or as the default value.

Example:
x = None


Dynamic Typing in Python: In Python, a variable's data type is automatically ascertained from its assigned value. Even during execution, you can modify a variable's type:

Example:
x = 10      # int
x = "Hello" # Now it's a string


















Post a Comment

0 Comments