Python Data Types & Collections – Comprehensive Study Notes
Basic Data Types
- Fundamental, immutable or primitive kinds of information the computer recognises immediately.
- string – text of any length; can hold sentences, phrases, single characters.
- Declaration / assignment example:
identifier = "Hello World" - int – whole numbers, positive or negative.
- Example:
identifier = 5 - float – numbers with a decimal component.
- Example:
identifier = 25.1234 - bool – Boolean logic values only True or False.
Advanced (Built-in) Data Types
- Collections and specialised containers supplied by Python.
- List – ordered, mutable, allows duplicates.
- Tuple – ordered, read-only (immutable once created).
- Dict (Dictionary) – key/value mapping, unordered (preserves insertion order ≥3.7), changeable, indexed by keys.
- Set – unordered, unindexed, unique values, partially writable.
- Frozenset – immutable version of a set.
- Bytes / Bytearray / Memoryview – raw binary data representations useful for low-level or I/O tasks.
Console I/O
- print() – displays data in the terminal / standard output.
- input() – pauses program, waits for user keystrokes, returns string.
- Bare prompt:
variable = input() - With custom prompt:
variable = input("Enter a Number: ")
Casting (Type Conversion)
- Explicitly change one data type into another.
- Numbers ⇒ String:
str(number) - String ⇒ Integer:
int(string) - String ⇒ Float:
float(string) - Any list⇄tuple / set⇄list⇄tuple conversions follow the same naming convention:
tuple(list), list(tuple), etc.
Variables & Arithmetic Operators
- A variable is a named reserved memory location whose contents can vary.
- Arithmetic Operators perform mathematical operations inside the program – addition, subtraction, multiplication, division, exponentiation, modulus, integer division.
LISTS
- Read-/Write ordered collection.
- Declaration syntax:
identifier = [value0, value1, value2], e.g. courses = ['BSIT', 'BSCS', 'BSEMC'].
Reading
- Whole list:
print(list) - Single item by index (
0-based): print(list[index])- Positive index counts from start: 0,1,2,…
- Negative index counts from end: −1,−2,−3,…
- Range/Slice:
print(list[start:end]), print(list[:end]), print(list[start:])- end index is excluded (half-open interval).
Writing / Modifying
- Assign item:
list[index] = new_value (e.g. list[0] = "Ganda"). - Length: len(list) – returns total number of items.
- Count occurrences:
list.count(value).
Adding Items
append(value) – appends to end.insert(index, value) – inserts at specific index.- Operator
+ – concatenates two lists → new list. extend(other_list) – appends all elements of other_list to original.
Deleting Items
remove(value) – deletes first matching value; error if absent.pop(index) – removes and returns item at index; if index omitted, removes last element.del list[index] – delete by index; del list removes entire list object.clear() – empties list but retains variable.
Copying / Duplicating
copy() – shallow clone, e.g. listTwo = listOne.copy().
Re-ordering
reverse() – in-place order reversal.sort() – ascending by default; sort(reverse=True) for descending; behaves by type (alphabetical for strings, numeric order for numbers).
Nested Lists (Sublists)
- Allowed to store another list as an element:
courses = ['BSIT','BSCS','BLIS', ['GANDA','CUTE']].
Practice Exercise (One-Function Calculator)
- Prompt user for two numbers, ask which arithmetic operator, output result using conditionals or dictionary of lambdas.
TUPLES
- Immutable (read-only) ordered collection; typically used for fixed records.
- Syntax:
identifier = (value0, value1, value2) - Example:
courses = ('BSIT','BSCS','BSEMC')
Casting Between Tuples & Lists
- List → Tuple:
tuple(list) - Tuple → List:
list(tuple) (gives mutability if edits are required).
SETS
- Unordered, unindexed, unique-value collection; duplicates automatically dropped.
- Syntax:
identifier = {value0, value1, value2} - Example:
courses = {'BSIT', 'BSCS', 'BLIS'}
Reading & Immutability Caveats
- Whole set:
print(set) - Cannot access by index; must cast to list/tuple first.
- Direct reassignment of individual element not possible for the same reason.
Size & Membership
- Length: len(set)
Adding Data
add(value) – single element.update(iterable) – add multiple items (list, tuple, another set, etc.).
Removing Data
remove(value) – errors if value missing.discard(value) – silent if value missing.pop() – removes arbitrary element (effectively first one chosen by interpreter).clear() – empties set.- Copy:
setTwo = setOne.copy()
Set Operations
union() – all distinct elements of both sets.difference(other) – elements in left set only.intersection(other) – shared elements.symmetric_difference(other) – elements exclusive to each set.- Relationship tests:
isdisjoint(other) – True if intersection is empty.issubset(other) – A⊆B?issuperset(other) – A⊇B?
Casting Sets
- Same pattern:
list(set_obj), tuple(set_obj), set(list_obj).
DICTIONARIES
- Unordered (insertion-ordered ≥3.7), mutable mapping of keys to values.
- Syntax:
identifier = {key1: value1, key2: value2} - Example:
student = {'name': 'jerstin', 'course': 'BSCS', 'age': 20}
Reading
- Whole dict:
print(dictionary) - Specific value:
print(dictionary['key']) - Alternative:
print(dictionary.get('key')) (returns None by default if key absent).
Writing / Updating
- Change value:
dictionary[key] = new_value - Add new key/value: assign to non-existent key.
- Length: len(dictionary)
Deleting
pop('key') – removes item by key, returns the value.popitem() – removes last inserted key/value pair (random prior to Python 3.7).clear() – empties dictionary.
Copying & Views
- Shallow copy:
dict_copy = original.copy() - Keys view:
dictionary.keys() → list-like view of all keys. - Values view:
dictionary.values() → list-like view of all values.
Composite Structures
- List of Dictionaries –
[{'name':'Ann'},{'name':'Bob'}] useful for records. - Nested Dictionary –
{'student1': {'name':'Ann'}, 'student2': {'name':'Bob'}} enables multi-level data representation.
Ethical / Practical Insights & Real-World Relevance
- Choosing the correct data structure drastically affects algorithmic efficiency (lookup times, mutability, memory).
- Immutability (tuples, frozensets) is valuable for integrity (e.g. fixed coordinates) and hashability (as dict keys / set members).
- Sets naturally model mathematical sets → useful for deduplication, membership tests, feature tagging.
- Dictionaries underpin JSON/XML style data interchange; mastering CRUD on dicts is foundational for API and data-processing work.
- Understanding casting safeguards user input (strings from
input()) before numeric operations, reducing runtime errors.