Simple Python Projects for Beginners

After enrolling in the Python Course, you will get various kinds of live projects to provide you with experience. Along with this, you will also have to complete the assignments and projects to showcase your skills. Here are some of the simple Python Projects along with their Python codes you should go for as a beginner.

· Guess the Number Game- It consists of building a guessing game where the computer has to guess the correct number. In this project, you will have to work with Python’s random module and work towards building functions and getting the user input.

import random

def guess_number():

# Generate a random number between 1 and 100

secret_number = random.randint(1, 100)

# Initialize guess count

guess_count = 0

print(“Welcome to Guess the Number Game!”)

print(“I have chosen a number between 1 and 100. Try to guess it!”)

while True:

guess = int(input(“Enter your guess: “))

guess_count += 1

if guess < secret_number:

print(“Too low! Try again.”)

elif guess > secret_number:

print(“Too high! Try again.”)

else:

print(f”Congratulations! You’ve guessed the number {secret_number} correctly in {guess_count} guesses!”)

break

if __name__ == “__main__”:

guess_number()

·  Rock, paper, scissors- This is a simple random choice game that ensures that you will have to work with if statements and get the user input. Along with it, this project is a great choice and it will help you build your fundamentals like conditionals and functions.

import random

def get_user_choice():

while True:

user_choice = input(“Enter your choice (rock, paper, or scissors): “).lower()

if user_choice in [“rock”, “paper”, “scissors”]:

return user_choice

else:

print(“Invalid choice! Please enter rock, paper, or scissors.”)

def get_computer_choice():

return random.choice([“rock”, “paper”, “scissors”])

def determine_winner(user_choice, computer_choice):

if user_choice == computer_choice:

return “It’s a tie!”

elif (user_choice == “rock” and computer_choice == “scissors”) or

(user_choice == “paper” and computer_choice == “rock”) or

(user_choice == “scissors” and computer_choice == “paper”):

return “You win!”

else:

return “Computer wins!”

def play_game():

print(“Welcome to Rock, Paper, Scissors!”)

while True:

user_choice = get_user_choice()

computer_choice = get_computer_choice()

print(f”You chose {user_choice}. Computer chose {computer_choice}.”)

print(determine_winner(user_choice, computer_choice))

play_again = input(“Do you want to play again? (yes/no): “).lower()

if play_again != “yes”:

print(“Thanks for playing!”)

break

if __name__ == “__main__”:

play_game()

· Hangman- This Python project will help you learn how to work with dictionaries, lists, and nested if statements. Furthermore, this project will also ensure that you are capable of learning how to work with the string.

import random

def choose_word():

words = [“apple”, “banana”, “orange”, “grape”, “strawberry”, “kiwi”, “melon”, “peach”]

return random.choice(words)

def display_word(word, guessed_letters):

display = “”

for letter in word:

if letter in guessed_letters:

display += letter

else:

display += “_”

return display

def hangman():

word = choose_word()

guessed_letters = []

attempts_left = 6

print(“Welcome to Hangman!”)

print(“Try to guess the word.”)

while True:

print(“n” + display_word(word, guessed_letters))

print(f”Attempts left: {attempts_left}”)

guess = input(“Guess a letter: “).lower()

if len(guess) != 1 or not guess.isalpha():

print(“Please enter a single letter.”)

continue

if guess in guessed_letters:

print(“You already guessed that letter.”)

continue

guessed_letters.append(guess)

if guess not in word:

attempts_left -= 1

print(“Incorrect guess!”)

if attempts_left == 0:

print(“You ran out of attempts. The word was:”, word)

break

else:

print(“Correct guess!”)

if “_” not in display_word(word, guessed_letters):

print(“Congratulations! You guessed the word:”, word)

break

play_again = input(“Do you want to play again? (yes/no): “).lower()

if play_again == “yes”:

hangman()

else:

print(“Thanks for playing!”)

if __name__ == “__main__”:

hangman()

·  Countdown Timer- The countdown timer is a great beginner project that will help you become used to working with the loops in Python. Along with this, you will use the Time Python module for this project.

import time

def countdown_timer(seconds):

