Python Reference -- PAGE UNDER CONSTRUCTION --

Python & Programming Concepts

Created by Guido van Rossum (Netherlands, 1991), Python's syntax mirrors the English language, letting developers accomplish more with fewer lines of code. It runs cross-platform on Windows, Mac, and Linux.

Python is an interpreted (scripting) language — the interpreter executes code line-by-line at run-time, unlike compiled languages that translate the entire program before execution.

Variables

Named containers for storing and referencing data values in a program.

§ 01

In Python, a variable is created at the moment you first assign a value — no explicit declaration needed. Variables are dynamically typed and can change type after being set.

variables.py
x = 5 y = "John" # Multiple assignment x, y, z = "Orange", "Banana", "Cherry" a = b = c = "Orange" # Global keyword inside function count = 0 def increment(): global count count += 1
  • Created on first assignment — no declaration command
  • Dynamically typed; type can change at any time
  • Cannot begin with a number — only letters or underscore
  • Case-sensitive: age, Age, and AGE are three distinct variables
  • Alphanumeric + underscore only; no hyphens or spaces
  • String and int cannot be concatenated directly with +
  • Use global keyword to modify global variable inside a function
W3Schools — Python Variables
JavaScript
variables.js
var a = 27; let b = "hello"; const PI = 3.14;

Use var, let, or const. Values in quotes. End statements with semicolon. Prefer const/let over var in modern JS.

PHP
variables.php
$name = "Alice"; $score = 42;

All variables must begin with $. Semicolons required. No type declaration needed.

Input and Output

Reading data from the user and displaying results — a program's primary communication channel.

§ 02
Python
io_example.py
name = input("Enter your name: ") print("Welcome,", name) print(f"Hello, {name}!") # f-string
More on Python I/O ↗
JavaScript PHP
name = prompt("Your name?") Use $_POST or $_GET superglobals for form input
alert("Hello, " + name)
console.log(name)
echo "Hello " . $name;
print("Hello");

Strings

Ordered sequences of characters enclosed in quotes. The most common data type in web programming.

§ 03

In Python, strings are arrays of characters — you can index and slice them. An escape character is a backslash \ followed by the intended character.

strings.py
a = "Hello, World!" print(a[1]) # → 'e' print(a[0:5]) # → 'Hello' (slice) print(len(a)) # → 13 print(a.upper()) # → 'HELLO, WORLD!' # Escape characters b = "She said \"hi\"" c = "Line1\nLine2" # \n = newline
  • Strings are arrays — access via index: a[0]
  • Slicing: a[start:end] — end index is exclusive
  • Escape with backslash: \" \' \n \t
W3Schools — Python Strings ↗
JavaScript PHP
document.write("Hi " + aName); $s = "this is a string";
Template literals: `Hello ${name}` Concatenate with . (dot): "Hi " . $name
"text".length strlen($s)

Arrays & Collections

Python provides four built-in collection types — each suited to different data-storage needs.

§ 04
Array syntax: list_name = [item1, item2, ...] — e.g. cars = ["Saab", "Volvo", "BMW"]
JavaScript
arrays.js
// Array literal (preferred) const names = ["Sam", "Jo"]; // Constructor syntax var arr = new Array(2); arr[0] = "Sam";
PHP
arrays.php
// Indexed array $cars = ["Saab", "Volvo", "BMW"]; // Associative (key→value) $ages = ["Alice" => 25];

PHP's powerful associative arrays work like Python dictionaries, with many built-in functions.

list

List

General-purpose ordered collection. The Python equivalent of an array.

Ordered Changeable Duplicates ✓
x = ["apple", "banana", "cherry"] cars = ["Saab", "Volvo", "BMW"] x.append("date") # add item x.remove("apple") # remove item

Use square brackets [ ]

tuple

Tuples

Immutable ordered collection. Faster iteration than lists; can be used as dictionary keys.

