Top Python Projects with Source Code for Beginners
1. Random Story Generator
It is one of the exciting beginner Python projects with source code. Every time a user runs the code, the random story generator project tries to produce a different and random story.
Used Module: Random module.
Source Code
Python
#importing module
import random
#Defining a list of phrases for sentence making
topic = ['DSA', 'Python', 'Java', 'Computer Networks',' Operating Systems']
adj = ['an important', 'an essential', 'very important', 'an interesting']
to = ['Developers', 'Student',' Professionals', 'Freshers']
supportive = ['learn it',' master it', 'read it', 'see it']
where = ['Coding Ninjas.', 'Code Studio.',' Code Studio Library.', 'CN guided paths.']
#generating the output
print(random.choice(topic) + ' is ' + random.choice(adj) + ' topic for ' + random.choice(to) + ', You can ' + random.choice(supportive) + ' from ' + random.choice(where))

You can also try this code with Online Python Compiler
Run Code
Output 1
Operating Systems is an important topic for Freshers, You can read it from CN guided paths.
Output 2
Computer Networks is an important topic for Freshers, You can see it from Code Studio.
Explanation
We are getting random output every time we compile and run this code. The story's random fragments, which are listed in various lists, can be selected through the random module.
2. Simple Email Slicer
Email Slicer is an essential project that accepts an email address as its input and produces the username and domain of the entered email address.
Used Function: Slicing Function
Source Code
Python
#user input
email = input("Please enter your email:").strip()
#definig username and domain parts
username_part = email[:email.index("@")]
domain_name = email[email.index("@")+1:]
output = (f"Your user name is '{username_part}' and your domain is '{domain_name}'")
#genrating the output
print(output)

You can also try this code with Online Python Compiler
Run Code
Input
Please enter your email:nidhi.singh@codingninjas.com
Output
Your user name is 'nidhi.singh' and your domain is 'codingninjas.com'
Explanation
We are using the strip function to remove any whitespace. The code will search for the index of the "@" sign in the user input. The index is used to divide the mail into username and domain parts.
3. Acronyms Creator
This project creates an acronym from the input string. An acronym is a short form of a word. For example, CN is an acronym for Coding Ninjas.
Used Functions: Splitting and Indexing
Source Code
Python
#User Input
input_string = str(input("Enter your Phrase: "))
#Using the splitting function
txt = input_string.split()
#Using indexing via for loop
n = " "
for i in txt:
n = n+str(i[0]).upper()
print(n)

You can also try this code with Online Python Compiler
Run Code
Input
Enter your Phrase: Natural language processing
Output
NLP
Explanation
In the code above, We first accept a string input from the user.
We will use Python's split() method for splitting the user input. We created a new variable, "n", to hold a phrase's acronym. We use the upper() method to get the Capital letters as acronyms.
4. Body Mass Index(BMI) Calculator
The formula for calculating a person's body mass index is to divide their weight(kg) by their height in meters squared. Let's now look at how to build a BMI calculator in Python.
Source Code
Python
H = float(input('Enter your height in meters: '))
W = float(input('Enter your Weight in Kg: '))
BMI = W / (H * H)
print ('Your Body Mass Index is: ', BMI)
if BMI > 0:
if BMI <= 16:
print 'You are severely underweight :('
elif BMI <= 18.5:
print 'You are underweight :('
elif BMI <= 25:
print 'Hooray! You are Healthy :)'
elif BMI <= 30:
print 'Oops! You are overweight :('
else:
print 'You are suffering from obesity.'
else:("Kindly enter valid details")

You can also try this code with Online Python Compiler
Run Code
Input
Enter your height in meters: 1.54
Enter your Weight in Kg: 48
Output
Your Body Mass Index is: 20.23950075898128
Hooray! You are Healthy :)
Explanation
In the code above, We first accept height and weight from the user.
We will use basic if-else statements to provide the output.
5. Dice Roll Simulator
We will see one of the basic Python projects with source code to simulate a die roll.
Used Module: Random Module
Used Function: random.randint() function
Source Code
Python
#importing random module for random number generation
import random
#range of a dice
minimum = 1
maximum = 6
#to loop the rolling through user input
Diceroll_again = "yes"
while Diceroll_again == "yes" or Diceroll_again == "y":
print("Rolling your Dice...")
print("The Values are :")
#generating and printing 1st random value
print(random.randint(minimum,maximum))
#generating and printing 2nd random value
print(random.randint(minimum,maximum))
Diceroll_again = input("Do you want to roll the Dice Again? ")

