Python Data Types

Data types classify the kind of value a variable can hold. Python has several built-in data types:

1. Numeric Types:

  • Integers (int): Whole numbers without any decimal point. Example: x = 10
  • Floating-point numbers (float): Numbers with a decimal point. Example: y = 3.14
  • Complex Numbers (complex): Numbers with a real and imaginary component (represented as a + bj, where 'j' is the imaginary unit. Example: z = 2 + 3j. Less commonly used, for specialized math or engineering related functions that cannot otherwise be fully represented)
x = 10 # Integer
y = 3.14 # Float
z = 2+3j # Complex

2. Text Type:

  • Strings (str): Sequences of characters. Use single ' ' or double " " quotes.
    Example: name = "Khizar", message = 'Hello, AIT!'
name = "Akef"
quote = 'Never give up!'

3. Sequence Types:

  • Lists (list): Ordered, mutable (changeable) collections of items. Use square brackets [].
    Example: my_list = [1, 2, "three"]
  • Tuples (tuple): Ordered, immutable (cannot be changed after creation) collections of items. Use parentheses ().
    Example: my_tuple = (1, 2, "three")
  • Range (range): sequence of numbers. Example: range(5) (creates sequence: 0, 1, 2, 3, 4)
    Primarily useful when creating dynamic code output since you do not hardcode numbers for every operation step of iteration when constructing looping mechanisms which is of course the core area where such functionality makes most sense to improve adaptability or output variation possibilities
my_list = [1, "two", 3.0] # List
my_tuple = (10, 20, "thirty") # Tuple
my_range = range(1,6) # range from 1 to 5 in intervals of +1 integer step, i.e., (1, 2, 3, 4, 5)

4. Mapping Type:

  • Dictionaries (dict): Unordered collections of key-value pairs. Use curly braces {}.
    Example: person = {"name": "Zaid", "age": 20}
car = {"brand":"Ford", "model": "Mustang", "year": 1964}

5. Set Types:

  • Sets (set): Unordered collections of unique items. Useful for removing duplicates or checking for memberships. Use curly braces {}, and unlike dictionaries no key:value pairing, elements listed individually inside curly braces and they remain unordered for operations such as union or complement.
    Example: my_set = {1, 2, 3, 3} (duplicate removed). To create empty set set(). Note for sets alone you also enclose set information between curly braces: my_empty_set=set() so remember no curly braces in initialization for creating an empty set. Empty dictionaries like any other must use some curly brace as long as initialized, for any valid non-empty sets this syntax not usable to signal a different kind of variable being constructed and populated
  • Frozen Sets (frozenset): Similar to a set, but once constructed, remains immutable in value.
fruits = {"apple", "banana", "cherry"} # set
frozen_fruits = frozenset({"apple", "banana", "cherry"}) #frozenset

6. Boolean Type:

  • Boolean (bool): Represents truth values True or False. Result of logical comparison operations.
    Example: is_valid = True
raining = False # Boolean indicating current weather

7. Binary Types:

  • Bytes (bytes): Sequence of immutable bytes ranging 0 - 255. Often seen or required for operations in file operations using open(). Typical output encoding, default typically utf-8 but many variations can be encountered when interacting with input not all adhering to default and common syntax across datasets
  • Byte Array (bytearray): sequence of mutable bytes ranging 0 - 255
  • Memory View (memoryview): supports manipulation over existing byte data in safe/convenient means where such use appropriate in contrast to simple raw access of the sort usable via bytes() where bytes() provides full content itself unlike a "pointer view", view object remains unattached by contrast as expected since operating on its own
binary_data = b"Hello" # bytes
mutable_data = bytearray(5) # bytearray
dataview= memoryview(mutable_data) # Memory view

8. None Type:

  • NoneType (None): Represents no value/empty object/placeholder Example: result = None
value = None #None