Programming_Assignment_Unit_5_Final
Programming_Assignment_Unit_5_Final
Md Khorshed Alam
Shanthini S
In this unit, we explored the fundamental concepts of Iterations and Strings in Python. This
Each operation is broken down into a separate function to make the code more modular and
reusable.
Python Code:
return name[:n]
def count_vowels(name):
vowels = 'aeiou'
count = 0
if char in vowels:
count += 1
return count
def reverse_name(name):
return name[::-1]
# Main program
3
Output:
Explanation:
The revised program introduces modular programming using three well-defined functions:
display_left_chars(), count_vowels(), and reverse_name(). This structure aligns with the best
practices of breaking down a problem into smaller subproblems for clarity and maintainabil-
ity.
The function display_left_chars(name, n) uses string slicing to extract the first `n` characters
from the input name. It returns a substring starting from index 0 up to, but not including, in-
dex `n`.
The count_vowels(name) function counts how many vowels appear in the string. It iterates
over each character and checks its presence in the string 'aeiou'. It converts text to lowercase
The reverse_name(name) function returns the reversed string using Python’s slicing syntax
Each function handles one task, making the code cleaner and easier to debug or expand. For
instance, if the program later needed to ignore whitespace or count only distinct vowels, these
This approach not only demonstrates an understanding of iterations and strings (Chapters 7 &
8 of Downey, 2015), but also encourages early habits in functional abstraction — a valuable
Program Screen:
5
Output Screen:
6
References
Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green