Lesson 5: Computing Fundamentals#

Ever wondered how computers actually work? How do they store photos, play music, and run programs? Letโ€™s peek under the hood!

What Youโ€™ll Learn#

  • How computers store information (binary!)

  • What bits, bytes, and memory are

  • How text, images, and sound are stored

  • Computer hardware components

  • How the Internet works

  • Operating systems basics

๐Ÿ’ก Real-World Analogy#

Think of a computer like a kitchen:

  • CPU (processor) = The chef (does the work)

  • RAM (memory) = The countertop (temporary workspace)

  • Storage (hard drive) = The pantry (long-term storage)

  • Input = Ingredients you bring in

  • Output = The delicious meal you create!


1. The Binary System: How Computers Think#

Everything in a computer is stored as 0s and 1s!

Why Binary?#

Computers use electricity, which has two states:

  • 0 = No electricity (OFF)

  • 1 = Electricity flowing (ON)

This is called binary - a number system with only two digits (0 and 1).

How We Count vs How Computers Count#

Humans (Decimal - Base 10):

  • Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

  • After 9, we go to 10 (one group of ten, zero ones)

Computers (Binary - Base 2):

  • Digits: 0, 1

  • After 1, we go to 10 (one group of two, zero ones)

Counting in Binary#

Decimal

Binary

Explanation

0

0

Zero

1

1

One

2

10

One two, zero ones

3

11

One two, one one

4

100

One four, zero twos, zero ones

5

101

One four, zero twos, one one

6

110

One four, one two, zero ones

7

111

One four, one two, one one

8

1000

One eight, zero fours, zero twos, zero ones

# Python can convert numbers to binary
number = 10
binary = bin(number)  # bin() converts to binary string
print(f"The number {number} in binary is: {binary}")
print(f"Without '0b' prefix: {binary[2:]}")

# Let's see more examples
print("\nDecimal to Binary:")
for num in [1, 2, 5, 10, 16, 25, 100, 255]:
    print(f"{num:3d} = {bin(num)[2:]}")

2. Understanding Binary Place Values#

Just like decimal has place values (ones, tens, hundreds), binary has place values too!

Decimal Place Values:#

 1  2  3  4
 โ†“  โ†“  โ†“  โ†“
1000 100 10 1  (powers of 10)

Binary Place Values:#

 1  0  1  0
 โ†“  โ†“  โ†“  โ†“
 8  4  2  1  (powers of 2)

To convert binary to decimal:

  1. Write the place values (8, 4, 2, 1)

  2. If the binary digit is 1, add that place value

  3. If the binary digit is 0, skip it

Example: 1010 in binary

Binary:  1    0    1    0
Value:   8  + 0  + 2  + 0  = 10
# Let's manually convert binary to decimal
def binary_to_decimal(binary_string):
    """
    Convert binary string to decimal number.
    Shows the step-by-step calculation.
    """
    # Remove '0b' prefix if present
    binary_string = binary_string.replace('0b', '')
    
    print(f"Converting binary {binary_string} to decimal:")
    print("\nPosition values (powers of 2):")
    
    decimal = 0
    length = len(binary_string)
    
    # Show place values
    place_values = [2 ** (length - 1 - i) for i in range(length)]
    print("Position:", ' '.join(str(pv).rjust(4) for pv in place_values))
    print("Binary:  ", ' '.join(digit.rjust(4) for digit in binary_string))
    
    # Calculate
    calculation = []
    for i, digit in enumerate(binary_string):
        position = length - 1 - i
        value = 2 ** position
        if digit == '1':
            decimal += value
            calculation.append(str(value))
        else:
            calculation.append('0')
    
    print("Values:  ", ' + '.join(val.rjust(4) for val in calculation))
    print(f"\nResult: {decimal}")
    return decimal

# Test it
binary_to_decimal("1010")
print("\n" + "="*40 + "\n")
binary_to_decimal("1111")
# Convert binary to decimal (Python's way)
binary_string = "0b1010"  # '0b' prefix means binary
decimal = int(binary_string, 2)  # Convert from base 2
print(f"Binary {binary_string} = Decimal {decimal}")

