# Binary & Data <!-- .element: class="r-fit-text" --> Week 2 --- ## Review  <!-- .element: style="height:400px" --> -- ### Four Roles of a Computer? - Input <!-- .element: class="fragment" --> - Storage <!-- .element: class="fragment" --> - Processing <!-- .element: class="fragment" --> - Output <!-- .element: class="fragment" --> NOTE: Input, storage, processing, output -- ### Hardware vs. Software? - Hardware: _The physical parts of a computer_ <!-- .element: class="fragment" --> - Software: _The programs that run on the hardware_ <!-- .element: class="fragment" --> --- ## How Computers Store Information <!-- .element: class="r-fit-text" --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- Numbers! Specifically, **binary** numbers. <!-- .element: class="fragment" --> -- Decimal: base-10 > 1, 2, 3, 4, 5... Binary: base-2 > 1, 10, 11, 100, 101... NOTES: - What other words start with "bi?" - What is another base you could count with? -- ### Bits A bit is a single binary digit: either 0 or 1. -- ### Bytes A byte is 8 bits. `00000000` is 0 in decimal <!-- .element: class="fragment" --> `11111111` is 255 in decimal <!-- .element: class="fragment" --> NOTES: why 8? because it's a power of 2 (2^3) --- ## Representing Data <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- ### Integers Binary numbers work just like decimal numbers, but with only two digits (0 and 1) instead of ten. -- ## Negative Integers How do you store the number -42? > <mark>0</mark>0100101 = 37 >> > <mark>1</mark>0100101 = -37 <!-- .element: class="fragment" --> -- ## Exercise Invent a method to store rational numbers in binary NOTES: - Rational numbers = "any number that can be written as a fraction" - Serialization = "translating data into a format that can be stored and reconstructed later" - Split into teams - Have each time serialize a fraction, other team deserializes - What are the limitations of your method? -- ### Floating Point How do you store a number like 3.14?  <!-- .element: style="height:400px" --> <!-- .element: class="fragment" --> NOTE: You do **not** need to remember this! -- ### Floating Point  <!-- .element: style="height:400px" --> -- ### Letters Assign each letter a number. > "Hello" = 72 101 108 108 111 NOTES: - This is called a "character encoding" - ASCII is the most common one for English text - For example, the letter "A" is number 65 --- ## Variables <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- ### What is a Variable? A name for some data. <!-- .element: class="fragment" --> NOTES: Think of it like a "bucket" or a "handle" -- ### Naming Things > There are only two hard things in Computer Science: cache invalidation and **naming things**. NOTES: This is a famous quote in programming. Good naming is hard but really important. -- ### Python Naming Rules - Must start with a letter or underscore (`_`) - Can contain letters, numbers, and underscores - Cannot contain spaces or special characters - Case-sensitive: `name` ≠ `Name` - By convention, use `snake_case` (lowercase with underscores) ```py # Good names student_name = "Alice" age_in_years = 15 _private_value = 42 # Bad names sn = "Alice" # too short studentName = "Alice" # camelCase, not Pythonic s n = "Alice" # space, won't work 2fast = 100 # starts with number ``` NOTES: Show that Python enforces some rules (syntax errors for spaces/numbers at start) but others are conventions (snake_case). -- ### Exercise: Name These Variables Suggest good variable names for: 1. A student's favorite color <!-- .element: class="fragment" --> 2. The number of books in a library <!-- .element: class="fragment" --> 3. Whether a user is logged in <!-- .element: class="fragment" --> 4. A person's email address <!-- .element: class="fragment" --> NOTES: - favorite_color - total_books or num_books - is_logged_in (boolean convention: starts with "is_" or "has_") - email_address or user_email -- ### Two Types of Variables - Primitive <!-- .element: class="fragment" --> - Compound <!-- .element: class="fragment" --> -- ### Primitive Data Types - Integer (int) <!-- .element: class="fragment" --> - Floating Point Number (float) <!-- .element: class="fragment" --> - String (str) <!-- .element: class="fragment" --> - Boolean (bool) <!-- .element: class="fragment" --> - NoneType (None) <!-- .element: class="fragment" --> -- ### Example ```py 100 # integer 3.14 # float "Hello, World!" # string True # bool None # None ``` -- ### Exercise: Choose a Data Type What primitive data type would you use to store: 1. A person's age <!-- .element: class="fragment" --> 2. A product's price <!-- .element: class="fragment" --> 3. A user's name <!-- .element: class="fragment" --> 4. Whether an account is premium <!-- .element: class="fragment" --> 5. The result of a calculation that hasn't been computed yet <!-- .element: class="fragment" --> NOTES: - age: int - price: float - name: str - premium: bool - result: None (use as a placeholder) -- ### What's Hard to Store with Primitives? Think about data that would be difficult to represent with just a single primitive type. <!-- .element: class="fragment" --> NOTES: Example 1: A student's schedule. You'd need to store the course name (string), time (maybe a string or int for hours), room number (int), and instructor name (string). A single primitive type can't hold all of that together. Example 2: A restaurant's menu item. You'd need the dish name (string), price (float), ingredients (multiple strings), and whether it's vegetarian (bool). Again, primitives alone aren't enough. This is why we need compound data types like lists and dictionaries. -- ### Compound Data Types - Lists <!-- .element: class="fragment" --> - Tuples <!-- .element: class="fragment" --> - Dictionaries <!-- .element: class="fragment" --> - Sets <!-- .element: class="fragment" --> -- ### Example ```py [1, 2, 3] # list (1, 2, 3) # tuple {"one": 1, "two": 2} # dictionary {1, 2, 3} # set ``` -- ### Reassignment Will this code throw an error? ```py year = 2026 year = "crazy" ``` No ✅ <!-- .element: class="fragment" --> -- ### Reassignment Will this code throw an error? ```py year = 2026 message = year + " is crazy" ``` Yes 💥 Instead, do this: <!-- .element: class="fragment" --> ```py year = 2026 message = str(year) + " is crazy" ``` ---  <!-- .element: style="height:400px" -->