Chapter 2 Control Statements

  -Give the purpose of selection statements in Python.

Selection statements (like if, elif, and else) in Python serve the purpose of making decisions in the code. They allow the program to choose different paths or actions based on the evaluation of conditions. 


- List the types of type conversion in Python.

Implicit Type Conversion (Coercion):

Automatically performed by the interpreter.

Example: Converting an integer to a float during arithmetic operations.


x = 5

y = 2.0

result = x + y  # Implicitly converts x to float


Explicit Type Conversion (Casting):

Done by the programmer using predefined functions like int(), float(), str(), etc.

Example: Converting a string to an integer.


- Explain backward indexing in strings.

Backward indexing in strings refers to the ability to access individual characters in a string by counting positions from the end of the string, rather than from the beginning. In Python, backward indexing is supported using negative indices.

Here's how backward indexing works:

  • The last character in the string has an index of -1.
  • The second-to-last character has an index of -2.
  • And so on, moving towards the beginning of the string.


  • Write a Python program to display power of 2 using an anonymous function.
# Anonymous function to calculate the power of 2
power_of_2 = lambda x: 2 ** x

# Displaying powers of 2 from 0 to 10
for i in range(11):
    result = power_of_2(i)
    print(f"2^{i} = {result}")