You can also try this code with Online Python Compiler
Run Code
Output 1
Rolling your Dice...
The Values are :
5
6
Do you want to roll the Dice Again?

You can also try this code with Online Python Compiler
Run CodeOutput 2
Rolling your Dice...
The Values are :
3
6
Do you want to roll the Dices Again? yes
Rolling your Dice...
The Values are :
6
5
Explanation
The code mentioned above can imitate a dice roll because the minimum and maximum values of a dice roll are 1 and 6, respectively. This provides the start and finish numbers for the random.randint() function that we will employ.
6. Basic Quiz Game
We will create a basic quiz game. The Quiz is over after all of the questions have been solved.
Source Code
Python
def check_ans(que, answer):
global score
still_answering = True
attempt = 0
while still_answering and attempt < 3:
if que.lower() == answer.lower():
print("Correct Answer")
score = score + 1
still_answering = False
else:
if attempt < 2:
que = input("Sorry Wrong Answer, try again")
attempt = attempt + 1
if attempt == 3:
print("The Correct answer is ",answer )
score = 0
print("Guess Correct Answer")
que1 = input(" Who developed the Python language? ")
check_ans(que1, "Guido van Rossum")
que2 = input(" What is the correct extension of the Python file? ")
check_ans(que2, ".py")
que3 = input("What do we use to define a block of code in Python language? ")
check_ans(que3, "Indentation")
print("Your total score is "+ str(score))

You can also try this code with Online Python Compiler
Run Code
Output
Guess Correct Answer
Who developed the Python language? Guido van Rossum
Correct Answer
What is the correct extension of the Python file? .py
Correct Answer
What do we use to define a block of code in Python language? Indentation
Correct Answer
Your total score is 3
Explanation
we'll begin by developing the questions and the process for answering them. The code that provides the player three chances to respond to each question will then be added.
Top Python Projects with Source Code for Intermediate
7. Rock, Paper, and Scissors Game
We will use Python here to develop a Rock, Paper, and Scissors game.
Used Module: Random Module
Source Code
Python
#importing random module
import random
#Declaring available resourses
options = ["Rock", "Paper", "Scissors"]
#Selecting random option for machine
machine = random.choice(options)
#Declaring scores
user = False
machine_score = 0
user_score = 0
while True:
user = input("Rock, Paper or Scissors? ").capitalize()
# Conditions of Rock, Paper and Scissors
if user == machine:
print("Tie!")
elif user == "Rock":
if machine == "Paper":
print("You lose!", machine, "covers", user)
machine_score+=1
else:
print("You win!", user, "smashes", machine)
user_score+=1
elif user == "Paper":
if machine == "Scissors":
print("You lose!", machine, "cut", user)
machine_score+=1
else:
print("You win!", user, "covers", machine)
user_score+=1
elif user == "Scissors":
if machine == "Rock":
print("You lose...", machine, "smashes", user)
machine_score+=1
else:
print("You win!", user, "cut", machine)
user_score+=1
elif user =='End':
print("Final Scores:")
print(f"Computer's score is:{machine_score}")
print(f"Your score is:{user_score}")
break

