Python Tutorials

0

Python File readline() and writeline() Method

Program 1 # Program for vowel count import os try: if(os.path.isfile(“d://filedata/student.txt”)): f=open(“d://filedata/student.txt”,”r”) count=0 while True: ch=f.read(1) if not ch: break print(ch,end=””) ch=ch.upper() if(ch==’A’ or ch==’E’ or ch==’I’or ch==’O’ or ch==’U’): count=count+1 print(“\nTotal Vowel: “,count)...

0

Binary File in Python

Program 1 #Program for Binary Mode #Program to copy one text file to other import os # try: # f1=open(“d://filedata\student.txt”,”r”) # f2=open(“d://indore\student1.txt”,”w”) # if(f1.readable() and f2.writable()): # mylist=f1.readlines() # f2.writelines(mylist) # print(“file copied successfully”)...

0

How to Write and Read Data From File in Python

Program 1 # Program for File in python try: f=open(“d://filedata/student.txt”,”w”) if(f.writable()): mystr=input(“Enter a String: “) f.write(mystr) print(“File Created……”) except FileExistsError as obj: print(obj) except FileNotFoundError as obj: print(obj) finally: f.close() # print(f.name) # print(f.closed)...

0

Lock vs Rlock in Python

Program 1 # Difference between Lock and RLock import time from threading import * # l=RLock() # def printTable(n): # l.acquire() # l.acquire() # for i in range(1,11): # print(n*i) # time.sleep(1) # l.release()...

0

Create Thread without Inheritance in Python

Program 1 import time from threading import * class MyThread: def display(self,n): print(current_thread().getName()) for i in range(1,n+1): print(i) time.sleep(1) # Main Thread M1=MyThread() T1=Thread(target=M1.display,args=(5,),name=”First”) T2=Thread(target=M1.display,args=(10,),name=”Second”) T1.start() T2.start()  

0

Python Race Condition

Program 1 # Race Condition import time from threading import * l=Lock() def printTable(n): l.acquire() for i in range(1,11): print(n*i) time.sleep(1) l.release() #Main Thread start=time.time() T1=Thread(target=printTable,args=(5,),name=”One”) T2=Thread(target=printTable,args=(8,),name=”Two”) T3=Thread(target=printTable,args=(10,),name=”Two”) T1.start() T2.start() T3.start() T1.join() T2.join() T3.join()...

0

Python Exception in Thread

Program 1 import time from threading import * #Exception in Thread # def display(a,b): # print(“This is my display Thread”) # c=a//b # threading.excepthook() # # Main Thread # T1=Thread(target=display,args=(20,5)) # T1.start() # a=int(input(“Enter...

0

Exception Handling in Python

Program 1 #Exception Handling # try: # a=int(input(“Enter First Number”)) # b=int(input(“Enter Second Number”)) # c=a//b # print(c) # except ZeroDivisionError as obj: # print(“Unable to divide by zero”) # except ValueError as obj:...

0

Interface in Python

Program 1 from abc import ABC,abstractmethod class Button(ABC): # Interface @abstractmethod def click(self): None @abstractmethod def dbclick(self): None # def display(self): # print(“Hello i am display”) class Color(Button): def click(self): print(“Color is Red Now”)...

0

Python Project – Banking Application

Program 1 class Bank: mylist=[] #static variable def __init__(self,accno,name,amount): self.accno=accno self.name=name self.amount=amount @staticmethod def withdAmount(): ano=int(input(“Enter Account Number: “)) flag=False for i in range(5): if(Bank.mylist[i].accno==ano): flag=True print(“Name: “,Bank.mylist[i].name) amt=int(input(“Enter amount for withdrawal: “)) if(amt>Bank.mylist[i].amount):...