Computer ยท Chapter 07

๐Ÿ‘จโ€๐Ÿ’ป Programming Basics

Algorithms, flowcharts, Python emulator with 8 examples.

๐Ÿ‘จโ€๐Ÿ’ป Writing Instructions for Computers

Programming is the process of writing instructions (code) that a computer can execute. A program is a set of instructions. A programming language is the syntax used to write those instructions.

Levels of language:
โ€ข Machine Language โ€” binary (0s and 1s). Directly understood by CPU. Fastest but unreadable.
โ€ข Assembly Language โ€” mnemonics (MOV, ADD). Converted to machine code by assembler.
โ€ข High-level Language โ€” human-readable (Python, C, Java). Converted by compiler or interpreter.
Compiler โ€” translates entire program at once (C, C++). Interpreter โ€” translates line-by-line (Python, JavaScript).

Algorithm โ€” step-by-step solution to a problem. Properties: input, output, definiteness, finiteness, effectiveness.
Flowchart โ€” visual representation of algorithm. Symbols: Oval (start/end), Rectangle (process), Diamond (decision), Parallelogram (I/O), Arrow (flow).

๐Ÿ Python basics โ€” key concepts

Variables: x = 5  |   name = "Ravi"  |   pi = 3.14
Data types: int (5), float (3.14), str ("hello"), bool (True/False), list ([1,2,3]), dict ({key:val})
Input/Output: print("Hello")  |   name = input("Enter name: ")
If-else: if x > 0: ... elif x == 0: ... else: ...
Loops: for i in range(5): ...  |   while condition: ...
Functions: def greet(name): return "Hello " + name
Lists: nums = [1,2,3]  |   nums.append(4)  |   len(nums)

๐Ÿ“‹ Programming concepts โ€” exam critical

Variable โ€” named memory location. Constant โ€” value doesn't change.
Operator types: Arithmetic (+,-,*,/,//,%,**), Relational (==,!=,>,<), Logical (and, or, not), Assignment (=,+=,-=)
Array โ€” collection of same-type elements. Index starts from 0.
Function/Procedure โ€” reusable block of code. Function returns value, procedure does not.
Recursion โ€” function calling itself. Base case prevents infinite recursion.
OOP concepts: Class, Object, Inheritance, Encapsulation, Polymorphism, Abstraction.

๐ŸŽฌ

Flowchart Symbols & Algorithm Design

Animation
FLOWCHART โ€” "Find if number is EVEN or ODD" โ€” CLICK EACH SHAPE START INPUT number N N % 2 == 0 Is remainder zero? YES Print "EVEN" NO Print "ODD" END CLICK ANY SHAPE Each shape in a flowchart has a specific meaning. Following the arrows shows the execution path.

This flowchart checks if N is even (N%2==0). Now try coding it below in the Python emulator!

๐Ÿ’ป

Python Emulator โ€” Run Code in Browser

Interactive

Write Python code below and click โ–ถ Run to execute. Supports print(), input(), loops, functions, lists and more.

๐Ÿ Python 3
Output will appear here...
Practice (O-Level): Write a Python program to check if a number is prime.
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# Test the function
for num in range(1, 20):
    if is_prime(num):
        print(num, "is prime")

# Output: 2, 3, 5, 7, 11, 13, 17, 19

Key insight: We only need to check divisors up to โˆšn. If n has a factor greater than โˆšn, its corresponding factor must be less than โˆšn โ€” so we would have already found it. This makes the algorithm O(โˆšn) instead of O(n).
Practice (CCC): What is the difference between a compiler and an interpreter?
Compiler:
โ€ข Translates entire source code into machine code ALL AT ONCE before execution
โ€ข Creates an executable file (.exe, .out)
โ€ข Errors shown after complete compilation
โ€ข Faster execution (already translated)
โ€ข Languages: C, C++, Go, Rust, Fortran
โ€ข Example: gcc program.c -o program โ†’ ./program

Interpreter:
โ€ข Translates and executes source code LINE BY LINE
โ€ข No separate executable file created
โ€ข Errors shown as soon as problematic line is reached
โ€ข Slower execution (translates each time)
โ€ข Easier for debugging โ€” immediate feedback
โ€ข Languages: Python, JavaScript (browser), Ruby, PHP
โ€ข Example: python3 program.py

Hybrid approach (Java):
โ€ข Java compiler converts to bytecode (.class file)
โ€ข JVM (Java Virtual Machine) interprets bytecode
โ€ข "Write once, run anywhere" โ€” bytecode runs on any OS with JVM

Modern Python uses CPython which compiles to bytecode (.pyc) first, then interprets.
Modern JS engines (V8 in Chrome) JIT-compile JavaScript for speed.
โ†
Previous
Database & SQL