File Operation
File Operation
File Operation
import re
a=input("Enter the Mail ID: ")
mails=['@gmail.com','@hotmail.com','@stc.ac.in','yahoomail.com']
if a[0].isalpha() and a[1].isalpha():
for i in range(len(mails)):
if mails[i] in a:
flag=1
break
else:
flag=0
if flag==1:
print("Valid Mail id")
else:
print("Invalid Mail id")
password=input("Enter password: ")
flag=0
while True:
if len(password)<8:
flag=-1
break
elif not re.search("[a-z]",password):
flag=-1
break
elif not re.search("[A-Z]",password):
flag=-1
break
elif not re.search("[0-9]",password):
flag=-1
break
elif not re.search("[@_*&]",password):
flag=-1
break
else:
flag=0
break
if flag==-1:
print("Valid password")
else:
print("Invalid password")
OUTPUT
Enter the Mail ID: [email protected]
Valid Mail id
Enter password: sundar
Valid password
STANDARD BUILD IN FUNCTIONS
dsm=dict()
print("Empty Dictionary:",dsm)
print("Type Methd:",type(dsm))
n=int(input("Enter the no of elements into the dictionary:"))
print("Enter the elements:")
for i in range(n):
print("Enter the key:")
ky=input()
print("Enter the value:")
val=input()
ne={ky:val}
dsm.update(ne)
print("The Dictionary:",dsm)
va=input("Enter the key value of Dictionary to find: ")
print("Key values of Dictionary is:",dsm.get(va))
print("The Items of Dictionary:",dsm.items())
print("The keys of dictionary:",dsm.keys())
print("The values of Dictionary:",dsm.values())
p=input("Enter the key value for pop: ")
print("The values of pop element in Dictionary:",dsm.pop(p))
print("The Dictionary after pop:",dsm)
ndsm=dsm.copy()
print("Copy of Dictionary:",ndsm)
print("Length of Dictionary:",len(dsm))
print("Maximum of Dictionary:",max(dsm))
print("Minimum of Dictionary:",min(dsm))
dsm.clear()
print("Dictionary After clear method:",dsm)
OUTPUT
Empty Dictionary: {}
Type Methd: <class 'dict'>
Enter the no of elements into the dictionary:3
Enter the elements:
Enter the key:
Name:
Enter the value:
Sri
Enter the key:
Age:
Enter the value:
20
Enter the key:
Gender:
Enter the value:
Male
The Dictionary: {'Name:': 'Sri', 'Age:': '20', 'Gender:': 'Male'}
Enter the key value of Dictionary to find: Gender:
Key values of Dictionary is: Male
The Items of Dictionary: dict_items([('Name:', 'Sri'), ('Age:', '20'), ('Gender:', 'Male')])
The keys of dictionary: dict_keys(['Name:', 'Age:', 'Gender:'])
The values of Dictionary: dict_values(['Sri', '20', 'Male'])
Enter the key value for pop: Age:
The values of pop element in Dictionary: 20
The Dictionary after pop: {'Name:': 'Sri', 'Gender:': 'Male'}
Copy of Dictionary: {'Name:': 'Sri', 'Gender:': 'Male'}
Length of Dictionary: 2
Maximum of Dictionary: Name:
Minimum of Dictionary: Gender:
Dictionary After clear method: {}
EXCEPTION HANDLING
try:
b=input("Enter a value: ")
print("The enter value is",b)
r=1/int(b)
except:
print("OOPS!",sys.exe_info()[0],"occured")
print("Next Entry")
print()
print("The reciprocal of",b,"is",r)
OUTPUT
Enter a value: 5
The enter value is 5
The reciprocal of 5 is 0.2
CREATING EXCEPTION
class Error(Exception):
pass
class ValueTooSmallError(Error):
pass
class ValueTooLargeError(Error):
pass
acno=int(input("Enter a Account number:"))
name=input("Enter Name:")
bal=float(input("Enter balance:"))
while True:
try:
wid=float(input("Enter the Withdrawal amount:"))
if wid<0:
raise ValueTooSmallError
elif wid>bal:
raise ValueTooLargeError
break
except ValueTooSmallError:
print("Amount Withdrawal could not be negative. Please try again")
print()
except ValueTooLargeError:
print("Withdrawal amount greater than available balance.Try again")
print("Amount withdrawal successful.Current balance is",bal-wid)
OUTPUT
Enter a Account number:5896478987456321
Enter Name:Sri
Enter balance:87000
Enter the Withdrawal amount:-500
Amount Withdrawal could not be negative. Please try again
Name: Sri
Age: 20
Gender Male
Class: III Bsc IT
python: 78
Android: 87
Elective1: 89
Elective2: 78
Average marks: 83.0
Name: Sri
Age: 20
Gender Male
Class: III Bsc IT
python: 96
Android: 87
Elective1: 88
Elective2: 98
Average marks: 92.25
THREAD AND LOCKS
import threading
import time
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
threadLock.acquire()
print_time(self.name, self.counter, 3)
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
threads = []
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread3 = myThread(3, "Thread-2", 2)
thread1.start()
thread2.start()
thread3.start()
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
for t in threads:
t.join()
print ("Exiting Main Thread")
OUTPUT
Starting Thread-1Starting Thread-2Starting Thread-2
import pymysql
db = pymysql.connect(host='localhost', user='root', passwd='', db='student')
cursor = db.cursor()
ch='Y'
while(ch=='Y'):
print("\n\t\tVarious Operation\n\t1.Insert\n\t2.Delete\n\t3.Update\n\t4.Display")
op=int(input("Enter Your Choice"))
if op==1:
print("\n\t\tInsert Operation")
name = input("Name: ")
age = int(input("Age: "))
gender = input("Gender: ")
stuClass = input("Class: ")
print("Enter the marks of the respective subjects")
py = int(input("Python: "))
ad = int(input("Android: "))
e1 = int(input("Elective 1: "))
e2 = int(input("Elective 2: "))
sql = "INSERT INTO student(name,age,gender,Class,py,ad,e1,e2)VALUES
('%s','%d','%s','%s','%d','%d','%d','%d')" % (name,age,gender,stuClass,py,ad,e1,e2)
try:
cursor.execute(sql)
db.commit()
print("\n\t\t****Record Inserted Successfully***")
except:
db.rollback()
elif op==2:
print("\n\t\tDelete Operation")
name = input("Enter the Name to be Deleted: ")
sql = "DELETE FROM student where name=('%s')" % (name)
try:
cursor.execute(sql)
db.commit()
print("\n\t\t****Record Deleted Successfully***")
except:
db.rollback()
elif op==3:
print("\n\t\tUpdate Operation")
name = input("Enter the Name to be Updated: ")
print("Details for Updated")
age = int(input("Age: "))
gender = input("Gender: ")
stuClass = input("Class: ")
print("Enter the marks of the respective subjects")
py = int(input("Python: "))
ad = int(input("Android: "))
e1 = int(input("Elective 1: "))
e2 = int(input("Elective 2: "))
sql = "UPDATE student SET
age=('%d'),gender=('%s'),Class=('%s'),py=('%d'),ad=('%d'),e1=('%d'),e2=('%d') where
name=('%s')" % (age,gender,stuClass,py,ad,e1,e2,name)
try:
cursor.execute(sql)
db.commit()
print("\n\t\t****Record Updated Successfully***")
except:
db.rollback()
elif op==4:
print("\n\t\tDisplay Operation")
print("Details Student")
sql = "SELECT * FROM student"
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print("Name: ",row[0])
print("Age: ",row[1])
print("Gender: ",row[2])
print("Class: ",row[3])
print("\tMarks of the respective subjects")
print("Python: ",row[4])
print("Android: ",row[5])
print("Elective 1: ",row[6])
print("Elective 2: ",row[7])
else:
print("\n\tWrong Operation")
ch=(input("Do you have any other transaction(Y/N): ")).capitalize()
db.close()
OUTPUT
Various Operation
1.Insert
2.Delete
3.Update
4.Display
Enter Your Choice1
Insert Operation
Name: Sri
Age: 19
Gender: Male
Class: III BSC IT
Enter the marks of the respective subjects
Python: 48
Android: 47
Elective 1: 49
Elective 2: 46
Various Operation
1.Insert
2.Delete
3.Update
4.Display
Enter Your Choice3
Update Operation
Enter the Name to be Updated: Sri
Details for Updated
Age: 20
Gender: Male
Class: III BSC IT
Enter the marks of the respective subjects
Python: 47
Android: 40
Elective 1: 43
Elective 2: 41
Display Operation
Details Student
Name: Sri
Age: 20
Gender: Male
Class: III BSC IT
Marks of the respective subjects
Python: 47
Android: 40
Elective 1: 43
Elective 2: 41
Do you have any other transaction(Y/N): y
Various Operation
1.Insert
2.Delete
3.Update
4.Display
Enter Your Choice2
Delete Operation
Enter the Name to be Deleted: Sri