Program 2: Write a program to find whether an inputted number is perfect or not.
Solution:
# To find whether a number is perfect or not
def pernum(num):
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
print('Not a perfect number')
pernum(6)
pernum(15)
Program 3: Write a Program to check if the entered number is Armstrong or not.
Solution:
# Program to check if the entered number is Armstrong or not.
#An Armstrong number has sum of the cubes of its digits is equal to the number itself
no=int(input("Enter any number to check : "))
no1 = no
sum = 0
while(no>0):
ans = no % 10;
sum = sum + (ans * ans * ans)
no = int (no / 10)
if sum == no1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Program 4: Write a Program to find factorial of the entered number.
Solution:
#Program to calculate the factorial of an inputted number (using while loop)
num = int(input("Enter the number for calculating its factorial : "))
fact = 1
i=1
while i<=num:
fact = fact*i
i=i+1
print("The factorial of ",num,"=",fact)
Program 5: Write a Program to enter the number of terms and to print the Fibonacci
Series.
Solution:
#fibonacci
i =int(input("enter the limit:"))
x=0
y=1
z=1
print("Fibonacci series \n")
print(x, y,end= " ")
while(z<= i):
print(z, end=" ")
x=y
y=z
z=x+y
Program 6: Write a Program to enter the string and to check if it’s palindrome or not using loop.
Solution:
# Program to enter the string and check if it’s palindrome or not using ‘for’ loop.
msg=input("Enter any string : ")
newlist=[]
newlist[:0]=msg
l=len(newlist)
ed=l-1
for i in range(0,l):
if newlist[i]!=newlist[ed]:
print ("Given String is not a palindrome")
break
if i>=ed:
print ("Given String is a palindrome")
break
l=l-1
ed = ed - 1
Program 7: Write a Program to show the outputs based on entered list.
Solution:
my_list = ['p','r','o','b','e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
# Error! Only integer can be used for indexing
# my_list[4.0]
# Nested List
n_list = ["Happy", [2,0,1,5]]
# Nested indexing
# Output: a
print(n_list[0][1],n_list[0][2],n_list[0][3])
# Output: 5
print(n_list[1][3])
Program 8: Write a Program to enter the numbers in a list using split () and to use all the functions
related to list.
Solution:
#Program to enter the numbers in a list using split () and to use all the functions related to
list.
# numbers = [int(n, 10) for n in input().split(",")]
# print (len(numbers))
memo=[]
for i in range (5):
x=int(input("enter no. \n"))
memo.insert(i,x)
i+=1
print(memo)
memo.append(25)
print("Second List")
print(memo)
msg=input("Enter any string : ")
newlist=[]
newlist[:0]=msg
l=len(newlist)
print(l)
Program 9: Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order.
Solution:
#Floyd's triangle
n=int(input("Enter the number :"))
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print (j,end=' ')
print('\n')
Program 10: Write a Program to find factorial of entered number using user-defined module
fact().
Solution:
#Using function
import factfunc
x=int(input("Enter value for factorial : "))
ans=factfunc.fact(x)
print (ans)
Program 11: Write a Program to enter the numbers and find Linear Search, Binary Search,
Lowest Number and Selection Sort using list/array code.
Solution:
arr=[]
def array_operation():
ch=1
while ch!=10:
print('Various Array operation\n')
print('1 Create and Enter value\n')
print('2 Print Array\n')
print('3 Reverse Array\n')
Program 12: Write a Program to read data from data file and show Data File Handling
related functions utility in python.
Solution:
f=open("test.txt",'r')
print(f.name)
f_contents=f.read()
print(f_contents)
f_contents=f.readlines()
print(f_contents)
f_contents=f.readline()
print(f_contents)
for line in f:
print(line, end='')
f_contents=f.read(50)
print(f_contents)
size_to_read=10
f_contents=f.read(size_to_read)
while len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
Program 13: Write a Program to read data from data file in append mode and use
writeLines function utility in python.
Solution:
count=0
for sentence in read:
line=sentence.split()
times+=1
for each in line:
line2=each
times2+=1
if chk==line2:
count+=1
print("The search String ", chk, "is present : ", count, "times")
print(times)
print(times2)
Program 15: Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.
Solution:
#Program to read data from data file in read mode and
#append the words starting with letter ‘T’
#in a given file in python
f=open("test.txt",'r')
read=f.readlines()
f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)
Program 16: Write a Program to show MySQL database connectivity in python.
Solution:
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password='',db='school')
stmt=con.cursor()
query='select * from student;'
stmt.execute(query)
data=stmt.fetchone()
print(data)
Program 17: Write a Python program to implement all basic operations of a stack, such as
adding element (PUSH operation), removing element (POP operation) and displaying the
stack elements (Traversal operation) using lists.
Solution:
#Implementation of List as stack
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
Program 18: Write a program to display unique vowels present in the given word using
Stack.
Solution:
#Program to display unique vowels present in the given word
#using Stack
vowels =['a','e','i','o','u']
word = input("Enter the word to search for vowels :")
Stack = []
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print("The number of different vowels present in",word,"is",len(Stack))
c=input("Do you want to continue or not: ")
Inserting a record in ‘emp’
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
# Prepareing SQL statement to insert one record with the given values
sql = "INSERT INTO EMP VALUES (1,'ANIL KUMAR',86000);"
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Fetching all the records from EMP table having salary more than 70000.
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
sql = "SELECT * FROM EMP WHERE SALARY > 70000;"
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store in
resultset
resultset = cursor.fetchall()
for row in resultset:
print (row)
except:
print ("Error: unable to fetch data")
db1.close()
Updating record(s) of the table using UPDATE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is less than
80000
sql = "UPDATE EMP SET salary = salary +1000 WHERE salary<80000;"
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()