print(f”Countdown started for {seconds} seconds.”)

while seconds > 0:

print(seconds)

time.sleep(1)  # Wait for 1 second

seconds -= 1

print(“Time’s up!”)

if __name__ == “__main__”:

seconds = int(input(“Enter the number of seconds to countdown: “))

countdown_timer(seconds)

·  Tic-Tac-Toe- In this project, you will be able to build a tic-tac-toe game with various players in the command line. Along with this, you will also learn how to work with Python’s time and math modules. 

import random 

def print_board(board):

for row in board:

print(“|”.join(row))

print(“-” * 5) 

def check_winner(board):

# Check rows

for row in board:

if row[0] == row[1] == row[2] != ” “:

return row[0] 

# Check columns

for col in range(3):

if board[0][col] == board[1][col] == board[2][col] != ” “:

return board[0][col] 

# Check diagonals

if board[0][0] == board[1][1] == board[2][2] != ” “:

return board[0][0]

if board[0][2] == board[1][1] == board[2][0] != ” “:

return board[0][2] 

return None 

def is_board_full(board):

for row in board:

if ” ” in row:

return False

return True 

def tic_tac_toe():

board = [[” ” for _ in range(3)] for _ in range(3)]

players = [“X”, “O”]

current_player = random.choice(players) 

print(“Welcome to Tic-Tac-Toe!”)

print_board(board) 

while True:

print(f”Player {current_player}’s turn.”)

row = int(input(“Enter row (0, 1, or 2): “))

col = int(input(“Enter column (0, 1, or 2): “)) 

if board[row][col] != ” “:

print(“That position is already taken. Try again.”)

continue 

board[row][col] = current_player

print_board(board) 

winner = check_winner(board)

if winner:

print(f”Player {winner} wins!”)

break 

if is_board_full(board

 · Sudoku Solver- This Sudoku solver project will teach you how to build a sudoku solver that uses the backtracking technique. This backtracking helps you in searching for every possible combination to help solve the problem.

def print_board(board):

for row in board:

print(” “.join(map(str, row)))

def find_empty_location(board):

for i in range(9):

for j in range(9):

if board[i][j] == 0:

return i, j

return None, None

def is_valid_move(board, num, row, col):

# Check row

if num in board[row]:

return False

# Check column

if num in [board[i][col] for i in range(9)]:

return False

# Check 3×3 grid

start_row, start_col = 3 * (row // 3), 3 * (col // 3)

for i in range(start_row, start_row + 3):

for j in range(start_col, start_col + 3):

if board[i][j] == num:

return False

return True

def solve_sudoku(board):

row, col = find_empty_location(board)

if row is None:  # If there’s no empty cell, the board is solved

return True

for num in range(1, 10):

if is_valid_move(board, num, row, col):

board[row][col] = num

if solve_sudoku(board):

return True

board[row][col] = 0  # Backtrack

return False

if __name__ == “__main__”:

# Example Sudoku board (0 represents empty cells)

sudoku_board = [

[5, 3, 0, 0, 7, 0, 0, 0, 0],

[6, 0, 0, 1, 9, 5, 0, 0, 0],

[0, 9, 8, 0, 0, 0, 0, 6, 0],

[8, 0, 0, 0, 6, 0, 0, 0, 3],

[4, 0, 0, 8, 0, 3, 0, 0, 1],

[7, 0, 0, 0, 2, 0, 0, 0, 6],

[0, 6, 0, 0, 0, 0, 2, 8, 0],

[0, 0, 0, 4, 1, 9, 0, 0, 5],

[0, 0, 0, 0, 8, 0, 0, 7, 9]

]

if solve_sudoku(sudoku_board):

print(“Sudoku Solved:”)

print_board(sudoku_board)

else:

print(“No solution exists.”)

Conclusion:

Enrolling in the Python Certification Course is a wise career choice and it can land you in high paying job opportunities. Along with this, you will learn numerous career-beneficial skills and gain vast industry-level experience. Furthermore, the Python course will equip you with industry-level expertise and help you in gaining high-paying jobs. In conclusion, given above are some of the best Python projects for beginners along with their codes.

 

 

Related posts