Ordered Immutable Duplicates ✓
tup1 = ('physics', 'bio', 2000) tup2 = (1, 2, 3) tup3 = (50,) # single item needs comma tup4 = () # empty tuple

Use round brackets ( )

set

Sets

Unordered, unindexed. Ideal for membership testing and removing duplicates.

Unordered No Duplicates
fruits = {'apple', 'banana', 'cherry'} print("apple" in fruits) # True fruits.add("date") # add one fruits.update(["fig"]) # add many for x in fruits: print(x)

Use curly brackets { }

dict

Dictionaries

Key-value store. Keys must be unique and immutable; values are freely changeable.

Key:Value Changeable No Dup Keys
purse = {} purse['money'] = 10 purse['candy'] = 'pink' # Inline syntax: d = {'key': 'value', 'n': 42} d['key'] = 'updated' # change value

Syntax: {key: value, ...}

Conditional Statements — if | else

Control structures alter the flow of execution. Flavours: if, if|else, if|elif|else.

§ 05
if_else.py
# if | else num = 3 if num >= 0: print("Positive or Zero") else: print("Negative number")
if_elif_else.py
# if | elif | else num = 3 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
JavaScript & PHP: Conditional syntax is nearly identical in both — curly-brace blocks: if (expr) { ... } else if (expr) { ... } else { ... }

Loops & Iterations

while — indefinite, runs until condition is false. for — definite, iterates a sequence.

§ 06
while_loop.py
# Syntax while <expression>: <statements> # Example i = 0 while i < 5: print(i) i += 1
for_loop.py
# Syntax for <variable> in <iterable>: <statements> # Example fruits = ["apple", "banana"] for f in fruits: print(f) # Range-based for i in range(5): print(i)
JavaScript

Supports for, while, do...while, for...of (arrays), and for...in (object keys).

PHP

Loop syntax mirrors JavaScript: for ($i=0; $i<5; $i++) and foreach ($arr as $key => $val) for arrays.

Functions

Reusable named blocks of code. Define once, call anywhere. Reduces duplication.

§ 07
functions.py
# Define with def keyword def greet(name): print(name + " Roger") greet("Mat") # → "Mat Roger" # With return value def add(a, b): return a + b result = add(3, 4) # → 7
JavaScript
functions.js
// Declaration function greet(name) { console.log("Hello, " + name); } // Arrow function (ES6) const add = (a, b) => a + b;
PHP
functions.php
function greet($name) { echo "Hello, " . $name; } greet("Alice");

Operators

Symbols that perform operations on variables and values.

§ 08
Operator Name Description Python Example
**ExponentRaise left to the power of right2 ** 8 → 256
+  -  *  /ArithmeticStandard mathematical operationsa + b
%ModulusRemainder after division10 % 3 → 1
//Floor DivisionDivision rounded down to nearest integer9 // 2 → 4
==  !=  <  >  <=  >=ComparisonCompare two values; returns booleanx == y
and  or  notLogicalCombine or negate boolean expressionsx > 0 and x < 10
=  +=  -=  *=AssignmentAssign or update a variable's valuex += 5
in  not inMembershipTest if value exists in sequence"a" in ["a","b"]
is  is notIdentityCompare object identity (memory address)x is None

Environment Setup & IDEs

Tools and resources to get your Python development environment running.

§ 09
IDE Options

An IDE (Integrated Development Environment) combines a code editor, debugger, and build tools in one application.

  • PyCharm — Full-featured Python IDE (JetBrains)
  • VS Code — Lightweight, highly extensible, most popular
  • Atom — Open-source hackable editor (GitHub)
  • Eclipse — Java-based, multi-language with PyDev plugin
  • Repl.it — Free web-based IDE — no installation needed
Setup Python Environment Tutorial
Flowchart & Diagram Tools

Visualise algorithms and pseudocode before writing a single line of code.

Tip: Sketching the logic as a flowchart first dramatically reduces debugging time for complex programs.