Learn/ 11th Computer Science/ Unit 2 /Short Questions

Unit 2: Python Programming — Short Questions

11th Class Computer Science · Unit 2: Unit 2: Python Programming

Introduction to Python Programming

1.Why is Python considered beginner-friendly?

Python's simple syntax (like plain English), readability, and dynamic typing make it easy to learn and use, reducing the learning curve for beginners.

2.What role does Python's large community play in its popularity?

A strong community provides extensive support, tutorials, libraries, and frameworks, enabling problem-solving across diverse fields and accelerating development.

3.How does Python's versatility benefit developers?

Python supports multiple domains like web development, data analysis, AI, automation, and game development, making it a one-stop solution for varied applications.

4.Why is the name "Python" often misunderstood?

The name comes from the comedy show Monty Python's Flying Circus, not the snake, reflecting the language's playful design philosophy.

5.What are the core steps in computer programming?

Writing code, compiling/interpreting, executing, and producing output. These steps translate human logic into machine-executable instructions.

6.Why is an IDE crucial for Python development?

IDEs like PyCharm or VS Code offer features like syntax highlighting, debugging, and code completion, streamlining development and reducing errors.

7.What is the purpose of the 'PATH' environment variable in Python installation?

Adding Python to PATH allows running Python scripts from any directory via the command line without specifying the full installation path.

Basic Python Syntax and Structure

8.How does Python's syntax differ from other languages?

Python uses indentation and minimal symbols (e.g., no semicolons), prioritizing readability over complex syntax.

9.What are comments in Python, and why are they important?

Comments (single-line '#' or multi-line '"""') explain code logic, improving readability and aiding collaboration.

10.How does dynamic typing in Python enhance flexibility?

Variables can hold any data type without prior declaration, allowing rapid prototyping and adaptable code.

11.Why are meaningful variable names critical in Python?

They make code self-documenting, reduce reliance on comments, and ease debugging and maintenance.

12.What are the rules for Python variable naming?

Names must start with a letter/underscore, avoid reserved keywords, use snakecase, and be case-sensitive.

13.How do integers and floats differ in Python?

Integers (int) are whole numbers, while floats (float) represent decimal values (e.g., age = 17 vs. price = 19.99`).

14.Why is input validation important in Python programs?

It ensures data integrity by converting user inputs (strings) to appropriate types (e.g., `int(input())`).

Operators and Expressions

15.What is an operator?

An operator is a symbol or sign used to perform a specific operation on data. For example, '+' is used for addition, '-' for subtraction, '*' for multiplication, and '/' for division. Operators help us do calculations or compare values in programming.

16.What is an expression?

An expression is a combination of variables, numbers, and operators that gives a result. For example, '2 + 3', 'a * b', or 'x > y' are all expressions. When the computer runs the code, it calculates the value of the expression.

17.How do arithmetic operators in Python handle division?

'/' returns a float (e.g., `10 / 3 = 3.33`), while '//' performs floor division (e.g., `10 // 3 = 3`).

18.How do comparison operators work in Python?

They evaluate relationships between values (e.g., '==', '!=', '>', '<=') and return Boolean results (`True`/`False`).

19.Why use logical operators ('and', 'or', 'not') in conditions?

To combine multiple conditions (e.g., 'x > 5 and x < 10') for complex decision-making.

20.What is operator precedence in Python?

Operator precedence tells which operator is used first in an expression. Python follows rules to decide the order of operations like, '+, and '-'. It helps to get the correct result from a complex expression.

Control Structures

21.What is the use of a control structure?

In programming, we often need to control the flow of our program based on different conditions or repeat certain actions multiple times.

22.How many types of control structures?

There are two main types of control structures:
1. Decision making
2. Looping

23.What is the purpose of if statement?

The if statement allows us make decisions based on conditions. If the condition is true, it runs a block of code.

Syntax
if condition:
statement

24.What are use of if-else?

The if-else statement allows us to execute one block of code if a condition is true and another block if the condition is false.

Syntax
if condition:
statement
else
statement

25.Define Short Hand if-else Statement.

Python also allows a short-hand if-else statement that can be written in a single line.
#Syntax of short hand if-else statement
actioniftrue if condition else actioniffalse

26.What is the purpose of the `if-elif-else` structure?

It enables multi-way branching, allowing different code blocks to execute based on varying conditions.

27.How do you handle multiple conditions in Python?

Using 'if-elif-else' chains or nested conditionals to evaluate complex logical scenarios.

Loops

28.How does a `while` loop differ from a `for` loop?

`while` repeats as long as a condition is true (e.g., counting to 10), while `for` iterates over a sequence (e.g., list items).

29.What is the `range()` function used for in loops?

It generates a sequence of numbers for controlled iterations (e.g., 'for i in range(5)' runs 0-4).

Python Modules and Built-in Data Structures

30.Why are functions essential in Python?

They promote code reuse, modularity, and organization by encapsulating logic into callable blocks.

31.How do default parameters enhance function flexibility?

They allow optional arguments (e.g., 'greet(name="Student")), making functions adaptable to varying inputs.

32.How do modules improve code management in Python?

They group related functions/classes into files, enabling reuse across projects and reducing redundancy.

33.What is the purpose of the 'import' statement?

It loads external modules/libraries (e.g., 'import math') to access pre-built functionality.

34.Why are Python lists considered versatile?

They support multiple data types, dynamic resizing, and methods like append() and sort() for flexible data manipulation.

35.What are the use of Package Structure in Python?

To manage large projects, you can organize modules into packages. A package is simply a directory (folder) containing related modules. For example, if you're building an e-commerce platform, you could create a package named ecommerce with modules like products.py, customers.py, and orders.py.

Built-in Data Structures

36.How do tuples differ from lists?

Tuples are immutable (unchangeable) and use parentheses, while lists are mutable and use square brackets.

37.What is slicing in Python sequences?

It extracts subsets of data (e.g., 'list[1:4]') using start, stop, and step indices for efficient data handling.

38.Why use negative indices in Python?

They allow reverse traversal (e.g., 'list[-1]' accesses the last item), simplifying operations like retrieving end elements.

39.What are some common Methods and Operations on Lists?

Python provides several built-in methods to work with lists:
• append(item) – Adds an item to the end of the list.
Example: mylist.append(5) adds 5 to the end of the list.
• remove(item) – Deletes the first time that item appears in the list.
Example: mylist.remove(3) removes the first 3 it finds.
• sort() – Arranges the list in order, from smallest to biggest.
Example: mylist.sort() sorts numbers like 1, 2, 3, etc.
• reverse() – Flips the list so the last item comes first.
Example: mylist.reverse() turns [1, 2, 3] into [3, 2, 1].

40.How do dictionaries differ from lists in Python?

Dictionaries store key-value pairs for fast lookups, while lists maintain ordered, indexed collections.

Modular Programming Python

41.What is modular programming in Python?

Modular programming is a technique used to divide a program into smaller, manageable, and reusable pieces called modules. By breaking a program into modules, developers can work on different parts independently and reuse code efficiently. This approach simplifies managing complex programs and promotes code reuse.

42.Why is modular programming beneficial?

It breaks programs into manageable modules, enhancing maintainability, collaboration, and code reuse.

43.What is the significance of the 'main()' function in modular programming?

It defines the program's entry point, ensuring code runs only when executed directly, not when imported.

Object-Oriented Programming in Python

44.Define Object - Oriented Programming (OOP).

Object-Oriented Programming (OOP) in Python is a way of organizing and structuring code by creating classes and objects. A class is a template or blueprint for creating objects, and an object is an instance of a class with specific attributes and behaviors.

45.How do classes and objects relate in OOP?

A class is like a template for creating things, and an object is an actual thing created from that template. Imagine you want to make a toy car. You first need a blueprint or a template that describes how the toy car should look and function. This template includes details like:
• Color
• Size
• Number of wheels
• Type of material

Advanced Python Concepts

46.Why is file handling essential in Python?

It enables persistent data storage (e.g., reading/writing text or binary files) for applications like logging or configuration.

47.Define Exception Handling in python.

Exception handling is a mechanism to manage errors that occur during program execution. It allows a program to continue running or gracefully terminate if an error occurs, ensuring more robust and error-resilient code.

48.What is the use of Try-Except Blocks in Python?

In Python, the try block lets you test a block of code for errors, and the except block lets you handle errors if occur.

49.What is the advantage of using the 'with' statement for file handling?

It automatically closes files after operations, ensuring resource safety even if errors occur.

50.How do `write()` and `append()` modes differ in file handling?

'w' overwrites existing content, while 'a' adds new data to the end of the file.

Testing and Debugging in Python

51.What is unit testing, and how is it implemented in Python?

It tests individual code units (e.g., functions) using the 'unittest' module to validate correctness.

52.What is the role of the `pdb` module in debugging?

It provides an interactive debugger to step through code, inspect variables, and analyze runtime behavior.