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

Advance Python Programming Practicals Semester - 2

This document contains code snippets and outputs from 3 Python programs demonstrating various file operations, regular expressions, and threading concepts. The first program performs read and write operations on a file. The second program uses regular expressions to search, match, findall, search, split, and sub strings. The third program shows single and multi-threading using the threading module, creating threads to run functions concurrently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
732 views

Advance Python Programming Practicals Semester - 2

This document contains code snippets and outputs from 3 Python programs demonstrating various file operations, regular expressions, and threading concepts. The first program performs read and write operations on a file. The second program uses regular expressions to search, match, findall, search, split, and sub strings. The third program shows single and multi-threading using the threading module, creating threads to run functions concurrently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No 1 :-

Aim :- Write A Python Program To Implement Various File Operations.


Code :-

i) #READ Operation:-
fo=open("C:/Users/ORISANLAB-23/Desktop/abc.txt","r+")
str=fo.read(10) print(str) fo.close()

Output:-

ii) #WRITE Operation

fo=open("C:/Users/ORISANLAB-23/Desktop/abc.txt","w")
fo.write("Hello Python")
print(fo)
fo.close()

Output:-
Practical No 2 :-
Aim :- Write A Python Program To Demonstrate Use Of Regular Expression
For Suitable Application.
Code :-

i) Search For The First White-Space Character In The String :-

import re txt= “The Rain


In Spain” x=re.search(“\
s”,txt)
print(“The First White-Space Character Is Located In
Position:”,xstart())

Output:-

ii) Using Match Function :-

import re
line = ‘Cats Are Smaller Than Dogs’
matchobj= re.match(r’cats’,line) if
matchobj:
print(“Match Found”) else
print(“Match Not Found”)

Output:-

iii) Using findall() function :- import re txt="The Rain In Spain"


x=re.findall("ai",txt) print(x)

Output :-
iv) Using search() Function:-

import re txt= "The Rain


In Spain" x=re.search("\
s",txt) print("The First
White-Space Character
Is Located In The
Position:",x.start())

Output :-

v) Using split() Function :-

import re txt= "The Rain


In Spain" x=re.split("\
s",txt) print(x)

Output :-

vi) Using sub() Function :-

import re txt= "The Rain


In Spain" x=re.sub("\
s","9",txt) print(x)

Output :-
Practical No 3 :-

Aim :- Write A Python Program To Demonstrate The Concept Of


Threading In Python.

Code :-

i) Single Threading :-
import threading, time def
Test(n):
i=1
while i <= n:
print("main
thread=", i)
i = i + 1
time.sleep(5) if __name__
== "__main__":
t1 = threading.Thread(target=Test,
args=(15,)) t1.start() t1.join()

Output :-

ii) Multi Threading :-

import threading

def print_cube(num):
print("Cube:", num * num * num)

def print_square(num):
print("Square:", num * num)

if __name__ == "__main__":
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_cube,
args=(10,)) t1.start() t1.join() t2.start()
t2.join()
print("Done")

Output :-

iii) Multitasking Threading :-

You might also like