๐จโ๐ป 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).
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)
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
AnimationThis flowchart checks if N is even (N%2==0). Now try coding it below in the Python emulator!
Python Emulator โ Run Code in Browser
InteractiveWrite Python code below and click โถ Run to execute. Supports print(), input(), loops, functions, lists and more.
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).
โข 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.