# Try more examples
binary_numbers = ["1", "10", "11", "100", "101", "110", "111", "1000"]
print("\nBinary to Decimal conversions:")
for binary in binary_numbers:
    decimal = int(binary, 2)
    print(f"{binary:>4s} = {decimal}")

3. Bits and Bytes: Storage Units#

The Building Blocks#

Bit (Binary Digit):

  • The smallest unit of data

  • Can be either 0 or 1

  • Like a single light switch (on or off)

Byte:

  • 8 bits grouped together

  • Can store values from 0 to 255

  • Can represent one character (like โ€˜Aโ€™ or โ€˜5โ€™)

Storage Size Hierarchy#

Unit

Size

Example

1 Bit

0 or 1

One light switch

1 Byte

8 bits

One character (โ€˜Aโ€™)

1 Kilobyte (KB)

1,024 bytes

One page of text

1 Megabyte (MB)

1,024 KB

One high-quality photo

1 Gigabyte (GB)

1,024 MB

About 1 hour of HD video

1 Terabyte (TB)

1,024 GB

250,000 songs

Why 1024 instead of 1000?#

Because computers use binary! 1024 = 2^10, which is more natural for computers than 1000.

# Understanding byte capacity
print("What can 1 byte (8 bits) store?")
print(f"Minimum value: {0}")
print(f"Maximum value: {2**8 - 1} (255)")
print(f"Total possible values: {2**8} (0-255)")

print("\nIn binary:")
print(f"Smallest: {bin(0)}")
print(f"Largest:  {bin(255)} (11111111)")
# Convert bytes to human-readable format
def bytes_to_readable(bytes_count):
    """
    Convert bytes to human-readable format.
    """
    if bytes_count < 1024:
        return f"{bytes_count} bytes"
    elif bytes_count < 1024 * 1024:
        return f"{bytes_count / 1024:.2f} KB"
    elif bytes_count < 1024 * 1024 * 1024:
        return f"{bytes_count / (1024 * 1024):.2f} MB"
    elif bytes_count < 1024 * 1024 * 1024 * 1024:
        return f"{bytes_count / (1024 * 1024 * 1024):.2f} GB"
    else:
        return f"{bytes_count / (1024 * 1024 * 1024 * 1024):.2f} TB"

# Test with different file sizes
print("File Size Examples:")
print(f"Text file: {bytes_to_readable(500)}")
print(f"Small image: {bytes_to_readable(50000)}")
print(f"Song (MP3): {bytes_to_readable(5000000)}")
print(f"HD Movie: {bytes_to_readable(5000000000)}")
print(f"Video game: {bytes_to_readable(50000000000)}")

4. Text Encoding: How Computers Store Text#

Computers donโ€™t actually store letters - they store numbers! Text encoding is the system that maps letters to numbers.

ASCII (American Standard Code for Information Interchange)#

  • Created in 1963

  • Uses 7 bits (128 characters)

  • Includes: A-Z, a-z, 0-9, punctuation, control characters

Example ASCII Codes:

  • โ€˜Aโ€™ = 65

  • โ€˜Bโ€™ = 66

  • โ€˜aโ€™ = 97

  • โ€˜0โ€™ = 48

  • โ€˜ โ€˜ (space) = 32

Unicode / UTF-8#

  • Modern standard for text

  • Can represent ALL languages and emojis!

  • UTF-8 can use 1-4 bytes per character

  • Backward compatible with ASCII

# Working with ASCII codes
letter = 'A'
code = ord(letter)  # ord() gets the ASCII/Unicode code
print(f"Letter '{letter}' has code: {code}")
print(f"In binary: {bin(code)}")

# Convert code back to letter
code = 65
letter = chr(code)  # chr() converts code to character
print(f"\nCode {code} represents: '{letter}'")
# Encode a message
message = "HELLO"
print(f"Encoding message: '{message}'\n")

print("Letter | ASCII Code | Binary")
print("-------|------------|------------------")
for char in message:
    code = ord(char)
    binary = bin(code)[2:].zfill(8)  # 8-bit binary
    print(f"  {char}    |    {code:3d}     | {binary}")

