Python Viva Differences CheatSheet
Python Viva Differences CheatSheet
List vs Tuple
- List: Mutable, uses [], slower
- Tuple: Immutable, uses (), faster
'is' vs '=='
- 'is': Compares memory location (object identity)
- '==': Compares values
Python vs Java
- Python: Interpreted, dynamically typed, short syntax
- Java: Compiled, statically typed, verbose
break vs continue
- break: Exits the loop completely
- continue: Skips current iteration, continues loop
Mutable vs Immutable
- Mutable: Can be changed (e.g. list, dict)
- Immutable: Cannot be changed (e.g. int, str, tuple)
Function vs Method
- Function: Independent block of code
- Method: Associated with object/class
Compiler vs Interpreter
- Compiler: Converts whole code at once
- Interpreter: Converts code line by line (Python uses this)
append() vs extend()
- append(): Adds one element
- extend(): Adds multiple elements from iterable
Set vs Dictionary
- Set: Unique values, no keys
- Dictionary: Key-value pairs
range() vs xrange()
- range(): Returns list (Python 2), generator (Python 3)
- xrange(): Only in Python 2, memory efficient
input() vs raw_input()
- input(): Evaluates input (Python 2), returns string (Python 3)
- raw_input(): Returns string (Python 2)
del vs remove()
- del: Deletes by index
- remove(): Deletes by value
@staticmethod vs @classmethod
- staticmethod: No self/cls, utility functions
- classmethod: Uses cls, affects class-level data
len() vs __len__()
- len(): Built-in function
- __len__(): Internal method for len()
Error vs Exception
- Error: Critical, system-related
- Exception: Handled via try-except
Tuple vs String
- Tuple: Collection of values (any type)
- String: Sequence of characters only
Class vs Object
- Class: Blueprint
- Object: Instance of a class
Inheritance vs Encapsulation
- Inheritance: Reusability of code
- Encapsulation: Data hiding using access modifiers
Overloading vs Overriding
- Overloading: Same method name, diff args (not direct in Python)
- Overriding: Redefining method in subclass
Encapsulation vs Abstraction
- Encapsulation: Hides data using private vars
- Abstraction: Hides implementation using abstract classes
Constructor vs Method
- Constructor: __init__, auto-called when object created
- Method: Normal function inside class, called explicitly
is-a vs has-a
- is-a: Inheritance (Dog is-a Animal)
- has-a: Composition (Car has-a Engine)
Class Variable vs Instance Variable
- Class Variable: Shared across all objects
- Instance Variable: Unique to each object