Python Coding Dat 16 | Python Practice: Practical Coding Exercises

Python Practice: Practical Coding Exercises

Watch the lesson tutorial  ðŸ”»

Learning Python is best done by doing. In this article, we will look at two practical coding exercises that solve real-world problems. We will break down the logic, clean up the code, and explain exactly how they work.

Exercise 1: Paint Area Calculator

Imagine you are painting a wall. You know the height and width of the wall, and you know that one can of paint covers a specific amount of area (e.g., 5 square meters). How do you calculate exactly how many cans you need to buy?

You cannot buy half a can of paint, so even if you need 1.2 cans, you must round up to 2.

Code: Wall Paint Calculator

import math

def paint_calc(height, width, cover):
    # Calculate the area of the wall
    area = height * width
    
    # Calculate cans needed (area / coverage per can)
    num_of_cans = math.ceil(area / cover)
    
    print(f"You will need {num_of_cans} cans of paint.")

# Main program execution
test_h = int(input("Height of wall: "))
test_w = int(input("Width of wall: "))
coverage = 5

paint_calc(height=test_h, width=test_w, cover=coverage)

Code Explanation

  1. import math:

    We import the math module at the very top. This is required because we need a specific function called ceil (ceiling) to round our numbers up.

  2. Defining the Function (def paint_calc):

    We define a function that takes three arguments: height, width, and cover. This makes the code reusable for any wall size.

  3. The Calculation:

    • area = height * width: First, we find the total square meters of the wall.

    • math.ceil(area / cover): This is the most important line. If the area is 12 and coverage is 5, the result is 2.4. If we used standard rounding, it might round down to 2, leaving us with an unpainted spot. math.ceil() forces the number to round up to the nearest whole integer (3), ensuring we have enough paint.

  4. Calling the Function:

    We ask the user for input using int(input(...)), store those values in variables, and pass them into the function to get the result.

Exercise 2: Prime Number Checker

A prime number is a number that is only divisible by 1 and itself (e.g., 3, 5, 7, 11). If a number can be divided cleanly by any other number (like 4 is divisible by 2), it is not prime.

This exercise creates a function that checks if a user's number is prime.

Code: Prime Number Checker

def prime_checker(number):
    is_prime = True
    
    # Check numbers from 2 up to the number itself
    for i in range(2, number):
        # The modulo operator (%) gives the remainder
        if number % i == 0:
            is_prime = False
            
    if is_prime:
        print("That is absolutely a Prime Number")
    else:
        print("That is absolutely NOT a Prime Number")

# Main program execution
n = int(input("Check this number: "))
prime_checker(number=n)

Code Explanation

  1. The Flag (is_prime = True):

    We start by assuming the number is prime. We set a variable (often called a "flag") to True. We will only change this to False if we find proof that it isn't prime.

  2. The Loop (for i in range(2, number)):

    We need to check every number between 2 and the number just before our input.

    • Example: If the user types 5, the loop checks numbers 2, 3, and 4.

  3. The Modulo Check (number % i == 0):

    Inside the loop, we use %. This operator calculates the remainder of a division.

    • If number % i == 0, it means i divides into number perfectly with zero remainder.

    • If this happens, the number is not prime. We flip our flag: is_prime = False.

  4. Final Decision:

    After the loop finishes checking all possibilities, we look at our flag.

    • If is_prime is still True, the loop never found a divisor, so it is a Prime Number.

    • If is_prime is False, we found a divisor, so it is not a Prime Number.


Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python