# Calculate storage needed
bytes_needed = len(message)
bits_needed = bytes_needed * 8
print(f"\nStorage needed: {bytes_needed} bytes ({bits_needed} bits)")
# Uppercase vs lowercase
print("Notice the pattern between uppercase and lowercase:\n")
letters = ['A', 'a', 'B', 'b', 'Z', 'z']
for letter in letters:
    print(f"'{letter}' = {ord(letter):3d} = {bin(ord(letter))}")

print("\nDifference: lowercase = uppercase + 32")
print("In binary, that's flipping one bit!")
# Modern Unicode: Emojis!
emojis = ['๐Ÿ˜€', 'โค๏ธ', '๐Ÿ', '๐Ÿ’ป', '๐Ÿš€']
print("Unicode for Emojis:\n")
for emoji in emojis:
    code = ord(emoji)
    print(f"{emoji} = {code} (needs more than 1 byte!)")

5. Computer Components: The Hardware#

The Main Parts of a Computer#

1. CPU (Central Processing Unit) ๐Ÿง 

  • The โ€œbrainโ€ of the computer

  • Performs calculations and executes instructions

  • Measured in GHz (billions of operations per second)

  • Example: Intel Core i7, Apple M1

2. RAM (Random Access Memory) ๐Ÿ“

  • Short-term memory (temporary workspace)

  • Fast but volatile (loses data when power off)

  • Holds currently running programs and data

  • Typical size: 8GB - 32GB

3. Storage (Hard Drive / SSD) ๐Ÿ’พ

  • Long-term memory (permanent storage)

  • Persistent (keeps data when power off)

  • Stores files, programs, operating system

  • HDD (slower, cheaper) vs SSD (faster, expensive)

4. GPU (Graphics Processing Unit) ๐ŸŽฎ

  • Specialized processor for graphics and parallel tasks

  • Essential for gaming, video editing, AI/ML

  • Example: NVIDIA GeForce, AMD Radeon

5. Motherboard ๐Ÿ”Œ

  • Connects all components together

  • The โ€œnervous systemโ€ of the computer

6. Input Devices โŒจ๏ธ๐Ÿ–ฑ๏ธ

  • Keyboard, mouse, touchscreen, microphone

  • How you communicate with the computer

7. Output Devices ๐Ÿ–ฅ๏ธ๐Ÿ”Š

  • Monitor, speakers, printer

  • How the computer communicates with you

How They Work Together#

Input โ†’ RAM โ†’ CPU โ†’ RAM โ†’ Output
        โ†‘     โ†“     โ†‘
        โ””โ”€โ”€ Storage โ”€โ”€โ”˜

Example: Opening a photo

  1. You click the photo file (Input)

  2. OS loads photo from Storage to RAM

  3. CPU processes image data

  4. GPU renders the image

  5. Image displays on Monitor (Output)


6. How Data is Represented#

Everything is stored as binary, but how?

Images ๐Ÿ–ผ๏ธ#

  • Pixels: Tiny dots of color

  • Each pixel stores RGB values (Red, Green, Blue)

  • Each color component: 0-255 (1 byte)

  • Total per pixel: 3 bytes

  • Example: 1920x1080 image = 2,073,600 pixels ร— 3 bytes = ~6MB

Audio ๐ŸŽต#

  • Sampling: Record sound at regular intervals

  • CD quality: 44,100 samples per second

  • Each sample: 2 bytes

  • Stereo (2 channels) ร— 44,100 ร— 2 bytes = ~172KB per second

  • 3-minute song = ~30MB uncompressed

Video ๐ŸŽฌ#

  • Sequence of images (frames) + audio

  • 30 frames per second (fps) is standard

  • HD video (1920x1080, 30fps) = ~60MB per second uncompressed!

  • Compression (H.264, H.265) reduces to ~5MB per second

Compression#

  • Reduces file size by removing redundant data

  • Lossy: Some quality lost (JPEG, MP3, MP4)

  • Lossless: Perfect quality (PNG, FLAC, ZIP)

