Cs Shakthi Practicals
Cs Shakthi Practicals
Arithmetic operations
Aim:
To display arithmetic operations
Coding:
a=int(input("enter a number:"))
b=int(input("enter a number:”))
print("a+b=",a+b)
print("a-b=",a-b)
print("a*b=",a*b)
print('a/b=',a/b)
print('a to the power b=',a**b)
Output:
Result:
The above code was used and the desired output was acquired.
2)
Perfect number
Aim:
To verify whether a number is a perfect number or not
Coding:
a=int(input("enter a number:"))
g=0
for i in range(1,a):
if a%i==0:
g+=i
if g==a:
print(a,"is a perfect number")
else:
print(a,"is not a perfect number")
Output:
Result:
The above code was used and the desired output was acquired.
3)
Armstrong number
Aim:
To verify if the given number is armstrong number or not
Coding:
a=int(input("enter a number:"))
b=a
g=0
while b>0:
r=b%10
g+=r**3
b=b//10
if g==a:
print(a,"is an armstrong number")
else:
print(a,"is not an armstrong number")
Output:
Result:
The above code was used and the desired output was acquired.
4)
Palindrome number
Aim:
To verify if a number is palindrome or not
Coding:
a=int(input("enter a number:"))
b=a
f=[]
while b>0:
r=b%10
f.append(r)
b=b//10
g=f[-1:-len(f)-1:-1]
if f==g:
print(a,"is palindrome")
else:
print(a,"is not palindrome")
Output:
Result:
The above code was used and the desired output was acquired.
5)
Lists
Aim:
To use list functions
Code:
c='hello'
b='hello there'
a=list(c)
print('list conversion:',a)
a.append(b)
print("length=",len(a))
print("after appending to list",a)
d=[10,20,10,30,10,40]
print('counting no.of 10:',d.count(10))
print("indexing:",d.index(20))
print("popping elements:",d.pop())
print("max:",max(d))
print("min:",min(d))
print("sum:",sum(d))
Output:
Result:
The above code was used and the desired output was acquired.
6)
Floyd’s triangle
Aim:
To write a code that outputs Floyd’s triangle
Coding:
a=int(input("enter number of lines:"))
g=0
for i in range(1,a+1):
for j in range(0,i):
g+=1
print(g,end="")
print()
Output:
Result:
The above code was used and the desired output was acquired.
7)
List manipulation
Aim:
To write a program which doubles the numbers at odd places and halves the
numbers at even places.
Code:
a=eval(input("enter a list of numbers:"))
b=[]
for i in range(1,len(a)+1):
if i%2==0:
g=a[i-1]/2
else:
g=a[i-1]*2
b.append(g)
print(b)
Output:
Result:
The above code was used and the desired output was acquired.
8)
Counting number of even and odd numbers
Aim:
To Write a Python program input n numbers in tuple and pass it to function to count
how many even and odd numbers are entered.
Code:
a=eval(input("enter a tuple of numbers:"))
o=0
e=0
for i in a:
if i%2==0:
e+=1
else:
o+=1
print("number of even numbers=",e)
print("number of odd numbers=",o)
Output:
Result:
The above code was used and the desired output was acquired.
9)
Counting the number of vowels in a string
Aim:
To Write a Python program to pass a string to a function and count how
many vowels present in the string.
Code:
a=input("enter a string:")
v=0
for i in a:
if i in('a','e','i','o','u'):
v+=1
print("number of vowels in sentence=",v)
Output:
Result:
The above code was used and the desired output was acquired.
10)
Random number generator
Aim:
To write a program to generate random Number between 100 and 1000
Code:
import random as rand
print(rand.randint(100,1000))
Output:
Result:
The above code was used and the desired output was acquired.
11)
Counting the number of times “my” has occurred
Aim:
To write a python program to Count the number of times the word “my”
has occurred in the given text file.
Code:
a=input("enter a sentence:")
a=a+" "
l=[]
s=""
for i in a:
if i.isspace():
l.append(s)
s=""
else:
s=s+i
g=0
for i in l:
if i=="my" or i=="My":
g+=1
print("number of times:",g)
Output:
Result:
The above code was used and the desired output was acquired.
12)
Counting digits and uppercase letters
Aim:
To write a python program to Count the number of digits and
uppercase letters in a text file.
Code:
a=input("enter a sentence:")
d=0
u=0
for i in a:
if i.isdigit():
d+=1
elif i.isupper():
u+=1
print("no. of uppercase:",u)
print("no.of digits:",d)
Output:
Result:
The above code was used and the desired output was acquired.
13)
Creating and reading a binary file
Aim:
To Write a program to enter Student no, name and marks of three subjects until
the user ends of a student using a binary file and to read a file. Code:
import pickle
a=open("student.bin",'ab')
g='y'
while g=='y':
a=open("student.bin",'ab')
b=[]
c={}
n=int(input("enter student ID:"))
m=input("enter student name:")
c['math']=input("enter math marks:")
c['phy']=input("enter physics marks:")
c['chem']=input("enter chemistry marks:")
b.append(n)
b.append(m)
b.append(c)
pickle.dump(b,a)
a.close()
g=input("enter y if you wish to continue or n if you wish
to stop:") h=open("student.bin",'rb')
t=pickle.load(h)
print(t)
Output:
Result:
The above code was used and the desired output was acquired.
14)
Searching records in a binary file
Aim:
To write a program to open file stu.dat and search for records with roll no as 12
and 24. If found, display the records.
Code:
import pickle
a=open("stu.dat",'rb')
g=False
try:
while True:
b=pickle.load(a)
if b['roll no']==14 or b['roll no']==24:
print(b)
g=True
except EOFError:
if g==False:
print("record not found")
else:
print("record found")
Output:
Result:
The above code was used and the desired output was acquired.
15)
Creating a CSV file products
Aim:
To write a program creating a CSV file to enter the product details.
Code:
import csv
b=int(input("enter number of records to be entered:"))
for i in range(b):
a=open("prod.csv",'w')
c=[]
d=input("enter product name:")
e=int(input("enter qty:"))
f=int(input("enter price:"))
c.extend([c,d,e])
g=csv.writer(a)
g.writerow(c)
a.close()
Output:
Result:
The above code was used and the desired output was acquired.
16)
Stack operations
Aim:
To write a program to perform stack operations.
Code:
l=[]
def pushing():
for i in range(5):
a=int(input("number:"))
l.append(a)
def printing():
for i in range(5):
print(l.pop())
pushing()
printing()
Output:
Result:
The above code was used and the desired output was acquired.
17)
Printing vowels using stacks
Aim:
To write a program to print the vowels in a string using stacks concept
Code:
l=input("enter a statement:")
a=list(l)
for i in range(len(a)):
b=a.pop()
if b in ['a','e','i','o','u']:
print(b)
Output:
Result:
The above code was used and the desired output was acquired.
18)
MySQL table student
Aim:
To create a table student and execute the following commands
Code:
Output:
Result:
The above code was used and the desired output was acquired.
19)
Library and student joins
Aim:
To Create a table library and execute the following commands
Code:
Output:
Result:
The above code was used and the desired output was acquired.
20)
Two table questions
Aim:
To use operations on two tables and get the desired outputs
sec)
21.Python Interface
Aim:
To establish a connection between python and my sql and accomplish the queries
Coding:
Import mysql.connector
mycon=
mysql.connector.connect(host=’localhost’,username=’root’,database=’sakthi’,password=’123
456’)
cursor=mycon.cursor()
(i)cursor.execute(‘select*from student’)