Week 1: Data Types & Representation
2082 words
10 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Week 1: Data Types & Representation > **BSCS1001 — IIT Madras BS Degree** **Prerequisite:** Topic 01 (What is Computational Thinking) **Cross-links:** BSCS1002-Python (Week 1 — Data Types), BSCS2002-PDSA (Week 2 — Data Structures) ## 1. Motivation: Why Data Types?

Week 1: Data Types & Representation
BSCS1001 — IIT Madras BS Degree Prerequisite: Topic 01 (What is Computational Thinking) Cross-links: BSCS1002-Python (Week 1 — Data Types), BSCS2002-PDSA (Week 2 — Data Structures)
1. Motivation: Why Data Types?
Imagine you receive a form with the following fields filled in:
| Field | Value | Is This OK? |
|---|---|---|
| Name | Rahul123!@# | ❌ Names shouldn't have numbers or special chars |
| Age | -5 | ❌ Age can't be negative |
| Marks | 108 | ❌ Marks can't exceed 100 |
| Phone | hello | ❌ Phone should be digits |
You can spot these errors because you know what kind of data each field should contain. But a computer doesn't — unless we tell it.
Data types are the mechanism we use to tell the computer (or another person) what values are allowed and what operations are permitted.
Real-world analogy: A "data type" is like a mold for a key. The mold determines what shape keys can be made (allowed values) and what locks they can open (allowed operations).
What a Data Type Defines
(Diagram)
2. Sanity of Data
Sanity of data means checking that data makes sense — that values are within expected ranges and operations are meaningful.
Two Kinds of Constraints
1. Value Constraints (What values are allowed?)
| Data Field | Allowed Values | Why? |
|---|---|---|
| Marks | 0 to 100 | Can't score more than 100 or less than 0 |
| Gender | M or F | Only two values (in our dataset) |
| Name | Letters, spaces | No numbers or special characters |
| Date | 0 to 365 | Day of year |
2. Operation Constraints (What can we do?)
| Data | Allowed Operations | Not Allowed |
|---|---|---|
| Marks | + (add), < (compare) | × (multiplying marks is meaningless) |
| Name | = (compare for equality) | + (can't add two names) |
| Gender | = (check if equal) | +, - (meaningless) |
Worked Example: Sanity Check
Problem: Check if a student record is valid.
Record:
{Name: "Alice", Age: 22, Maths: 85, Physics: 110}| Field | Sanity Rule | Check | Verdict |
|---|---|---|---|
| Name | Only letters and spaces | "Alice" ✅ | Valid |
| Age | Between 0 and 150 | 22 ✅ | Valid |
| Maths | Between 0 and 100 | 85 ✅ | Valid |
| Physics | Between 0 and 100 | 110 ❌ | Invalid |
💡 Key Insight: Sanity of data leads directly to the concept of data types. Once we define types, sanity checks become automatic.
3. Basic Data Types
The course defines three basic data types (plus a fourth — String — which is a sequence of characters).
3.1 Boolean
The simplest data type. Named after George Boole, a mathematician.
| Property | Description |
|---|---|
| Values | True or False only |
| Operations | AND, OR, NOT |
| Result type | Boolean |
Boolean Operations (Truth Tables)
AND (
AND): True only when BOTH inputs are True.| A | B | A AND B |
|---|---|---|
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
OR (
OR): True when AT LEAST ONE input is True.| A | B | A OR B |
|---|---|---|
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
NOT (
NOT): Flips the value.| A | NOT A |
|---|---|
| True | False |
| False | True |
Worked Example: Boolean Evaluation
Evaluate:
(True AND False) OR (NOT False)| Step | Expression | Result |
|---|---|---|
| 1 | True AND False | False |
| 2 | NOT False | True |
| 3 | False OR True | True |
3.2 Integer
Whole numbers (positive, negative, and zero).
| Property | Description |
|---|---|
| Values | ..., -3, -2, -1, 0, 1, 2, 3, ... |
| Operations | +, -, ×, / (result: Integer) |
| Comparisons | ``, = (result: Boolean) |
Integer Operations Examples
| Operation | Result | Data Type of Result |
|---|---|---|
3 + 5 | 8 | Integer |
10 - 7 | 3 | Integer |
4 × 6 | 24 | Integer |
7 < 3 | False | Boolean |
5 = 5 | True | Boolean |
⚠️ Note: In this course, integer division/may produce non-integer results, so we mostly use+,-,×for integers.
3.3 Character
A single letter, digit, or symbol.
| Property | Description |
|---|---|
| Values | A–Z, a–z, 0–9, special characters |
| Operations | = (compare for equality) |
| Result type | Boolean |
Examples of characters:
'A', 'z', '5', '?', ' ' (space)
Special characters: , ; : * / & % $ # @ !3.4 String
A sequence of characters.
| Property | Description |
|---|---|
| Values | Any sequence of characters, e.g., "Hello", "Alice", "M" |
| Operations | = (equality), char in string? (checking membership) |
| Result type | Boolean; Boolean |
Key difference: A Character is a single symbol, a String is a sequence of zero or more characters.
4. Subtypes
A subtype is a more restricted version of a basic type. It limits the allowed values and/or operations.
Why Subtypes?
A plain Integer is too broad. Consider:
- Marks: Should be 0–100, not any integer
- Count: Should be 0 or positive, not negative
- Date: Should be 0–365, not any integer Subtypes allow us to create more specific, meaningful types.
Integer Subtypes
SeqNo (Sequence Number)
pseudoRange: 0, 1, 2, ..., Max (e.g., 10000) Operations: None of the integer operations make sense Purpose: Identifies a card/row uniquely
Marks
pseudoRange: 0, 1, 2, 3, ..., 100 Operations: +, - (result: Marks); <, >, = (result: Boolean) NOT allowed: ×, ÷ (multiplying marks makes no sense!)
Count
pseudoRange: 0, 1, 2, 3, ... Operations: +, - (result: Count); <, >, = (result: Boolean) NOT allowed: ×, ÷
Date (Day of Year)
pythonRange: 0, 1, 2, 3, ..., 365 Operations: print (result: String); <, >, = (result: Boolean) Meaning: 0 = "1 Jan", 31 = "1 Feb", 365 = "31 Dec"
Character Subtypes
Gender
pseudoValues: 'M' or 'F' Operation: = (compare) Result: Boolean
String Subtypes
Names
pseudoValues: Strings with no special characters Operation: = Result: Boolean
City
pseudoValues: Strings with no special characters Operation: = Result: Boolean
Words
pseudoValues: Strings with alphanumeric and punctuation Operation: = Result: Boolean
Category
pseudoValues: One of: "Noun", "Verb", "Preposition", "Adjective" Operation: = Result: Boolean
Subtype Hierarchy Diagram
(Diagram)
5. Type Transformations
Sometimes we need to convert data from one type to another.
Date to String Transformation
Dates are stored as integers (0–365) but displayed as strings.
| Integer Value | Display String |
|---|---|
| 0 | "1 Jan" |
| 31 | "1 Feb" |
| 59 | "1 Mar" |
pythonprint(0) → "1 Jan" print(31) → "1 Feb" print(365) → "31 Dec"
Fractional Marks Transformation
In this course, we avoid floating-point numbers by scaling:
- Actual mark:
62.5 - Stored as integer:
6250(multiply by 100) - Display:
print(6250)→"62.5"(divide by 100 when printing)
pseudoValue to store = Original mark × 100 Display value = Stored value ÷ 100
Example:
| Original | Scaled (×100) | Stored Type | Operation | Printed |
|---|---|---|---|---|
| 76.25 | 7625 | Integer | +, - | "76.25" |
| 82.5 | 8250 | Integer | +, - | "82.5" |
| 91.0 | 9100 | Integer | +, - | "91.0" |
Why do this? Integers are simpler, faster, and avoid rounding errors that occur with decimal numbers.
6. Records: Grouping Data Together
A Record (also called struct or tuple) is a data type with multiple named fields, each of which has a name and a value.
Record Example: Marks Card
(Diagram)
Why Records?
Without records, we'd need separate variables for every attribute of every student, which is impossible for a class of 200.
| Without Records | With Records |
|---|---|
Alice_Maths, Bob_Maths, Charlie_Maths... | Each card X has X.Maths |
| Need 200 variables for each subject | One record type, many instances |
| Cannot write generic procedures | Can write procedures that work on any card |
Accessing Record Fields
We use dot notation:
RecordName.FieldNamepseudoX.Maths ← The Maths field of card X X.Name ← The Name field of card X X.Gender ← The Gender field of card X
Other Record Examples
Word in Paragraph Record
| Field | Data Type |
|---|---|
| SerialNo | SeqNo |
| Word | Words |
| PartOfSpeech | Category |
Shopping Bill Record
| Field | Data Type |
|---|---|
| CustomerName | Names |
| ItemName | Words |
| Category | Category |
| Quantity | Count |
| Amount | Marks (type, for cost) |
7. Lists: Sequences of Data
A List is a sequence of data elements, all of the same type.
List vs Record
| Record | List |
|---|---|
| Groups different types of data | Groups same type of data |
| Fields have names | Elements have positions |
| Example: A single student's data | Example: All students' IDs |
Examples of Lists
| List Name | Element Type | Description |
|---|---|---|
MarksCardList | MarksCard Record | All marks cards in the dataset |
ParagraphWordList | WordInPara Record | All words in a paragraph |
ShoppingBillList | ShoppingBill Record | All shopping bills |
ItemList | Item Record | Items in a single bill |
Visual Representation
pseudoMarksCardList = [Card1, Card2, Card3, ..., CardN] ↑ ↑ ↑ ↑ SeqNo=1 SeqNo=2 SeqNo=3 SeqNo=N
Key Point About Lists
- All elements in a list are usually of the same data type
- Lists can be iterated over (the iterator pattern from Topic 01)
- Lists can be built gradually by appending elements
Preview: We'll work extensively with lists starting in Week 5. For now, just understand that a list is a collection of items arranged in a specific order.
8. Comparison of Data Types
Basic Data Types Summary Table
| Type | Values | Operations | Result Types | Use Case |
|---|---|---|---|---|
| Boolean | True, False | AND, OR, NOT | Boolean | Conditions, flags |
| Integer | ..., -2, -1, 0, 1, 2, ... | +, -, ×, /, , = | Integer, Boolean | Counts, sums, indices |
| Character | A-Z, a-z, 0-9, special chars | = | Boolean | Single letters, gender codes |
| String | Any character sequence | =, char-in-string? | Boolean | Names, words, categories |
Type vs Subtype
| Aspect | Basic Type | Subtype |
|---|---|---|
| Range | Broad (e.g., all integers) | Restricted (e.g., 0-100) |
| Operations | All type operations | Subset of operations |
| Example | Integer → Marks | Marks: 0-100, no ×, ÷ |
Record vs List
| Aspect | Record | List |
|---|---|---|
| Structure | Named fields | Positional elements |
| Element types | Can be different | Same type preferred |
| Access | By field name: X.Name | By position |
| Analogy | A row in a table | The whole table |
9. Practice Questions
Basic Questions
Q1. Which basic data type has only two possible values?
Show AnswerBoolean — values areTrueandFalse. Q2. Evaluate:(NOT (True AND False)) OR (False AND True)Show Answer
| Step | Expression | Result |
|---|---|---|
| 1 | True AND False | False |
| 2 | NOT False | True |
| 3 | False AND True | False |
| 4 | True OR False | True |
Q3. What is the range of values for the Marks subtype?
Show AnswerMarks range from 0 to 100 (inclusive). Q4. Which of these operations are allowed on a Gender field? (a)+(b)=(c)×(d)<Show AnswerOnly (b)=(comparison for equality) is allowed on Gender. Arithmetic operations like+,×,<are meaningless.
Intermediate Questions
Q5. What is wrong with storing marks as a plain Integer data type? Why do we need a Marks subtype?
Show AnswerIf marks are stored as Integer, the computer would allow:
- Negative marks (-5) → invalid
- Marks above 100 (150) → invalid
- Multiplication of marks → meaningless
A Marks subtype restricts:
Range to 0-100 Operations to+,-,<,>,=only (no×,÷) Q6. Convert these fractional marks to their scaled integer representation: (a) 45.75 (b) 80.0 (c) 99.99 Show Answer
| Original | Scaled (×100) |
|---|---|
| 45.75 | 4575 |
| 80.0 | 8000 |
| 99.99 | 9999 |
Q7. If
Date values are stored as integers 0-365, what date does value 59 represent? (Hint: 0 = 1 Jan, 31 = 1 Feb)Show Answer
0 = 1 Jan (31 days in January) 31 = 1 Feb (28 days in February in non-leap year) 31 + 28 = 59 = 1 March Q8. What is the difference between a Record and a List? Give one example of each. Show Answer
| Aspect | Record | List |
|---|---|---|
| Fields | Named (e.g., X.Name, X.Maths) | Positional (by index) |
| Types | Can mix types | Usually same type |
| Example | A single MarksCard (has Name, Maths, Physics, etc.) | MarksCardList (all cards together) |
Advanced Questions
Q9. Design a Record type for a Library Book. What fields would it have and what data types/subtypes would each field use?
Show AnswerjavaRecord: Book - BookID: SeqNo (unique identifier) - Title: String (book name) - Author: Names (author name) - ISBN: String (unique book number) - YearPublished: Integer (subtype: 1900-2024) - Pages: Count (number of pages) - IsIssued: Boolean (True if checked out) - Category: String (subtype: Fiction/Non-fiction/Reference)
Q10. Can you add two Names together? Can you compare two Names with
=? Explain why or why not for each.Show Answer
=(equality): ✅ Yes! We can check if two names are the same (e.g.,"Alice" == "Alice"→ True). Result is Boolean.+(addition): ❌ No! Adding names doesn't make sense — what would"Alice" + "Bob"mean? Names are identifiers, not quantities.This is exactly what data types enforce: only meaningful operations are allowed. Q11. Explain whymarks × marksis not allowed butmarks + marksis allowed. Show Answer
marks + marks: Adding marks makes sense — we sum scores to get a total. The result is also of type Marks (or Total).marks × marks: Multiplying marks has no real-world meaning. What would "85 × 72" represent? It's not a score, not a count, nothing useful. The Marks subtype correctly forbids this.This illustrates the key principle: data types model real-world constraints. Q12. Trace the Boolean expression:(A OR B) AND (NOT A)for all four combinations of A and B. Show Answer
| A | B | A OR B | NOT A | (A OR B) AND (NOT A) |
|---|---|---|---|---|
| F | F | F | T | F |
| F | T | T | T | T |
| T | F | T | F | F |
| T | T | T | F | F |
The expression is True only when A is False AND B is True.
📚 Cross-References
| Course | Topic | Connection |
|---|---|---|
| BSCS1002 (Python) | Week 1 — Data Types | Python's int, bool, str types |
| BSCS1002 (Python) | Week 2 — Operators | Boolean operators in Python |
| BSCS2002 (PDSA) | Week 1 — Abstract Data Types | Formal definition of types |
Next Topic: 03 — Pseudocode BasicsQuiz Tip: Questions about allowed operations on specific data types are very common in Quiz 1. Memorize the operation tables! Join Discord PreviousWhat is Computational Thinking?NextPseudocode Basics