Ch#02: Python Programming - Solutions
Class Activities
#01: BMI Calculator (Pg#25)
Program to calculate Body Mass Index (BMI) using weight (kg) and height (m).
weight = float(input('Enter your weight in kilograms (kg): '))
height = float(input('Enter your height in meters (m): '))
bmi = weight / (height ** 2)
print(f'Your BMI is: {bmi:.2f}')
# Basic classification logic
if bmi < 18.5:
print('Classification: Underweight')
elif 18.5 <= bmi < 25:
print('Classification: Normal weight')
else:
print('Classification: Overweight')
#03: Even or Odd (Pg#27)
Checking if a number is even or odd using standard and shorthand if-else statements.
num = int(input('Enter a number: '))
# Standard if-else statement
if num % 2 == 0:
print('Even')
else:
print('Odd')
# Short-hand if-else statement
print('Even') if num % 2 == 0 else print('Odd')
#04: Positive, Negative, or Zero (Pg#28)
Using if-elif-else statement to check the nature of a number.
number = float(input('Enter a number: '))
if number > 0:
print('The number is positive.')
elif number < 0:
print('The number is negative.')
else:
print('The number is zero.')
#05: While Loop - Even/Odd (Pg#28)
Prints even numbers and counts odd numbers from 1 to 20 using a while loop.
i = 1
odd_count = 0
while i <= 20:
if i % 2 == 0:
print(f'{i} is Even')
else:
odd_count += 1
i += 1
print(f'Total odd numbers: {odd_count}')
#06: Range and For Loops (Pg#28)
Printing even numbers (2-10) and first 10 multiples of 3.
# Part 1: Even numbers from 2 to 10
print('Even numbers from 2 to 10:')
for i in range(2, 11, 2):
print(i)
# Part 2: First 10 multiples of 3
print('\nFirst 10 multiples of 3:')
for i in range(1, 11):
print(i * 3)
#07: List Max Value Function (Pg#29)
A function that returns the maximum value from a list.
def find_max(numbers):
if not numbers:
return None
return max(numbers)
# Example usage:
my_list = [10, 45, 2, 99, 30]
print(f'The maximum value is: {find_max(my_list)}')
#08: Favorite Books List (Pg#33)
List manipulation: Add, Replace, Remove, and Merge.
books = ['To Kill a Mockingbird', '1984', 'The Great Gatsby', 'Pride
and Prejudice']
[Link]('Moby Dick')
books[1] = 'Brave New World'
[Link]('The Great Gatsby')
extra_books = ['War and Peace', 'Hamlet']
[Link](extra_books)
print('Final list:', books)
#09: Slicing and Indexing (Pg#34)
Operations on list, tuple, and string sequences.
my_list = [10, 20, 30, 40, 50, 60, 70, 80]
my_tuple = ('Math', 'Science', 'English', 'History', 'Geography')
my_string = 'Python Programming'
# 3rd element
print(my_list[2], my_tuple[2], my_string[2])
# Slice 2 to 5
print(my_list[2:6])
print(my_tuple[2:6])
# String slice 7 to end
print(my_string[7:])
# Last two elements
print(my_list[-2:], my_tuple[-2:])
# String negative slice
print(my_string[-2:])
#10: Python Modules (Pg#35)
Calculator module and its usage in main script.
# --- [Link] ---
def add(a, b): return a + b
def subtract(a, b): return a - b
# --- [Link] ---
import calculator
print([Link](15, 8))
print([Link](25, 10))
Exercise Questions
#05: Positive/Negative/Zero (Pg#40)
num = float(input('Enter a number: '))
if num > 0: print('Positive')
elif num < 0: print('Negative')
else: print('Zero')
#06: Odd Numbers While Loop (Pg#40)
Prints odd numbers 1-100 and provides total count.
n = 1
count = 0
while n <= 100:
if n % 2 != 0:
print(n)
count += 1
n += 1
print(f'Total count: {count}')