1.
Difference between For and While Loops
<b>For Loop</b> <b>While Loop</b>
Used when number of iterations is known. Used when number of iterations is unknown.
Works with sequences like range(), lists, or tuples. Continues until a condition becomes false.
Automatically increases the counter. Requires manual increment or update.
Simpler for counting tasks. Better for condition-based loops.
Stops when sequence ends. Condition checked before each iteration.
More readable for fixed loops. More flexible for dynamic conditions.
Initialization is inside loop header. Initialization done outside loop.
Executes block for each item in sequence. Executes block repeatedly while condition is true.
2. Difference between Do-While and While Loops (Simulated in
Python)
<b>Do-While Loop</b> <b>While Loop</b>
Executes at least once before checking condition. May not execute even once if condition is false.
Condition checked after executing loop body. Condition checked before executing loop body.
Simulated in Python using while True and break. Built-in loop type in Python.
Useful when we want to run code at least once. Used when condition must be checked first.
May use break to exit loop manually. Ends automatically when condition becomes false.
Less commonly used in Python. More commonly used in Python programs.
Emphasizes execution-before-condition. Emphasizes condition-before-execution.
Used in input validation programs. Used in counting or indefinite loops.
3. Difference between Arithmetic and Relational Operators
<b>Arithmetic Operators</b> <b>Relational Operators</b>
Used for mathematical calculations. Used to compare two values.
Include +, -, *, /, %, **, //. Include ==, !=, >, <, >=, <=.
Operate on numerical values. Operate on numbers, strings, or sequences.
Return numeric result. Return boolean result (True/False).
Used in calculations and expressions. Used in decision-making and conditions.
Can be combined in complex formulas. Can be used inside loops and if-statements.
Used to modify variable values. Used to control program flow.
Example: 5 + 3 = 8. Example: 5 > 3 returns True.
4. Difference between Assignment and Equality Operators
<b>Assignment Operator (=)</b> <b>Equality Operator (==)</b>
Used to assign values to variables. Used to compare two values.
Does not compare two values. Checks if values are equal.
Stores right-hand value to left variable. Returns True or False.
Example: x = 5 assigns value 5 to x. Example: x == 5 checks equality.
Used in initialization and updates. Used inside if and while conditions.
Cannot be used in conditional statements. Does not change variable values.
Performs variable assignment. Performs logical comparison.
Is a single equal sign. Is a double equal sign.
5. Difference between Implicit and Explicit Type Conversion
<b>Implicit Conversion</b> <b>Explicit Conversion</b>
Also called type coercion. Also called type casting.
Done automatically by Python. Done manually using functions like int(), float(), str().
No user involvement needed. Requires user involvement.
Occurs when mixing data types in expressions. Used when specific type conversion is needed.
Prevents data loss by converting smaller to larger types.
May cause data loss if incompatible.
Simplifies coding and reduces errors. Gives programmer more control.
Handled internally by interpreter. Explicitly written in code.
Example: int + float = float. Example: int('5') → 5.
6. Difference between Lists and Sets
<b>List</b> <b>Set</b>
Ordered collection of items. Unordered collection of unique items.
Allows duplicate elements. Does not allow duplicates.
Supports indexing and slicing. Does not support indexing or slicing.
Defined using square brackets []. Defined using curly braces {}.
Can contain mixed data types. Can contain only immutable elements.
Maintains insertion order. Does not maintain insertion order.
Elements can be modified. Elements cannot be accessed by index.
Example: [1, 2, 2, 3]. Example: {1, 2, 3}.
7. Difference between Tuples and Lists
<b>Tuple</b> <b>List</b>
Ordered collection of elements. Ordered collection of elements.
Immutable — cannot be changed. Mutable — can be changed.
Defined using parentheses (). Defined using square brackets [].
Faster than lists in execution. Slightly slower than tuples.
Uses less memory. Uses more memory.
Can be used as dictionary keys. Cannot be used as dictionary keys.
Safer for storing fixed data. Useful for dynamic data.
Example: (1, 2, 3). Example: [1, 2, 3].
8. Difference between Tuples and Dictionaries
<b>Tuple</b> <b>Dictionary</b>
Ordered collection of elements. Unordered collection of key-value pairs.
Elements are accessed by index. Elements are accessed by keys.
Defined using (). Defined using {} with key:value pairs.
Immutable and fixed once created. Mutable — can add or remove elements.
Stores simple ordered data. Stores data with relationships.
Faster for sequential access. Slightly slower due to key lookup.
Does not store key-value pairs. Stores mapping between values.
Example: (10, 20, 30). Example: {'a':10, 'b':20}.