Python Module 4
Python Module 4
Python Module 4
Question-1
The organizers want to ensure that all participants provide valid email
addresses during the sign-up. The developer tasked with this assignment,
is responsible for creating an email validation system. This system will
help filter out incorrect email addresses and ensure that participants
receive important updates and notifications about the exhibition.
def validate_email(email):
if "@" in email:
domain = email.split("@")[1]
if domain in valid_domains:
return True
return False
if validate_email(user_email):
print("Email is valid.")
else:
Question-2
In the world of art and literature, finding and highlighting specific
phrases or keywords within a text is crucial for creating impactful
narratives. In this assignment, you will develop a Python program that
helps users find and highlight a given substring within a larger text.
This program can be useful for curators and writers seeking to emphasize
certain themes or ideas within their content.
index = text.find(substring)
if index == -1:
highlight_end = "]"
return modified_text
print("Modified Text:")
print("highlighted_text")
Question-3
A creative developer has been entrusted with designing the digital badges
for the exhibition participants. Each badge will feature the initials of
the artist or attendee. Bella decides to automate the process by creating
a script that takes the participant's full name and generates their
personalized initials to be featured on the badges.
def get_initials(name):
words = name.split()
return ''.join(initials)
initials = get_initials(full_name)
print("Initials: ",initials)
Question-4
Dana, an aspiring journalist, has been assigned the task of creating
engaging and concise descriptions for the artworks to be displayed. To
ensure the descriptions are effective, she wants to avoid repetitive
words. Taking help from a programmer and her programming skills, Dana
develops a program that analyses the descriptions, providing her with
insights into the frequency of words. This helps her craft compelling
descriptions that captivate the exhibition visitors.
def count_words(text):
words = text.split()
word_count = {}
if word in word_count:
word_count[word] = word_count[word] + 1
else:
word_count[word] = 1
return word_count
article = article.lower()
word_frequency = count_words(article)
print("Word Frequency:")
print(f"{word}: {count}")
Question-5
The organizers want to get feedback from the participants and analyse the
feedback received after the exhibition. In this program, visitors can
provide feedback, and the program demonstrates the usage of various string
functions to analyse and manipulate the feedback. The analysis includes
counting the number of characters and words, converting to uppercase,
lowercase, and title case, reversing the feedback, and counting the number
of vowels.
You can further enhance and customize this program to include additional
string functions, graphical representations of the analysis, and other
interactive features.
def analyze_feedback(feedback):
num_characters = len(feedback)
num_words = len(feedback.split())
uppercase_feedback = feedback.upper()
lowercase_feedback = feedback.lower()
title_case_feedback = feedback.title()
reversed_feedback = feedback[::-1]
vowels = "AEIOUaeiou"
print("Feedback Analysis:")
print("--------------------------------------------------")
print("--------------------------------------------------")
def main():
analyze_feedback(feedback)
main()
WebScraping
Question-1
Now, The organizers of the exhibition got many entries online through
their website and they further wanted to get the information extracted
from the website. But their website wasn't working as some of the visitors
complained so they decided to first confirm if they were able to reach the
website or not. Adding to that a developer recommended he can test it
using Python’s request library.
import requests
url = "https://www.vadehraart.com/"
response = requests.get(url)
print(response.status_code)
if response.status_code == 200:
print(response.content)
else:
print("The webpage fetch has failed.")
Beautiful Soup
Question-1
In the context of an art exhibition that celebrates creativity and
inspiration, The organizers have asked the developers to develop a digital
curation system that gathers and presents a curated collection of
inspirational quotes, each paired with captivating visuals, to enhance
visitors' artistic experience. The objective is to create a Python-based
solution that automates the extraction of quotes, their associated themes,
images, and authors from a designated webpage.
import requests
URL = "http://www.values.com/inspirational-quotes"
r = requests.get(URL)
print(r)
soup
quotes=[]
table
quote = {}
quote['theme'] = row.h5.text
quote['url'] = row.a['href']
quote['img'] = row.img['src']
print(quotes)
webpage_url = 'https://en.wikipedia.org/wiki/Main_Page'
response = urlopen(webpage_url)
header_list = []
print(header.text)
header_list.append(header.text)
word_frequency = {}
words = header.split()
if word in word_frequency:
word_frequency[word] = word_frequency[word] + 1
else:
word_frequency[word] = 1
print('--------------------------')
count = 0
print(f"{word}: {freq}")
count = count + 1
if count >= 5:
break
Capstone
Question-1
Gather and extract relevant information from the IMDb website's 'Top 250'
movie list, including movie rankings, titles, release years, IMDb ratings,
vote counts, and genres, using web scraping techniques and data parsing in
Python?"
import requests
url = "https://www.imdb.com/search/title/?groups=top_250&sort=user_rating"
response = requests.get(url)
print(response.status_code)
print(len(movie_containers))
movie_data = {}
movie_info = {}
movie_info['ranking'] = ranking
name = container.h3.a.text
movie_info['name'] = name
movie_info['year'] = year
imdb = container.strong.text
movie_info['imdb_rating'] = float(imdb)
movie_info['votes'] = vote
movie_info['genre'] = genres
movie_data[name] = movie_info
print(movie_data)
Question-2
Create a dictionary of the above data provided: Write a Python program to
perform the following operations as mentioned below:
movie_data = {'The Shawshank Redemption': {'ranking': '1', 'name': 'The Shawshank Redemption', 'year':
'1994', 'imdb_rating': 9.3, 'votes': '2,791,397', 'genre': 'Drama'}, 'The Godfather': {'ranking': '2', 'name':
'The Godfather', 'year': '1972', 'imdb_rating': 9.2, 'votes': '1,944,229', 'genre': 'Crime, Drama'}, 'The Dark
Knight': {'ranking': '3', 'name': 'The Dark Knight', 'year': '2008', 'imdb_rating': 9.0, 'votes': '2,771,569',
'genre': 'Action, Crime, Drama'}, "Schindler's List": {'ranking': '4', 'name': "Schindler's List", 'year': '1993',
'imdb_rating': 9.0, 'votes': '1,403,821', 'genre': 'Biography, Drama, History'}, 'The Godfather Part II':
{'ranking': '5', 'name': 'The Godfather Part II', 'year': '1974', 'imdb_rating': 9.0, 'votes': '1,320,870',
'genre': 'Crime, Drama'}, 'The Lord of the Rings: The Return of the King': {'ranking': '6', 'name': 'The Lord
of the Rings: The Return of the King', 'year': '2003', 'imdb_rating': 9.0, 'votes': '1,911,924', 'genre':
'Action, Adventure, Drama'}, '12 Angry Men': {'ranking': '7', 'name': '12 Angry Men', 'year': '1957',
'imdb_rating': 9.0, 'votes': '829,179', 'genre': 'Crime, Drama'}, 'Pulp Fiction': {'ranking': '8', 'name': 'Pulp
Fiction', 'year': '1994', 'imdb_rating': 8.9, 'votes': '2,141,402', 'genre': 'Crime, Drama'}, 'Spider-Man:
Across the Spider-Verse': {'ranking': '9', 'name': 'Spider-Man: Across the Spider-Verse', 'year': '2023',
'imdb_rating': 8.8, 'votes': '243,641', 'genre': 'Animation, Action, Adventure'}, 'Inception': {'ranking': '10',
'name': 'Inception', 'year': '2010', 'imdb_rating': 8.8, 'votes': '2,460,643', 'genre': 'Action, Adventure, Sci-
Fi'}, 'The Lord of the Rings: The Fellowship of the Ring': {'ranking': '11', 'name': 'The Lord of the Rings:
The Fellowship of the Ring', 'year': '2001', 'imdb_rating': 8.8, 'votes': '1,940,118', 'genre': 'Action,
Adventure, Drama'}, 'Fight Club': {'ranking': '12', 'name': 'Fight Club', 'year': '1999', 'imdb_rating': 8.8,
'votes': '2,225,606', 'genre': 'Drama'}, 'Forrest Gump': {'ranking': '13', 'name': 'Forrest Gump', 'year':
'1994', 'imdb_rating': 8.8, 'votes': '2,171,468', 'genre': 'Drama, Romance'}, 'Il buono, il brutto, il cattivo':
{'ranking': '14', 'name': 'Il buono, il brutto, il cattivo', 'year': '1966', 'imdb_rating': 8.8, 'votes': '787,484',
'genre': 'Adventure, Western'}, 'The Lord of the Rings: The Two Towers': {'ranking': '15', 'name': 'The
Lord of the Rings: The Two Towers', 'year': '2002', 'imdb_rating': 8.8, 'votes': '1,725,289', 'genre': 'Action,
Adventure, Drama'}, 'Jai Bhim': {'ranking': '16', 'name': 'Jai Bhim', 'year': '2021', 'imdb_rating': 8.8,
'votes': '209,719', 'genre': 'Crime, Drama, Mystery'}, 'Interstellar': {'ranking': '17', 'name': 'Interstellar',
'year': '2014', 'imdb_rating': 8.7, 'votes': '1,978,037', 'genre': 'Adventure, Drama, Sci-Fi'}, 'Goodfellas':
{'ranking': '18', 'name': 'Goodfellas', 'year': '1990', 'imdb_rating': 8.7, 'votes': '1,211,198', 'genre':
'Biography, Crime, Drama'}, 'The Matrix': {'ranking': '19', 'name': 'The Matrix', 'year': '1999',
'imdb_rating': 8.7, 'votes': '1,985,257', 'genre': 'Action, Sci-Fi'}, "One Flew Over the Cuckoo's Nest":
{'ranking': '20', 'name': "One Flew Over the Cuckoo's Nest", 'year': '1975', 'imdb_rating': 8.7, 'votes':
'1,041,219', 'genre': 'Drama'}, 'Star Wars: Episode V - The Empire Strikes Back': {'ranking': '21', 'name':
'Star Wars: Episode V - The Empire Strikes Back', 'year': '1980', 'imdb_rating': 8.7, 'votes': '1,338,664',
'genre': 'Action, Adventure, Fantasy'}, 'Oppenheimer': {'ranking': '22', 'name': 'Oppenheimer', 'year':
'2023', 'imdb_rating': 8.6, 'votes': '399,297', 'genre': 'Biography, Drama, History'}, 'Se7en': {'ranking': '23',
'name': 'Se7en', 'year': '1995', 'imdb_rating': 8.6, 'votes': '1,727,769', 'genre': 'Crime, Drama, Mystery'},
'The Silence of the Lambs': {'ranking': '24', 'name': 'The Silence of the Lambs', 'year': '1991',
'imdb_rating': 8.6, 'votes': '1,489,061', 'genre': 'Crime, Drama, Thriller'}, 'The Green Mile': {'ranking': '25',
'name': 'The Green Mile', 'year': '1999', 'imdb_rating': 8.6, 'votes': '1,356,698', 'genre': 'Crime, Drama,
Fantasy'}, 'Saving Private Ryan': {'ranking': '26', 'name': 'Saving Private Ryan', 'year': '1998',
'imdb_rating': 8.6, 'votes': '1,445,823', 'genre': 'Drama, War'}, 'Terminator 2: Judgment Day': {'ranking':
'27', 'name': 'Terminator 2: Judgment Day', 'year': '1991', 'imdb_rating': 8.6, 'votes': '1,138,887', 'genre':
'Action, Sci-Fi'}, 'Star Wars': {'ranking': '28', 'name': 'Star Wars', 'year': '1977', 'imdb_rating': 8.6, 'votes':
'1,410,486', 'genre': 'Action, Adventure, Fantasy'}, 'Sen to Chihiro no kamikakushi': {'ranking': '29',
'name': 'Sen to Chihiro no kamikakushi', 'year': '2001', 'imdb_rating': 8.6, 'votes': '806,573', 'genre':
'Animation, Adventure, Family'}, 'Cidade de Deus': {'ranking': '30', 'name': 'Cidade de Deus', 'year':
'2002', 'imdb_rating': 8.6, 'votes': '779,885', 'genre': 'Crime, Drama'}, 'La vita è bella': {'ranking': '31',
'name': 'La vita è bella', 'year': '1997', 'imdb_rating': 8.6, 'votes': '720,584', 'genre': 'Comedy, Drama,
Romance'}, "It's a Wonderful Life": {'ranking': '32', 'name': "It's a Wonderful Life", 'year': '1946',
'imdb_rating': 8.6, 'votes': '477,856', 'genre': 'Drama, Family, Fantasy'}, 'Shichinin no samurai': {'ranking':
'33', 'name': 'Shichinin no samurai', 'year': '1954', 'imdb_rating': 8.6, 'votes': '357,156', 'genre': 'Action,
Drama'}, 'Seppuku': {'ranking': '34', 'name': 'Seppuku', 'year': '1962', 'imdb_rating': 8.6, 'votes': '63,281',
'genre': 'Action, Drama, Mystery'}, 'Gladiator': {'ranking': '35', 'name': 'Gladiator', 'year': '2000',
'imdb_rating': 8.5, 'votes': '1,558,002', 'genre': 'Action, Adventure, Drama'}, 'The Prestige': {'ranking':
'36', 'name': 'The Prestige', 'year': '2006', 'imdb_rating': 8.5, 'votes': '1,391,963', 'genre': 'Drama,
Mystery, Sci-Fi'}, 'The Departed': {'ranking': '37', 'name': 'The Departed', 'year': '2006', 'imdb_rating': 8.5,
'votes': '1,376,163', 'genre': 'Crime, Drama, Thriller'}, 'Back to the Future': {'ranking': '38', 'name': 'Back
to the Future', 'year': '1985', 'imdb_rating': 8.5, 'votes': '1,259,666', 'genre': 'Adventure, Comedy, Sci-Fi'},
'Django Unchained': {'ranking': '39', 'name': 'Django Unchained', 'year': '2012', 'imdb_rating': 8.5, 'votes':
'1,629,532', 'genre': 'Drama, Western'}, 'Gisaengchung': {'ranking': '40', 'name': 'Gisaengchung', 'year':
'2019', 'imdb_rating': 8.5, 'votes': '882,754', 'genre': 'Drama, Thriller'}, 'Alien': {'ranking': '41', 'name':
'Alien', 'year': '1979', 'imdb_rating': 8.5, 'votes': '916,536', 'genre': 'Horror, Sci-Fi'}, 'Whiplash': {'ranking':
'42', 'name': 'Whiplash', 'year': '2014', 'imdb_rating': 8.5, 'votes': '927,771', 'genre': 'Drama, Music'},
'Léon': {'ranking': '43', 'name': 'Léon', 'year': '1994', 'imdb_rating': 8.5, 'votes': '1,205,981', 'genre':
'Action, Crime, Drama'}, 'The Usual Suspects': {'ranking': '44', 'name': 'The Usual Suspects', 'year': '1995',
'imdb_rating': 8.5, 'votes': '1,116,458', 'genre': 'Crime, Drama, Mystery'}, 'The Pianist': {'ranking': '45',
'name': 'The Pianist', 'year': '2002', 'imdb_rating': 8.5, 'votes': '874,314', 'genre': 'Biography, Drama,
Music'}, 'The Lion King': {'ranking': '46', 'name': 'The Lion King', 'year': '1994', 'imdb_rating': 8.5, 'votes':
'1,102,361', 'genre': 'Animation, Adventure, Drama'}, 'American History X': {'ranking': '47', 'name':
'American History X', 'year': '1998', 'imdb_rating': 8.5, 'votes': '1,155,786', 'genre': 'Crime, Drama'},
'Psycho': {'ranking': '48', 'name': 'Psycho', 'year': '1960', 'imdb_rating': 8.5, 'votes': '696,715', 'genre':
'Horror, Mystery, Thriller'}, 'The Intouchables': {'ranking': '49', 'name': 'The Intouchables', 'year': '2011',
'imdb_rating': 8.5, 'votes': '895,241', 'genre': 'Biography, Comedy, Drama'}, 'Casablanca': {'ranking': '50',
'name': 'Casablanca', 'year': '1942', 'imdb_rating': 8.5, 'votes': '590,567', 'genre': 'Drama, Romance,
War'}}
top_5_movies = sorted_movie_data[:5]
print(f"Movie: {movie_name}")
print(f"Votes: {movie_info['votes']}")
print(f"Genre: {movie_info['genre']}")
print("=" * 30)
Question-3
Begin by importing the necessary libraries: requests for sending HTTP
requests, BeautifulSoup for parsing HTML content, and any other required
libraries.
1. Define the URL of the IMDb page containing user reviews. Send a GET
request to the URL and create a BeautifulSoup object to parse the HTML
content.
3. Iterate through each review container and extract the text content of
the reviews.
Analyzing Reviews:
url = "https://www.imdb.com/title/tt13375076/reviews/?ref_=ttexr_ql_2"
response = requests.get(url)
print(response.status_code)
reviews = soup.find_all('div',{'class':"lister-item-content"})
print(len(reviews))
user_reviews = []
for r in reviews:
user_reviews.append(ur)
print(user_reviews)
def contains_bad_words(comment_text):
comment_text_lower = comment_text.lower()
if word in comment_text_lower:
return True
return False
if contains_bad_words(rev):
else: