0% found this document useful (0 votes)
420 views5 pages

Advanced Python Practical Exercises

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)
420 views5 pages

Advanced Python Practical Exercises

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
  • Practical No 1
  • Practical No 2
  • Practical No 3

Practical No 1 :-

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


Code :-

i) #READ Operation:-
fo=open("C:/Users/ORISANLAB-23/Desktop/[Link]","r+")
str=[Link](10) print(str) [Link]()

Output:-

ii) #WRITE Operation

fo=open("C:/Users/ORISANLAB-23/Desktop/[Link]","w")
[Link]("Hello Python")
print(fo)
[Link]()

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=[Link](“\
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= [Link](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=[Link]("ai",txt) print(x)

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

import re txt= "The Rain


In Spain" x=[Link]("\
s",txt) print("The First
White-Space Character
Is Located In The
Position:",[Link]())

Output :-

v) Using split() Function :-

import re txt= "The Rain


In Spain" x=[Link]("\
s",txt) print(x)

Output :-

vi) Using sub() Function :-

import re txt= "The Rain


In Spain" x=[Link]("\
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
[Link](5) if __name__
== "__main__":
t1 = [Link](target=Test,
args=(15,)) [Link]() [Link]()

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 = [Link](target=print_square, args=(10,))
t2 = [Link](target=print_cube,
args=(10,)) [Link]() [Link]() [Link]()
[Link]()
print("Done")

Output :-

iii) Multitasking Threading :-

Practical No 1 :-
Aim :- Write A Python Program To Implement Various File Operations. 
Code :-  
i)
#READ Operation:- 
fo=ope
Practical No 2 :-
Aim :- Write A Python Program To Demonstrate Use Of Regular Expression 
For Suitable Application. 
Code :-
iv)
Using search() Function:- 
 
import re txt= "The Rain
In Spain" x=re.search("
s",txt)  print("The  First
White-Space
Practical No 3 :-
 
Aim :- Write A Python Program To Demonstrate The Concept Of 
Threading In Python. 
 
Code :-  
 
i) 
Sing
print("Cube:", num * num * num) 
 
def print_square(num): 
    print("Square:", num * num) 
 
if __name__ == "__main__":

You might also like