Python Programming Guide

Comprehensive Chapter 5 Review

By Ramkrishna

1. Language Fundamentals

Q: Why is Python described as a "Dynamically Typed" language?
A: In Python, you don't need to declare the data type of a variable (like int x in C++). The interpreter determines the type based on the value assigned to it during runtime. Furthermore, a variable can change its type simply by being reassigned to a different value.
Q: What is the purpose of Comments, and what are the types?
A: Comments are used to explain code and make it readable; they are ignored by the interpreter.
  • Single-line: Starts with #.
  • Multi-line/Docstrings: Enclosed in triple quotes (''' or """).
Q: Interactive Mode vs. Script Mode?
A: Interactive Mode (REPL) executes code line-by-line and is best for quick debugging. Script Mode involves saving code in a .py file, allowing for complex, reusable programs.

2. Data Types & Storage

Q: Explain the concept of Mutability.
A:
  • Immutable (Unchangeable): int, float, string, tuple. If you modify them, a new memory address is created.
  • Mutable (Changeable): list, dictionary, set. You can change their content without changing their identity/memory address.
Q: What is the difference between an Identifier and a Keyword?
A: An Identifier is a user-defined name (variable, function name). A Keyword is a reserved word (if, while, def) that has a fixed meaning in Python and cannot be used as an identifier.

3. Advanced Operators & Output

Q: Explain the sep and end parameters in the print() function.
A:
  • sep: Defines the character between multiple values. Default is a space.
  • end: Defines what is printed at the very end of the line. Default is a newline (\n).
Example: print("Hi", "Ram", sep="-") outputs Hi-Ram.
Q: How do is and == differ?
A: == is an Equality operator; it checks if the values are the same. is is an Identity operator; it checks if both variables point to the exact same object in memory.

4. Error Handling (Debugging)

Q: Can you identify the three main types of programming errors?
A:
  1. Syntax Errors: Grammar mistakes (e.g., missing :). The program won't start.
  2. Runtime Errors: Unexpected events during execution (e.g., 10 / 0). The program crashes.
  3. Logical Errors: The code runs but the math/output is wrong (e.g., using + instead of *). These are the hardest to find.