You are on page 1of 6

IDAP ASSIGNMENT-2

DISHA A GOWDA
221627043

3. Create sub directory of your_name in the working directory. Open an exiting file to
append the userspecified content till user enters 'FINISH.

Solution :

filename = input("Enter the filename: ")


file = open(filename, "w")

while True:
line = input("Enter text (type 'finish' to stop): ")
if line == "finish":
break
file.write(line + "\n")

file.close()

file = open(filename, "r")


contents = file.read()
words = contents.split()
count = len(words)

print(f"The number of words in the file is: {count}")


file.close()

4. Open a user specified file in read mode and display the content of the file; if the user
inputs the mode other than read than raise an exception "INVALID MODE". Display the set
of words used in the file with their frequency of occrance.

Solution:

import pandas as pd

file_path = input("Enter the file path: ")


mode = input("Enter the mode (read or other): ")

if mode != "read":
raise ValueError("INVALID MODE")

file = open(file_path, 'r')


file_content = file.read()
print("File Content:", file_content)

punctuation = [",", "?", ".", "!", "**"]


word_list = file_content.split()
new_list = []

print(word_list)

for word in word_list:


if word[-1] in punctuation:
word = word[:-1]
new_list.append(word)
print(new_list)

word_dict = dict()

for word in new_list:


if word not in word_dict.keys():
word_dict[word] = 1
else:
word_dict[word] += 1

df = pd.DataFrame(word_dict.items())

print(df.to_markdown())

6. What is a DataFrame? How to create a DataFrame using List, Dictionary and Tuple?
[ support your answer with example codes ]

Solution: Data Frames


A data frame is a two-dimensional tabular labeled data structure with columns of
potentially different types. A data frame can be created from numerous data collections
such as the following:
o A 1D ndarray, list, dict, or series
o 2D Numpy ndarray
Structured or record ndarray
o A series
o Another data frame

Creating data frame using lists:

import pandas as pd

# List of strings
Ist = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']

# List of int
Ist2 = [11, 22, 33, 44, 55, 66, 77]

# Calling DataFrame constructor after zipping


# both lists, with columns specified
df = pd.DataFrame(list(zip(Ist, Ist2)), columns=['Name', 'val1'])

df

Creating data frame using Dictionary:

# Import the pandas library


import pandas as pd

# Dictionary with list objects in values


details = {
"Name": ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Age': [23, 21, 22, 21],
'University': ['BHU', 'JNU', 'DU', 'BHU']
}
# Creating a DataFrame object from dictionary with custom indexing
df = pd.DataFrame(details, index=['a', 'b', 'c', 'd'])

df

Creating data frame using Tuples:

import pandas as pd

# Data in the form of list of tuples


data = [
('Peter', 18, 17),
('Riff', 17, 8),
('John', 17, 8),
('Michel', 18, 17),
('Sheli', 17, 8)
]

# Create DataFrame using data


df = pd.DataFrame(data, columns=['Name', 'Age', 'Score'])

# Print DataFrame
print(df)

7. Write python code to create the following DataFrame. Insert a new column 'Mean' and
assign mean column values to 'Mean'

import pandas as pd

# Create a dictionary with the data


data = {'one': [1.0, 2.0, 3.0, 4.01],
'two': [4.0, 3.0, 2.0, 1.01]}

# Create a DataFrame
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd'])

# Calculate the mean for each row and assign it to the 'Mean' column
df['Mean'] = df.mean(axis=1)

# Print the resulting DataFrame


print(df)

8. What are the file attributes? List all the modes of file and their usages.

1. r - Opens a file for reading only; the default mode


2. rb - Opens a file for reading only in binary format
3. r+ - Opens a file for both reading and writing
4. rb+ - Opens a file for both reading and writing in binary format
5. W - Opens a file for writing only
6. wb - Opens a file for writing only in binary format
7. W+ - Opens a file for both writing and reading
8. wb+ - Opens a file for both writing and reading in binary format
9. a - Opens a file for appending
10. ab - Opens a file for appending in binary format
11. a+ - Opens a file for both appending and reading
12. ab+ - Opens a file for both appending and reading in binary format

10. Write python code to open a file that contains alphanumeric text, in read mode. Copy
numeric content to another text file and count the three-digit numbers in this file.

Solution:

import re

# Open the file containing alphanumeric text in read mode


with open('file1.txt', 'r') as f1:
# Read the contents of the file
content = f1.read()
# Extract numeric content from the file
numeric_content = re.findall(r'\d+', content)

# Write numeric content to another text file


with open('file2.txt', 'w') as f2:
for num in numeric_content:
f2.write(num + '\n')

# Count the number of three-digit numbers in the new file


count = 0
with open('file2.txt', 'r') as f3:
for line in f3:
if len(line.strip()) == 3:
count += 1

# Print the count of three-digit numbers


print(f'The number of three-digit numbers in file2.txt is {count}.')

9. i) Write a Python program to replace whitespaces with an underscore and vice versa.

Solution:

def replace_spaces_underscore(input_str):
replaced_str = ""
for char in input_str:
if char == ' ':
replaced_str += '_'
elif char == '_':
replaced_str += ' '
else:
replaced_str += char
return replaced_str

input_str = "Hello World"


result = replace_spaces_underscore(input_str)

print(result)

ii Write a Python program to extract year, month and date from an url.
Solution:

from urllib.parse import urlparse, parse_qs

url = "https://example.com/path?year=2023&month=09&date=27"

parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)

year = query_params.get('year', [''])[0]


month = query_params.get('month', [''])[0]
date = query_params.get('date', [''])[0]

print(f"Year: {year}, Month: {month}, Date: {date}")

111) Write a Python program to separate and print the numbers of a given string.

Solution:

def extract_numbers(input_str):
numbers = ""
for char in input_str:
if char.isdigit():
numbers += char
return numbers

input_str = "Hello123Wor1d456"
numbers = extract_numbers(input_str)
print(numbers)

iv) Write a Python program to find all words starting with 'a' or 'e' in a given string.

Solution:
def find_ae_words(input_str):
words = input_str.split()
ae_words = [word for word in words if word.startswith('a') or word.startswith('e')]
return ae_words

input_str = "Apples are awesome and elephants are enormous"


ae_words = find_ae_words(input_str)

print(ae_words)

v) Write a Python program to replace all occurrences of space, comma, or dot with a colon.
Solution:

def replace_with_colon(input_str):
replaced_str = input_str.replace(' ', ':').replace(',', ':').replace('.', ':')
return replaced_str

input_str = "This is a sample, text. Replace spaces, commas and dots."


result = replace_with_colon(input_str)
print(result)

vi) Write a Python program to find all five characters long word in a string.
Solution:

import re

# Define a sample string


text = "This is a sample text with some words of varying lengths."

# Use regular expression to find five-character-long words


five_character_words = re.findall(r'\b\w{5}\b', text)

# Print the found words


print("Five-character words in the text:")
for word in five_character_words:
print(word)

vil) Write a Python program to find all three, four, five characters long words in a string.
Solution:

import re

# Define a sample string


text = "This is a sample text with some words of varying lengths."

# Use regular expression to find three, four, and five-character-long words


words = re.findall(r'\b\w{3,5}\b', text)

# Print the found words


print("Words with three, four, and five characters in the text:")
for word in words:
print(word)

You might also like