In [ ]:
cd
In [ ]:
import datetime
print(datetime.datetime.now())
1. #### Question1 Write a code snippet that opens a file named messages.txt in write mode and writes 3 different messages to it, each on a new line.
Please complete the following code:
messages_file = open()
messages_file.write()
messages_file.close()
messages_file = open()
print()
In [1]:
messages_file = open("messages.txt", "w")
messages_file.write("Hello everyone\n")
messages_file.write("This is Sahitya\n")
messages_file.write("All the best for the exam\n")
messages_file.close()
messages_file = open("messages.txt", "r")
print(messages_file.read())
messages_file.close()
Hello everyone
This is Sahitya
All the best for the exam
1. #### Question2 Given a DataFrame named population
with columns Country_Name
, Year
, and Population
, write code to select rows 2 to 5 (inclusive) only for the columns Country_Name
and Year
.
In [2]:
import pandas as pd
population = pd.DataFrame({'Country_Name': ['Germany', 'India', 'United States', 'United Kingdom', 'Australia'],
'Year': [2023, 2023, 2023, 2023, 2023],
'Population': [1543, 1580, 731, 673, 111]})
def g(population):
return population.iloc[1:6, :2]
result = g(population.copy())
print(result)
Country_Name Year