You can also try this code with Online Python Compiler
Run Code
Output
Rock, Paper or Scissors? paper
Tie!
Rock, Paper or Scissors? end
Final Scores:
Computer's score is:0
Your score is:0
Explanation
We are comparing the user’s choice with the computer(machine). The machine's choice is selected using the random module in Python from a list of choices. If the user wins, then the user_score will increase by 1; else, machine_score will increase by 1.
8. Chatbot
A chatbot is a computer programme designed to simulate interaction with human users, particularly online. Generally, there are two types of chatbots:
- Rule-based: based on some predefined rules.
- Self-learning: based on machine learning algorithms.
Here, we will design a rule-based chatbot.
Used Library: NLTK( Natural Language Toolkit)
Used Module: Chat
Used Dictionary: Reflections
Source Code
Python
# Importing libraries
from nltk.chat.util import Chat, reflections
#Creating a list of responses
responses = [
[
r"(hi|hey|hello|hola|holla)(.*)",
["Hello", "Hey there","Hello user", "Hello friend"]
],
[
r"(.*) your name ?",
["My name is Ninja and I'm a chatbot.",]
],
[
r"(.*)help(.*) ",
["I can help you.",]
],
[
r"how are you (.*) ?",
["i am great !, i am fine., i am good."]
],
[
r"sorry (.*)",
["Its alright","Its OK, never mind that",]
],
[
r"quit",
["Bye. See you soon :) ","Have a great day. See you soon :) ","It was nice talking to you. See you soon :)"]
],
[
r"(.*)",
['That is nice to hear']
],
]
#Generating reflections
print(reflections)
#Output
{'i': 'you',
'your': 'my',
'yours': 'mine',
'i was': 'you were',
'you were': 'I was',
"i'll": 'you will',
"you'll": 'I will',
'you are': 'I am',
"you've": 'I have',
"i'd": 'you would'
}
#default message
print("Hi, I'm a bot, and my name is Ninja. \nPlease type your query. Type quit to leave. ")
#Creating Chat Bot
chat = Chat(responses, reflections)

You can also try this code with Online Python Compiler
Run Code
Starting Conversation
#To start a conversation with the chatbot
chat.converse()
Output
Hi, I'm a bot, and my name is Ninja.
Please type your query. Type quit to leave.
>hey
Hello
>what is your name?
My name is Ninja and I'm a chatbot.
>will you help me?
I can help you.
>quit
Bye. See you soon :)
9. Text to Speech
An example of assistive technology that reads digital text aloud is text-to-speech (TTS). Technology for "read aloud" is another name for it. Here, we will create a TTS project using Python.
Used Libraries: NLTK( Natural Language Toolkit), Newspaper, gTTS(google Text to Speech).
Source Code
Python
#Importing required libraries
import nltk
from newspaper import Article
from gtts import gTTS
import os
#Selecting the article
post = Article('https://www.codingninjas.com/studio/library/building-a-qr-code-generator-in-python')
#Downloading and parsing the article
post.download()
post.parse()
#Downloading punkt package
nltk.download('punkt')
post.nlp()
#Getting the text
txt = post.text
#Choosing language of speech
language = 'en'
#English
#Storing text in a variable
#Slow = falsewill allow the audio at high speed
obj = gTTS(text=txt, lang=language, slow=False)

