You are on page 1of 10

Write a method in Python to read lines from a text file INDIA.

TXT, to find and display


the occurrence of the word 'India'. For example, if the content of the file is:

India is the fastest-growing economy. India is looking for more investments around
the globe. The whole world is looking at India as a great market. Most of the Indians
can foresee the heights that India is capable of reaching.

The output should be 4.

Program (count-word.py)

def count_word():
file = open('india.txt','r')
count = 0

for line in file:


words = line.split()
for word in words:
if word == 'India':
count += 1
print(count)

file.close()

# call the function count_word().


count_word()
Output
1. write a program that inputs a real number and converts it to nearest
using two different built functions. It also displays the given number
rounded off to 3 places after decimal.
import random

>>> random.seed(100)

>>> for _ in range(1000000):

... randn = random.uniform(-0.05, 0.05)


... actual_value = actual_value + randn

... truncated_value = truncate(truncated_value


+ randn)

...

>>> actual_value

96.45273913513529

>>> truncated_value

0.239
24. Write a program to create a triangle of stars using nested loop.

print("Print equilateral triangle Pyramid using asterisk symbol ")

# printing full Triangle pyramid using stars

size = 7

m = (2 * size) - 2

for i in range(0, size):

for j in range(0, m):

print(end=" ")

# decrementing m after each loop

m=m-1

for j in range(0, i + 1):

print("* ", end=' ')

print(" ")
1. Write a function DISPLAYWORDS () in python to read lines from a
text file STORY. TXT (), and display those words which are less
than 4 characters.

Remove all the lines that contain the character ‘a’ in a file and write it to
another file
1. Create a binary file with roll number, name and marks. Input a roll
no. and update the marks.
1. Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).

1. Create a CSV file by entering user-id and password, read and search
the password for the given user-id.
1. Write a program that display first ten Mersenne numbers.

You might also like