# Calculate image storage size
def calculate_image_size(width, height, bits_per_pixel=24):
    """
    Calculate uncompressed image size.
    24 bits = 3 bytes (RGB)
    """
    total_pixels = width * height
    bytes_per_pixel = bits_per_pixel // 8
    total_bytes = total_pixels * bytes_per_pixel
    
    print(f"Image: {width}x{height}")
    print(f"Total pixels: {total_pixels:,}")
    print(f"Bytes per pixel: {bytes_per_pixel}")
    print(f"Uncompressed size: {bytes_to_readable(total_bytes)}")
    print()

# Common image resolutions
calculate_image_size(640, 480)    # VGA
calculate_image_size(1920, 1080)  # Full HD
calculate_image_size(3840, 2160)  # 4K

7. Operating Systems#

An Operating System (OS) manages all computer resources and provides services to programs.

What Does an OS Do?#

  1. Manages Hardware

    • Controls CPU, memory, storage, devices

    • Allocates resources to programs

  2. Provides User Interface

    • GUI (Graphical User Interface) - Windows, icons, menus

    • CLI (Command Line Interface) - Text commands

  3. Runs Programs

    • Loads programs into memory

    • Manages multiple programs at once (multitasking)

  4. Manages Files

    • Organizes files in folders

    • Handles reading/writing files

  5. Security

    • User accounts and permissions

    • Protects against malware


8. Networks and the Internet#

How Computers Communicate#

Network: Connected computers that can share data

Internet: The worldwide network of networks

Key Concepts#

1. IP Address

  • Unique identifier for each device

  • Like a street address for your computer

  • Example: 192.168.1.1 (IPv4)

2. Packets

  • Data broken into small chunks

  • Each packet travels independently

  • Reassembled at destination

3. Protocols

  • Rules for communication

  • HTTP/HTTPS: Web pages

  • TCP/IP: How packets are sent

  • DNS: Translates domain names to IP addresses

4. Bandwidth

  • How much data can transfer per second

  • Measured in Mbps (megabits per second)

  • Example: 100 Mbps = ~12 MB per second

What Happens When You Visit a Website?#

  1. You type โ€œwww.google.comโ€ in browser

  2. DNS converts domain to IP address

  3. Browser sends HTTP request to Googleโ€™s server

  4. Server sends back HTML, CSS, JavaScript

  5. Browser renders the page

All in milliseconds!


9. How Programs Work#

The Execution Cycle#

  1. Fetch: CPU gets instruction from memory

  2. Decode: CPU figures out what the instruction means

  3. Execute: CPU performs the operation

  4. Store: CPU saves the result

This happens billions of times per second!

Programming Languages#

Low Level (closer to computer):

  • Machine Code: Binary (0s and 1s)

  • Assembly: Symbolic machine code (MOV, ADD, SUB)

High Level (closer to human):

  • Python, JavaScript, Java, C++

  • Easier to read and write

  • Needs to be compiled or interpreted to machine code

Compilation vs Interpretation:

  • Compiled: Translated all at once (C, C++, Go)

  • Interpreted: Translated line-by-line (Python, JavaScript)


๐Ÿ“ Exercises#

Exercise 1: Binary Converter#

Create a program that:

  1. Takes a decimal number

  2. Converts it to binary

  3. Shows the place values

  4. Verifies by converting back

Test with: 10, 25, 100, 255

# Your code here

Exercise 2: Word Analyzer#

Create a function that takes a word and:

  1. Converts each letter to ASCII code

  2. Shows the binary representation

  3. Calculates total storage needed (bytes and bits)

Test with: โ€œCODEโ€, โ€œPYTHONโ€, โ€œHELLO WORLDโ€

# Your code here

Exercise 3: Storage Calculator#

Create a program that calculates storage for different media:

  • Text file: 1 byte per character

  • Image: width ร— height ร— 3 bytes

  • Audio: seconds ร— 44,100 ร— 2 bytes

Calculate storage for:

  • A 5-page essay (2000 characters)

  • A 1920ร—1080 photo

  • A 3-minute song