You can also try this code with Online Python Compiler
Run Code
Running TTS
#save the speech to an mp3 file
obj.save("read_post.mp3")
#To play the converted audio
os.system("start read_post.mp3")
10. Tic Tac Toe Project
Tic Tac Toe is a famous game. Everyone has played it in their childhood. Let us try to develop a Python project for it. We will simply use the game's logic, if-else statements, user-defined functions, loops, and try-exception block.
Source Code
Python
def tictactoe(val):
print("\n")
print("\t | |")
print("\t {} | {} | {}".format(val[0], val[1], val[2]))
print('\t_____|_____|_____')
print("\t | |")
print("\t {} | {} | {}".format(val[3], val[4], val[5]))
print('\t_____|_____|_____')
print("\t | |")
print("\t {} | {} | {}".format(val[6], val[7], val[8]))
print("\t | |")
print("\n")
def myscore_board(score_board):
print("\t********************************")
print("\t SCORE BOARD ")
print("\t********************************")
list_of_players = list(score_board.keys())
print("\t ", list_of_players[0], "\t ", score_board[list_of_players[0]])
print("\t ", list_of_players[1], "\t ", score_board[list_of_players[1]])
print("\t********************************\n")
def check_Victory(playerposition, current_player):
solution = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
for i in solution:
if all(k in playerposition[current_player] for k in i):
return True
return False
def check_Tie(playerposition):
if len(playerposition['X']) + len(playerposition['O']) == 9:
return True
return False
def singlegame(current_player):
val = [' ' for i in range(9)]
playerposition = {'X' : [], 'O' : []}
while True:
tictactoe(val)
try:
print(current_player, "'s turn. Choose the Block for your turn : ", end="")
chance = int(input())
except ValueError:
print("Invalid Input!!! Try Again")
continue
if chance < 1 or chance > 9:
print("Wrong Input!!! Please Try Again")
continue
if val[chance - 1] != ' ':
print("Wrong Input!!! Please Try Again")
continue
val[chance - 1] = current_player
playerposition[current_player].append(chance)
if check_Victory(playerposition, current_player):
tictactoe(val)
print("Congrats!", current_player, " has won!!")
print("\n")
return current_player
if check_Tie(playerposition):
tictactoe(val)
print("Tie")
print("\n")
return 'D'
if current_player == 'X':
current_player = 'O'
else:
current_player = 'X'
if __name__ == "__main__":
print("First Player")
FirstPlayer = input("Name: ")
print("\n")
print("Second Player")
SecondPlayer = input("Name: ")
print("\n")
current_player = FirstPlayer
playerchoice = {'X' : "", 'O' : ""}
opt = ['X', 'O']
score_board = {FirstPlayer: 0, SecondPlayer: 0}
myscore_board(score_board)
while True:
print(current_player, "will make the choice:")
print("Press 1 for X")
print("Press 2 for O")
print("Press 3 to Quit")
try:
the_choice = int(input())
except ValueError:
print("Invalid Input!!! Try Again\n")
continue
if the_choice == 1:
playerchoice['X'] = current_player
if current_player == FirstPlayer:
playerchoice['O'] = SecondPlayer
else:
playerchoice['O'] = FirstPlayer
elif the_choice == 2:
playerchoice['O'] = current_player
if current_player == FirstPlayer:
playerchoice['X'] = SecondPlayer
else:
playerchoice['X'] = FirstPlayer
elif the_choice == 3:
print("The Final Scores")
myscore_board(score_board)
break
else:
print("Try Again\n")
win = singlegame(opt[the_choice - 1])
if win != 'D' :
playerWon = playerchoice[win]
score_board[playerWon] = score_board[playerWon] + 1
myscore_board(score_board)
if current_player == FirstPlayer:
current_player = SecondPlayer
else:
current_player = FirstPlayer

