0% found this document useful (0 votes)
16 views

python 1-2 week-6 and week-7 programs

The document contains Python code examples demonstrating Method Resolution Order (MRO) in multiple inheritance, merging file contents, searching for words in a file, and finding the most repeated word in a text file. It includes code snippets for each task along with expected outputs. The examples illustrate fundamental file handling and inheritance concepts in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

python 1-2 week-6 and week-7 programs

The document contains Python code examples demonstrating Method Resolution Order (MRO) in multiple inheritance, merging file contents, searching for words in a file, and finding the most repeated word in a text file. It includes code snippets for each task along with expected outputs. The examples illustrate fundamental file handling and inheritance concepts in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Week-6

5. Write a Python program to demonstrate the usage of


Method Resolution Order ( MRO ) in multiple levels of
Inheritances.
class A :

def process ( self ) :

print ( ' This is A process ' )

class B :

def process ( self ) :

print ( ' This is B process ' )

class C ( A , B ) :

def process ( self ) :

print ( ' This is C process ' )

class D ( C , B ) :

pass

obj = D ( )

obj . process ( )

print ( D . mro ( ) )

Output:

This is C process

[<class '__main__.D'>, <class '__main__.C'>, <class '__main__.A'>, <class


'__main__.B'>, <class 'object'>]
Week-7

1. Write a Python code to merge two given file contents into a third file.

Step1:-create one(text file) and two(text file)

One.txt

two.txt

file1.py
file1 = open ('one.txt' , 'r' )

content1 = file1 . read ( )

file1 . close ( )

# Open the source text file - 2

file2 = open ( 'two.txt' , 'r' )

content2 = file2 . read ( )

file2 . close ( )

# Open the destination file to merge content

file3 = open ( 'three.txt ' , 'w' )

file3 . write ( content1 + content2 )

file3 . close ( )
# Print merge text file

file3 = open ( 'three.txt ' , 'r' )

print ( file3 . read ( ) )

file3 . close ( )

output:

Note: all file save in same folder

2.Write a Python code to open a given file and construct a function to check for given
words present in it and display on found.

def search_str(file_name) :

search_word = input ( " Enter a word you want to search in file : " )

with open (file_name,'r') as file :

content = file.readlines ( )

for word in content :

if word .find (search_word)!= - 1 :

print ( ' The search word exists in the file & available at line = ' ,content . index ( word ) )

break

else :

print ( " string does not exist in a file " )

file . close ( )
file = open ( "search.txt" , 'w' )

Lines = [ " Hai\n " , "hello\n " , "welcome to siiet\n " ]

file . writelines ( Lines )

file . close ( )

search_str("search.txt")

Output:

Enter a word you want to search in file : to

The search word exists in the file & available at line = 2

3.Write a Python code to Read text from a text file, find the word with most number of
occurrences.

Step1:Create data.text file

Program:

count = 0

word = " "

max_Count = 0
words = [ ]

#Opens a file in read mode

file = open ( "data.txt" , "r" )

#Gets each line till end of file is reached

for line in file :

#Splits each line into words

string = line.lower( ).replace( ',' , ' ' ) . replace ( '.' ,' ' ) . split (" ")

#Adding all words generated in previous step into words

for s in string :

words . append ( s )

#Determine the most repeated word in a file

for i in range ( 0 , len ( words ) ) :

count = 1

#Count each word in the file and store it in variable count

for j in range ( i +1 , len ( words ) ) :

if ( words [ i ] == words [ j ] ) :

count = count + 1

# If maxCount is less than count then store value of count in maxCount

# and corresponding word to variable word

if ( count > max_Count ) :

max_Count = count

word = words [ i ]

print ( " Most repeated word is = " + word )

file . close ( )

OUTPUT: Most repeated word is = siiet

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy