AskHandle

AskHandle Blog

Python Programs for Practice

May 30, 2025Jessy Chan3 min read

Python Programs for Practice

Python is a versatile and user-friendly programming language that is widely used in various fields, from web development to data science. One effective way to deepen your understanding of Python is through practice. By working on different programming problems and projects, you can hone your skills and become a more proficient coder. In this article, we will explore a variety of Python programs that you can use for practice and skill-building.

1. Palindrome Checker

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Writing a program to check if a given input is a palindrome is a common exercise for beginner Python programmers. Here's a simple example to get you started:

python
1def is_palindrome(word):
2    word = word.lower().replace(" ", "")
3    return word == word[::-1]
4
5print(is_palindrome("madam"))  # Output: True
6print(is_palindrome("hello"))  # Output: False

2. Fibonacci Sequence Generator

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. Implementing a program to generate the Fibonacci sequence up to a specified number of terms is a great way to practice iterative or recursive algorithms in Python. Here's a sample code snippet for generating the Fibonacci sequence:

python
1def fibonacci(n):
2    sequence = [0, 1]
3    while len(sequence) < n:
4        sequence.append(sequence[-1] + sequence[-2])
5    return sequence
6
7print(fibonacci(10))  # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

3. Prime Number Checker

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Writing a program to check if a given number is prime can help you practice basic mathematical computations and conditional statements. Here's a simple Python function to check for prime numbers:

python
1def is_prime(number):
2    if number < 2:
3        return False
4    for i in range(2, int(number ** 0.5) + 1):
5        if number % i == 0:
6            return False
7    return True
8
9print(is_prime(17))  # Output: True
10print(is_prime(15))  # Output: False

4. Word Count Tool

Developing a program that counts the occurrences of each word in a given text can help you practice working with strings, dictionaries, and basic file I/O operations in Python. Here's a simple example of a word count tool in Python:

python
1def word_count(text):
2    word_list = text.lower().split()
3    word_freq = {}
4    for word in word_list:
5        word_freq[word] = word_freq.get(word, 0) + 1
6    return word_freq
7
8sample_text = "Python is a powerful programming language that is widely used in data science."
9print(word_count(sample_text))

5. Number Guessing Game

Creating a number guessing game is a fun way to practice user input validation and conditional statements in Python. The program generates a random number and prompts the user to guess it within a specified range. Here's a simple number guessing game implementation:

python
1import random
2
3def number_guessing_game():
4    target_number = random.randint(1, 100)
5    while True:
6        guess = int(input("Guess a number between 1 and 100: "))
7        if guess < target_number:
8            print("Too low, try again!")
9        elif guess > target_number:
10            print("Too high, try again!")
11        else:
12            print("Congratulations! You guessed the number.")
13            break
14
15number_guessing_game()

6. Matrix Operations

Implementing basic matrix operations such as addition, multiplication, and transposition is a great exercise to practice working with nested lists and numerical computations in Python. Below is an example demonstrating matrix addition in Python:

python
1def matrix_addition(matrix1, matrix2):
2    result = []
3    for i in range(len(matrix1)):
4        row = []
5        for j in range(len(matrix1[0])):
6            row.append(matrix1[i][j] + matrix2[i][j])
7        result.append(row)
8    return result
9
10matrix_a = [[1, 2], [3, 4]]
11matrix_b = [[5, 6], [7, 8]]
12print(matrix_addition(matrix_a, matrix_b))  # Output: [[6, 8], [10, 12]]

7. Web Scraping

Web scraping involves extracting data from websites, and Python has powerful libraries such as BeautifulSoup and requests that make this task easier. By writing a web scraping program, you can practice parsing HTML content, extracting information, and processing data from the web. Here's a simple example using BeautifulSoup to extract article titles from a webpage:

python
1import requests
2from bs4 import BeautifulSoup
3
4url = "https://www.example.com"
5response = requests.get(url)
6soup = BeautifulSoup(response.content, "html.parser")
7titles = soup.find_all("h2")
8
9for title in titles:
10    print(title.text)

Practicing Python programs is essential for improving your coding skills and becoming a proficient Python programmer. By working on a variety of programming challenges and projects, you can enhance your problem-solving abilities, logic building, and algorithmic thinking. Start by exploring the programs mentioned in this article and then continue to create your own projects. Happy coding!