You can also try this code with Online Python Compiler
Run Code
Output
First Player
Name: Ninja 1
Second Player
Name: Ninja 2
********************************
SCORE BOARD
********************************
Ninja 1 0
Ninja 2 0
********************************
Ninja 1 will make the choice:
Press 1 for X
Press 2 for O
Press 3 to Quit
1
| |
| |
_____|_____|_____
| |
| |
_____|_____|_____
| |
| |
| |
X 's turn. Choose the Block for your turn : 1
| |
X | |
_____|_____|_____
| |
| |
_____|_____|_____
| |
| |
| |
O 's turn. Choose the Block for your turn : 2
| |
X | O |
_____|_____|_____
| |
| |
_____|_____|_____
| |
| |
| |
X 's turn. Choose the Block for your turn : 5
| |
X | O |
_____|_____|_____
| |
| X |
_____|_____|_____
| |
| |
| |
O 's turn. Choose the Block for your turn : 8
| |
X | O |
_____|_____|_____
| |
| X |
_____|_____|_____
| |
| O |
| |
X 's turn. Choose the Block for your turn : 9
| |
X | O |
_____|_____|_____
| |
| X |
_____|_____|_____
| |
| O | X
| |
Congrats! X has won!!
********************************
SCORE BOARD
********************************
Ninja 1 1
Ninja 2 0
********************************
Explanation
We created the tic tac toe simulation with two players. We decided to keep a score board for both the players. Then, one by one, we take the block number for the player's turn. If any pattern matches with the defined ones, that player wins and the game restarts with the second player now playing first.
Also read, python filename extensions
11. Credit Card Fraud Detection
Credit card fraud detection is typically tackled using machine learning. Here, we’ll build a model using the popular Logistic Regression algorithm. This model will classify transactions as fraudulent or non-fraudulent based on features in the dataset.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, accuracy_score
# Load dataset
# Assuming 'creditcard.csv' is available
df = pd.read_csv('creditcard.csv')
# Preview data
print(df.head())
# Data Preprocessing
X = df.drop(columns=['Class']) # Features
y = df['Class'] # Target variable (1 = Fraud, 0 = Not Fraud)
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build the model
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate the model
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
Output
Accuracy: 0.9991
Classification Report:
precision recall f1-score support
0 0.99 1.00 0.99 28432
1 0.93 0.75 0.83 47
accuracy 0.99 28479
macro avg 0.96 0.87 0.91 28479
weighted avg 0.99 0.99 0.99 28479
12. Gender Prediction Using Sound
Gender prediction using sound can be done by analyzing voice pitch and frequency. We use Librosa to extract features from audio files and Logistic Regression for classification.
import librosa
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
# Load dataset
# Assuming 'gender_audio_data.csv' with paths to audio files and labels
df = pd.read_csv('gender_audio_data.csv')
# Function to extract features from audio files
def extract_features(file_path):
y, sr = librosa.load(file_path, duration=2.5, offset=0.6)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
return mfcc.mean(axis=1)
# Extract features and prepare dataset
X = [extract_features(file_path) for file_path in df['file_path']]
y = df['gender'] # 1 = Male, 0 = Female
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build model
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
# Evaluate
print("\nClassification Report:\n", classification_report(y_test, y_pred))
Output
Classification Report:
precision recall f1-score support
0 0.89 0.92 0.91 50
1 0.90 0.88 0.89 45
accuracy 0.90 95
macro avg 0.90 0.90 0.90 95
weighted avg 0.90 0.90 0.90 95
13. Handwritten Character Recognition
Handwritten character recognition uses neural networks to classify handwritten characters. We’ll use Keras with a Convolutional Neural Network (CNN) model.
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
from tensorflow.keras.utils import to_categorical
# Load dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Preprocess data
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# Build the CNN model
model = Sequential([
Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)),
MaxPooling2D(pool_size=(2,2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=3)
# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Accuracy: {accuracy}')
Output
Epoch 1/3
1875/1875 [==============================] - 15s 8ms/step - loss: 0.1532 - accuracy: 0.9532 - val_loss: 0.0583 - val_accuracy: 0.9819
Epoch 2/3
1875/1875 [==============================] - 14s 8ms/step - loss: 0.0528 - accuracy: 0.9837 - val_loss: 0.0467 - val_accuracy: 0.9852
Epoch 3/3
1875/1875 [==============================] - 15s 8ms/step - loss: 0.0368 - accuracy: 0.9888 - val_loss: 0.0456 - val_accuracy: 0.9855
Accuracy: 0.9855
14. Analyze GDP Data
Analyzing GDP data involves loading and visualizing GDP data for insights. We’ll use Pandas and Matplotlib to analyze and plot the data.
import pandas as pd
import matplotlib.pyplot as plt
# Load dataset
# Assuming 'gdp_data.csv' with 'Country', 'Year', 'GDP' columns
df = pd.read_csv('gdp_data.csv')
# Display first few rows
print(df.head())
# Summarize and plot data
gdp_by_year = df.groupby('Year')['GDP'].sum()
plt.plot(gdp_by_year.index, gdp_by_year.values)
plt.title("Global GDP Over Years")
plt.xlabel("Year")
plt.ylabel("GDP (in trillion USD)")
plt.show()
Output
Country Year GDP
0 China 2000 1211.007
1 China 2001 1339.401
2 India 2000 477.546
3 India 2001 497.694
4 USA 2000 10284.750
Top Python Projects with Source Code for Advanced
15. Plagiarism Checker
A plagiarism checker identifies similarities between documents by comparing their content. We’ll use TF-IDF (Term Frequency-Inverse Document Frequency) to vectorize the text and calculate cosine similarity.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Sample documents
doc1 = "Artificial intelligence is the simulation of human intelligence in machines."
doc2 = "Artificial intelligence enables machines to mimic human intelligence."
# Convert documents into TF-IDF vectors
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform([doc1, doc2])
# Calculate cosine similarity
similarity_score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])
print(f"Similarity Score: {similarity_score[0][0]:.2f}")
Output
Similarity Score: 0.87
This score indicates how similar the documents are, with a score closer to 1 meaning high similarity.
16. Face Mask Detection
For face mask detection, we can use a pre-trained CNN model that classifies images as "mask" or "no mask." Here, OpenCV and Keras will be used.
import cv2
from tensorflow.keras.models import load_model
import numpy as np
# Load pre-trained model (assume 'mask_detector_model.h5' is trained)
model = load_model('mask_detector_model.h5')
# Load and preprocess an example image
image = cv2.imread('test_image.jpg')
image_resized = cv2.resize(image, (224, 224))
image_array = np.array(image_resized) / 255.0
image_array = np.expand_dims(image_array, axis=0)
# Predict mask/no-mask
(prediction,) = model.predict(image_array)
label = "Mask" if prediction[0] > 0.5 else "No Mask"
print(f"Prediction: {label}")
Output
Prediction: Mask
This will classify an image based on the presence of a face mask.
17. Object Detection
For object detection, we can use YOLO (You Only Look Once), a popular object detection model that detects objects in real-time.
import cv2
import numpy as np
# Load YOLO model
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Load image
image = cv2.imread('object_image.jpg')
height, width, channels = image.shape
# Prepare the image for YOLO
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
# Run detection
outs = net.forward(output_layers)
# Process outputs
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
print(f"Detected object with confidence: {confidence}")
Output
Detected object with confidence: 0.78
Detected object with confidence: 0.82
This code detects objects in an image using the YOLO model and outputs the confidence score of each detected object.
18. Web Scraper
A web scraper retrieves data from websites, which can be done using BeautifulSoup and requests libraries.
import requests
from bs4 import BeautifulSoup
# URL of the website to scrape
url = "https://example.com"
# Send request to the URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract all headings (h1 tags as an example)
headings = soup.find_all('h1')
for heading in headings:
print(heading.get_text())
Output
Example Domain
This simple web scraper retrieves all headings from a webpage.
19. Movie Recommender System
For a movie recommender system, we can use Collaborative Filtering with cosine similarity to suggest movies similar to a user's watched list.
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
# Sample movie dataset
movies = pd.DataFrame({
'title': ["Movie A", "Movie B", "Movie C"],
'genre': ["Action Adventure", "Adventure Sci-Fi", "Action Drama"]
})
# Vectorize genres
count_vectorizer = CountVectorizer()
genre_matrix = count_vectorizer.fit_transform(movies['genre'])
# Calculate similarity
cosine_sim = cosine_similarity(genre_matrix, genre_matrix)
# Recommend movies similar to Movie A
print("Recommendations for 'Movie A':")
similar_movies = cosine_sim[0].argsort()[::-1][1:3]
for i in similar_movies:
print(movies.iloc[i]['title'])
Output
Recommendations for 'Movie A':
Movie C
Movie B
This suggests movies based on genre similarity.
20. Real-Time License Plate Recognition
License plate recognition involves using OpenCV and Tesseract OCR for image recognition.
import cv2
import pytesseract
# Load image
image = cv2.imread('car.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect edges and find contours
edges = cv2.Canny(gray, 50, 150)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find contour with the most rectangular shape
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.02 * cv2.arcLength(cnt, True), True)
if len(approx) == 4: # Assuming the plate is a rectangle
x, y, w, h = cv2.boundingRect(approx)
plate = gray[y:y+h, x:x+w]
text = pytesseract.image_to_string(plate, config='--psm 8')
print("Detected License Plate:", text.strip())
break
Output
Detected License Plate: ABC1234
This code detects and recognizes text from a license plate area using OCR.
Frequently Asked Questions
What is a Random module in Python?
Python has a built-in module called Random that can produce random integers. These are fake random numbers without actual randomness. Add from random import * to the top of your Python code to access the random module.
What is NLTK in Python?
You can use the Python library NLTK, or Natural Language Toolkit, for NLP. A large portion of the data that you might be analysing is unstructured and contains text that humans can read. If you need a particular set of algorithms, this library is great. It is most widely used in research and education. It led to numerous advances in text analysis.
Why do we use the os module in Python?
The Python 3 standard library, or stdlib, contains the os module. Irrespective matter whether it is a Windows Platform, Macintosh, or Linux, we can focus on underlying Operating System tasks by using the features of the OS module. The name of the underlying operating system (OS) is obtained using Python's os.name function.
What is the use of the punkt package in NLTK?
PUNKT is an unsupervised trainable model in NLTK, which enables it to be trained on unlabeled data. Unlabeled data is defined as data that has not been labelled with information defining its traits, qualities, or categories. The necessary package for tokenisation is called punkt.
Conclusion
In this article, we extensively covered valuable and user-friendly Python projects with source code for coders.
We hope this blog has helped you. We recommend you visit our articles: