Data types classify the kind of value a variable can hold. Python has several built-in data types:
int
): Whole numbers without any decimal point. Example: x = 10
float
): Numbers with a decimal point. Example: y = 3.14
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)
str
): Sequences of characters. Use single ' '
or double " "
quotes. name = "Khizar"
, message = 'Hello, AIT!'
list
): Ordered, mutable (changeable) collections of items. Use square brackets []
.my_list = [1, 2, "three"]
tuple
): Ordered, immutable (cannot be changed after creation) collections of items. Use parentheses ()
.my_tuple = (1, 2, "three")
range
): sequence of numbers. Example: range(5) (creates sequence: 0, 1, 2, 3, 4)dict
): Unordered collections of key-value pairs. Use curly braces {}
.person = {"name": "Zaid", "age": 20}
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.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
frozenset
): Similar to a set, but once constructed, remains immutable in value.
bool
): Represents truth values True
or False
. Result of logical comparison operations.is_valid = True
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
bytearray
): sequence of mutable bytes ranging 0 - 255
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
None
): Represents no value/empty object/placeholder Example: result = None