Prince
Prince
Prince
sum1 = 0
if n % i == 0:
sum1 = sum1 + i
if sum1 ==n:
else:
no1 = no
sum = 0
while no>0:
ans = no % 10;
if sum == no1:
else:
num = int(input ("Enter the number for calculating its factorial : "))
fact = 1
i=1
while i<=num:
fact = fact * i
i=i+1
n1, n2 = 0, 1
count=0
if nterms <= 0:
elif nterms == 1:
print (n1)
else:
print (n1)
nth = n1 + n2
n1 = n2
n2 = nth
count+= 1
Program 6: Write a Program to enter the string and check if it’s palindrome or not using loop.
return s == s[::-1]
if ans:
print ("Yes")
else:
print ("No")
Program 7: Recursively find the factorial of a natural number.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
if num < 0:
elif num == 0:
else:
Assume we have the following file, located in the same folder as Python:
Lines = file1.readlines()
count = 0
count += 1
Assume we have the following file, located in the same folder as Python:
if "a" in line:
else:
file2.write(line)
file1.close()
file2.close()
Program 10: Read a text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file.
vowels=0
consonants=0
uppercase=0
lowercase=0
str1 = file1.read()
for i in str1:
lowercase += 1
uppercase += 1
for j in str1:
j=j.lower()
vowels += 1
else:
if j.isalpha():
consonants += 1
import pickle
import sys
dict={}
def write_in_file():
file=open("stud2.dat","ab")
for i in range(no):
pickle.dump(dict, file)
file.close()
def display():
file=open("stud2.dat", "rb")
try:
while True:
stud=pickle.load(file)
print(stud)
except EOFError:
pass
file.close()
def search():
file=open("stud2.dat", "rb")
found=0
try:
while True:
data=pickle.load(file)
if data["roll"]==r:
print(data)
found = 1
break
except EOFError:
pass
if found==0:
file.close()
while True:
if ch==1:
write_in_file()
elif ch==2:
display()
elif ch==3:
search()
elif ch==4:
sys.exit()
Program 12: Write a random number generator that generates random numbers between 1 and
6(simulates a dice).
import random
a=[]
a.append(random.randint(1, 6))
Stack concept:
stack.append("Four")
stack.append("Five")
OUTPUT:
Queue concept:
queue.append("Four")
queue.append("Five")
print("pop 1",queue.pop(0))
print("pop 2",queue.pop(0))
OUTPUT:
Program 14: Take a sample of ten phishing e-mails (or any text file) and find most commonly
occurring word(s)
ph1= ["Robocalls are on the rise. Be wary of any pre-recorded messages you might receive"]
ph2=["our access to your library account is expiring soon due to inactivity. To continue to have
access to the library services account, you must reactivate your account. For this purpose, click the
web address below or copy and paste it into your web browser. A successful login will activate your
account and you will be redirected to your library profile"]
ph3=["We detected unknown IP access on our date base computer system our security requires you
to verify your account for secure security kindly Click Here and verify your account"]
ph5=["As we prepare to start the 2016 Tax filling season, we have undergone slight changes in the
filling process to make filling for your refund easier and to prevent unnecessary delays."]
ph6=["Hello, You have an important email from the Human Resources Department with regards to
your December 2015 Paycheck"]
ph7=["Your parcel (shipping document) arrived at the post office. Here is your Shipping
Document/Invoice and copy of DHL receipt for your tracking which includes the bill of lading and
DHL tracking number, the new Import/Export policy supplied by DHL Express. Please kindly check the
attached to confirm accordingly if your address is correct, before we submit to our outlet office for
dispatch to your destination"]
ph8=["Your Itunes-ID has been disabled .You've place your account under the risk of termination by
not keeping the correct informations .Please verify your account as soon as possible.Ready to check
?Click here to get back your account."]
ph9=["Hello, Please refer to the vital info I've shared with you using Google Drive. Click
https://www.google.com/drive/docs/file0116 and sign in to view details.."]
ph10=["I recently read your last article and it was very useful in my field of research. I wonder, if
possible, to send me these articles to use in my current research:This email is enclosed in the
Marquette University secure network, hence access it below.Access the documents here
<http://gabrielramon.be/<link removed>,***Ensure your login credentials are correct to avoid
cancellations** Part of the changes include updating our database with your information."]
words1=ph1[0].split()+ph2[0].split()+ph3[0].split()+ph4[0].split()+ph5[0].split()+ph6[0].split()+ph7[0].
split()+ph8[0].split()+ph9[0].split()+ph10[0].split()
stopwords=['to','the','and','or','you','your','with','have','had','has','of','in','our','is','for','it','will']
words=[]
for i in words1:
if i not in stopwords:
words.append(i)
word_counts = Counter(words)
top_five = word_counts.most_common(5)
print(top_five)
Program 15: Read a text file line by line and display each word separated by a #.
School programming"""
fh=open(r"myfile.txt","r")
item=[]
a=""
while True:
a=fh.readline()
words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))
Program 16: Create a student table and insert data. Implement the following SQL commands on the
student table:
ALTER table to add new attributes / modify data type / drop attribute
GROUP BY and find the min, max, sum, count and average.
#Switched to a database
mysql> USE GVKCV;
Database changed
#Describing table
mysql> desc student;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | char(10) | YES | | NULL | |
+-----------+----------+------+-----+----------+-------+
6 rows in set (0.21 sec)
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.11 sec)
#Droping a attribute
mysql> alter table student
-> drop column computers;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
#Describing table
mysql> desc student;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
5 rows in set (0.14 sec)
#ORDER BY BRANCH
#ACTUAL DATA
mysql> SELECT *FROM STUDENT;
+--------+--------+----------+--------+-------+-------+
| ROLLNO | BRANCH | NAME | TELUGU | HINDI | MATHS |
+--------+--------+----------+--------+-------+-------+
| 102 | MPC | student2 | 60 | 61 | 62 |
| 103 | BIPC | student3 | 70 | 71 | 72 |
| 104 | BIPC | student4 | 80 | 81 | 82 |
| 105 | BIPC | student5 | 90 | 91 | 92 |
| 106 | BIPC | student6 | 40 | 41 | 42 |
| 107 | MPC | student7 | 63 | 64 | 65 |
+--------+--------+----------+--------+-------+-------+
6 rows in set (0.00 sec)
mysql> SELECT BRANCH,COUNT(*)
-> FROM STUDENT
-> GROUP BY BRANCH;
+--------+----------+
| BRANCH | COUNT(*) |
+--------+----------+
| MPC | 2 |
| BIPC | 4 |
+--------+----------+
2 rows in set (0.01 sec)
mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
if mycon.is_connected()==False:
cursor=mycon.cursor()
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()
Program 18: Integrate SQL with Python by importing the pymysql module
mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
cursor=mycon.cursor()
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()