# Your code here

Exercise 4: Secret Message Encoder#

Create a simple encoder/decoder:

  1. Convert text to ASCII codes

  2. Add 3 to each code (Caesar cipher!)

  3. Convert back to text

  4. Create decoder that subtracts 3

Test by encoding and decoding: โ€œHELLOโ€

# Your code here

Exercise 5: Download Time Calculator#

Create a function that calculates download time:

  • Input: file size (MB), internet speed (Mbps)

  • Remember: 1 Byte = 8 bits

  • Output: time in seconds and minutes

Calculate download time for:

  • 100 MB file at 10 Mbps

  • 5 GB movie at 100 Mbps

  • 50 GB game at 50 Mbps

# Your code here

Exercise 6: Computer Spec Analyzer#

Create a program that rates computer specs:

  • Rate CPU speed (GHz): >3.0 = Fast, 2.0-3.0 = Medium, <2.0 = Slow

  • Rate RAM (GB): >16 = Great, 8-16 = Good, <8 = Low

  • Rate Storage (GB): >500 = Spacious, 250-500 = Adequate, <250 = Limited

Test with different configurations and give an overall rating.

# Your code here

โœ… Self-Check Quiz#

Before moving on, make sure you can answer these:

  1. What is binary and why do computers use it?

  2. How many values can 1 byte (8 bits) store?

  3. Whatโ€™s the difference between a bit and a byte?

  4. How many bytes in a kilobyte? Why 1024 and not 1000?

  5. What is ASCII?

  6. What does the CPU do?

  7. Whatโ€™s the difference between RAM and storage?

  8. What is an operating system?

  9. What is an IP address?

  10. How is text stored in a computer?


๐ŸŽฏ Key Takeaways#

  • Binary is how computers store everything (0s and 1s)

  • 1 Bit = one 0 or 1

  • 1 Byte = 8 bits (can store 0-255)

  • Storage hierarchy: Byte โ†’ KB โ†’ MB โ†’ GB โ†’ TB

  • ASCII/Unicode maps characters to numbers

  • CPU = brain (processes), RAM = short-term memory, Storage = long-term memory

  • Operating System manages hardware and runs programs

  • Internet = worldwide network of computers

  • Everything (text, images, audio, video) is ultimately stored as binary


๐Ÿš€ Next Steps#

Congratulations! You now understand how computers work at a fundamental level!

Youโ€™ve completed the Easy level! ๐ŸŽ‰

You now have a solid foundation in:

  • โœ… Python programming basics

  • โœ… Variables and data types

  • โœ… Operations and conditionals

  • โœ… AI and machine learning concepts

  • โœ… How computers work

Ready for more?

  • Medium Level: Functions, loops, data structures, OOP

  • Developer Tools: tools/01_shell_basics.ipynb - Essential professional skills

  • Hard Level: Advanced algorithms, deep learning, computer systems


๐Ÿ’ก Pro Tips#

  1. Everything is binary: Understanding this helps debug and optimize code

  2. Memory matters: Efficient programs use less RAM and storage

  3. Compression is magic: Reduces file sizes dramatically

  4. Think in powers of 2: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024โ€ฆ

  5. Network speed: Bits vs Bytes - divide Mbps by 8 for MB/s

  6. Modern computers are fast: Billions of operations per second!

  7. But theyโ€™re still limited: Good code respects resource constraints


๐ŸŒŸ Fun Facts#

  • Apollo 11 (1969 moon landing) had only 4KB of RAM!

  • Your phone has over 1,000,000 times more RAM

  • The first 1GB hard drive (1980) weighed 550 pounds and cost $40,000

  • Today you can buy 1TB (1,000 GB) for $50

  • ENIAC (1945), the first computer, weighed 30 tons

  • Modern smartphones are millions of times more powerful

  • Every Google search involves thousands of computers

  • Netflix streams over 1 billion hours per week

  • The Internet transmits about 2.5 quintillion bytes per day!


Youโ€™ve learned the fundamentals - now go build amazing things! ๐Ÿš€