This project contains a collection of Python functions that demonstrate the use of loops, conditionals, and basic algorithmic thinking. The project also includes unit tests using Python's unittest framework to verify that each function works correctly.
The goal of this assessment is to practice using loops in Python to solve common programming problems such as:
- Summing numbers
- Counting even numbers
- Calculating factorials
- Generating multiplication tables
- Working with strings
- Finding prime numbers
- Creating patterns
- Building multiplication grids
Each function is implemented in assessment1.py and tested using automated unit tests.
project-folder/
│
├── assessment1.py # Main file containing all loop-based functions
├── test_assessment.py # Unit tests for the functions
└── README.md # Project documentation
Returns the sum of numbers from 1 to n.
Example:
sum_to_n(5)
# Output: 15Counts how many even numbers exist between 1 and n (inclusive).
Example:
count_even_numbers(10)
# Output: 5Calculates the factorial of n using a loop.
Example:
factorial(5)
# Output: 120Returns the multiplication table of n from 1 to 10.
Example:
multiplication_table(3)
# Output: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]Returns the sum of all digits in a number.
Example:
sum_of_digits(1234)
# Output: 10Counts the number of vowels (a, e, i, o, u) in a string.
Example:
count_vowels("hello")
# Output: 2Returns a list of prime numbers from 2 up to n.
Example:
find_primes(10)
# Output: [2, 3, 5, 7]Reverses a given string.
Example:
reverse_string("hello")
# Output: "olleh"Generates a pyramid pattern of stars.
Example:
pyramid_pattern(3)
# Output:
["*", "**", "***"]Creates an n × n multiplication grid using nested loops.
Example:
multiplication_grid(3)
# Output:
[
[1, 2, 3],
[2, 4, 6],
[3, 6, 9]
]This project uses Python's built-in unittest framework.
To run the tests:
python -m unittestor
python test_assessment.pyAll tests should pass if the functions are implemented correctly.
- Python 3.x
- No external libraries required
This project demonstrates:
- Using for loops
- Working with ranges
- Applying conditional logic
- Using nested loops
- Performing string manipulation
- Writing unit tests
This project is open-source and available for educational purposes.
Name : Letsoenyo Clen Bongane