Python Projects for Beginners: Learn by Doing

Akash Dev - Dec 9 '23 - - Dev Community

Welcome, dear readers. In this blog, I will be sharing five basic Python projects for beginners. I will soon be sharing more articles on the Python programming language, from which you can kick-start your learning journey. But until then, I have shared a few simple projects so that you can get your hands dirty with this language and understand the concepts better when we learn more. So without further ado, let’s get started. 😊

Password generator🔑

Image description

This Python script generates a random password based on user input for the desired number of alphabets, symbols, and numbers. It uses predefined character sets for each category, randomly selects characters from these sets, combines and shuffles them to create a unique password, and then prints the final result. The purpose is to provide a customizable and secure password-generation tool 💯.

import random

print("Welcome to the PyPassword Generator!")

#Take user input for the desired number of alphabets, symbols, and numbers in the password.
alphabets = int(input("How many alphabets would you like in your password?"))
symbols = int(input("How many symbols would you like?"))
numbers = int(input("How many numbers would you like?"))

#Define the three character sets
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
number = '0123456789'
symbol = '!#$%&()*+'

#Randomly select characters from each character set based on the user-input quantities.
random_alphabets = random.sample(letters, alphabets)
random_symbols = random.sample(symbol, symbols)
random_numbers = random.sample(number, numbers)

#Combine the characters into a list called password
password = random_numbers + random_symbols + random_alphabets
random.shuffle(password)

#Join them into a string then print that string
finalPass = ''.join(password)
print(finalPass)
Enter fullscreen mode Exit fullscreen mode

Rock-paper-scissor🪨📃✂️

Image description

This is a fun and simple rock-paper-scissor game 🎮. The player inputs their choice (1 for rock, 2 for paper, 3 for scissors), and the computer randomly selects its choice 🎲. The choices are then compared, and the winner or a draw is determined based on the game rules. The ASCII art adds a visual element to the game, displaying the chosen moves for both the player and the computer 🖥️.

import random

# ASCII art for rock, paper, and scissors
rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

# List of choices for the game
choices = [rock, paper, scissors]

# Get player input
player_entry = int(input("Enter your choice: 1 for rock, 2 for paper, 3 for scissors"))

# Validate player input
if player_entry < 1 or player_entry > 3:
    print("You entered an invalid number")
else:
    # Get player and computer choices based on input and random selection
    player_choice = choices[player_entry - 1]
    randomised = random.randint(0, 2)
    computer_choice = choices[randomised]

    # Display player and computer choices
    print(f"\nYou played:\n{player_choice}")
    print(f"\nAI played:\n{computer_choice}")

    # Determine the winner or if it's a draw
    if player_choice == rock and computer_choice == paper:
        print("You lose!")
    elif player_choice == paper and computer_choice == scissors:
        print("You lose!")
    elif player_choice == scissors and computer_choice == rock:
        print("You lose!")
    elif player_choice == paper and computer_choice == rock:
        print("You win!")
    elif player_choice == scissors and computer_choice == paper:
        print("You win!")
    elif player_choice == rock and computer_choice == scissors:
        print("You win!")
    elif player_choice == rock and computer_choice == rock:
        print("You drew.")
    elif player_choice == paper and computer_choice == paper:
        print("You drew.")
    elif player_choice == scissors and computer_choice == scissors:
        print("You drew.")
Enter fullscreen mode Exit fullscreen mode

Love calculator💝

Image description

This code is a simple love calculator. It takes two names as input, converts them to lowercase for case-insensitive comparison, and then calculates a "love score" based on the occurrences of the letters in the words 'true' and 'love.' The final score is then used to print a message indicating the level of compatibility between the two names.

print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")

# Combine names and convert to lowercase for case-insensitive comparison
name = (name1 + name2).lower()

# Count occurrences of each letter in "true"
t = name.count("t")
r = name.count("r")
u = name.count("u")
e = name.count("e")

# Count occurrences of each letter in "love"
l = name.count("l")
o = name.count("o")
v = name.count("v")
e = name.count("e")  # Note: there are two 'e' in love

# Calculate the 'true' and 'love' values
true = t + r + u + e
love = l + o + v + e

# Combine 'true' and 'love' values to get the final score
true_love = int(true) * 10 + int(love)

# Evaluate the score and print a message accordingly
if true_love < 10 or true_love > 90:
    print(f"Your score is {true_love}, you go together like coke and mentos.")
elif 40 <= true_love <= 50:
    print(f"Your score is {true_love}, you are alright together.")
else:
    print(f"Your score is {true_love}.")
Enter fullscreen mode Exit fullscreen mode

Caesar cipher🔍

Image description

This is a basic Caesar cipher encryption and decryption. The user is prompted to choose between encoding and decoding, input a message, and provide a shift value. The caeser function then processes the input and prints the result. The program continues to run until the user decides to exit by typing 'no'.

# List of alphabet characters
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# Function to perform Caesar cipher encryption or decryption
def caeser(text, direction, shift):
    final_text = ""
    for char in text:
        if char in alphabet:
            if direction == "encode":
                index = (alphabet.index(char) + shift) % 26
                final_text += alphabet[index]
            elif direction == "decode":
                index = (alphabet.index(char) - shift) % 26
                final_text += alphabet[index]
        else:
            final_text += char
    print("Your " + direction + "d message is:", final_text)

# Flag to control whether to continue or exit the program
should_continue = True

# Main loop for user interaction
while should_continue:
    # User input for encryption or decryption
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt\n")

    # User input for the message and shift
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))

    # Call the Caesar function with user inputs
    caeser(text, direction, shift)

    # Ask the user if they want to continue or exit
    result = input("Type 'yes' if you want to go again or else type 'no'\n")

    # Update the flag based on user input
    if result == "no":
        should_continue = False
        print("GoodBye.. 👋")
Enter fullscreen mode Exit fullscreen mode

Pizza cost calculator🍕

Image description

This code defines a function pizza that calculates the cost of a pizza based on its size, pepperoni choice, and extra cheese choice. The user is then prompted to input their preferences for pizza size, pepperoni, and extra cheese. The pizza function is called with these inputs, and the calculated bill is printed.

# Function to calculate the bill for a pizza based on size, pepperoni, and cheese choices
def pizza(size, add_pepperoni, extra_cheese):
    bill = 0

    # Calculate the base cost based on pizza size
    if size == "S":
        bill = 15
        if add_pepperoni == "Y":
            bill += 2
    elif size == "M":
        bill = 20
        if add_pepperoni == "Y":
            bill += 3   
    else:
        bill = 25
        if add_pepperoni == "Y":
            bill += 3

    # Add extra cost for cheese if chosen
    if extra_cheese == "Y":
        bill += 1

    return bill

# User input for pizza size, pepperoni, and extra cheese
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")

# Calculate and print the total bill using the pizza function
bill_amount = pizza(size, add_pepperoni, extra_cheese)
print("Your bill is $" + str(bill_amount))
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, I have shared five beginner-friendly Python projects. The projects include a dynamic password generator, an interactive game like rock-paper-scissors, and a whimsical love calculator. Each project has provided insights into key programming concepts. The pizza order system demonstrated the power of functions in making code modular and easy to understand. As you make these projects, remember to experiment, modify, and combine elements for a unique coding experience. Keep coding and enjoy the journey. 😊

Link to My GitHub Repo

If you found this article helpful, I would appreciate your support! 🚀 Don't forget to hit the like button and give it a thumbs up. 🌟 Follow me for more engaging content and updates.

Image description

Consider subscribing to my newsletter for a regular dose of insightful articles and exciting projects. 📬 Stay in the loop and join a community passionate about coding and technology.

Thank you! 🙌✨

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .