Computer programming
11/1/2024
ROLL NO-2024UIT3082
NETAJI SUBHAS UNIVERSITY OF
TECHNOLOGY MAIN CAMPUS
practical file
By Aakash
Prac cal 1
Aim-Install Python and set up the development environment.;
Aim-Write a Python program to print "Hello, World!”;.
CODE: print(“hello world!)
Aim- Write a Python program to calculate the area of a circle given the radius
radius=int(input("enter the radius :"))
area=3.14*radius*radius
print(area)
OUTPUT:
Aim-Write a Python program to check if a number is even or odd.; Implement a simple
calculator using condi onal statements; Write a Python program to print the Fibonacci series
using a for loop.
#EVEN OR ODD CHECKER
num=int(input(" enter the number:"))
if num%2==0:
print("even number",num)
else:
print("odd number",num)
OUTPUT:
Aim-.; Implement a simple calculator using condi onal statements;
while True:
num1=int(input("enter the number 1 : "))
num2=int(input("enter the number 2 :"))
op=input("enter the operator:: ")
if op=="+":
print("addi on of both nummbers",num1+num2)
elif op=="-":
print("difference of both numbers:",num1-num2)
elif op=="*":
print("mul plica on of both numbers:",num1*num2)
elif op=="//":
print("division of both numbers",num1//num2)
ch=input("enter 1 to con nue: ")
if ch!="1":
break
OUTPUT:
Aim-; Write a Python program to print the Fibonacci series using a for loop.
n=int(input("enter the number: "))
n1=0
n2=1
print("fibonacci series",end="")
for i in range(2,n):
n3=n1+n2
n1=n2
n2=n3
print(n3,sep=" ")
OUTPUT:
Prac cal-3
Aim-Implement a func on to check if a given string is a palindrome.; Perform various
opera ons on lists (e.g., sor ng, slicing
CODE:
str1=input("enter the string: ")
if str1==str1[::-1]: print("string is palindrome")
else: print("string is not palindrome")
OUTPUT:
Aim-Perform various opera ons on lists (e.g., sor ng, slicing).;
CODE:
list1=[1,3,4,8,5,3,7]
list1.append(10)
print(list1)
list1.insert(0,13)
print(list1)
list1.sort()
print(list1)
list1.extend([1,2,3,4])
print(list1)
OUTPUT:
Aim-.; Use dic onaries to store and retrieve student grades.
dic={"name":["aakash","ravi","sujal"],"marks":[90,65,80]}
for i in dic["marks"]:
if i>80:
print("grade","A",i)
elif 60<i<80:
print("grade","B",i)
OUTPUT:
Prac cal-4
Aim-Create a class to represent a book with a ributes and methods.; Implement inheritance
by crea ng subclasses for different types of books.;
Code :
class book:
def __init__(self,booktype,bookname,publisher):
self.booktype=booktype
self.bookname=bookname
self.publisher=publisher
def bname(self):
print(self.bookname.upper())
def btype(self):
print(self.booktype)
def pub(self):
print(self.publisher)
class comic(book):
def __init__(self,writer):
super().__init__("comic","friendz","pawan k")
self.writer=writer
def func2(self):
print(self.writer)
b1=book("comic","friendz","pawan k")
c1=comic("shawn")
print(c1.writer)
print(c1.booktype) #showing inheritance
print(c1.bookname) #showing inheritance
print(c1.publisher)
print(b1.publisher)
output:
Aim-Write a generator func on to generate the Fibonacci series.
code:
a=int(input('Give amount: '))
def fib(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
print(list(fib(a)))
output:
Prac cal-5
Aim-Use lambda func ons, map, and filter to perform opera ons on a list.
Code:
list1=[1,2,3,4,56,68]
evens=filter(lambda x:x%2==0,list1)
print(list(evens)) #fiter out even number from list
output:
Code:
list1=[1,2,3,4,56,68]
evens=map(lambda x:x,list1)
print(list(evens)) #it will iter through the list
output:
MY MODULE:
Aim:-Create a module that contains func ons for mathema cal opera ons.; Import and use
func ons from external packages (e.g., math, random).
import math
def diff(a,b):
print(a-b)
def sum1(a,b):
sum2=a+b
print(sum2)
def pro(a,b):
product=a*b
print(product)
def div(a,b):
div1=a/b
print(div1)
def sqrt2(a,b):
x=math.sqrt(a)
u=math.sqrt(b)
print("square root of a:",x)
print("square root of b:",u)
def sine1(a,b):
y=math.sin(a)
z=math.sin(b)
print("sine of a:",y)
print("sine of b:",z)
USING MY MODULE:
Code:
import mymodule as mm
a=mm.sum1(1,2)
b=mm.diff(3,2)
c=mm.sqrt2(4,9)
p=mm.pro(4,3)
x=mm.sine1(30,45)
print(a)
print(b)
print(c)
print(p)
print(x)
output :
Prac cal-6
Aim-Create and manipulate NumPy arrays.Perform basic opera ons and
indexing on arrays.
Code:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr1=np.array([2,8,4,5,7])
d=arr.view()
x = arr.copy()
y=np.concatenate((arr,arr1))
c=np.sort(arr1)
z=np.where(arr==5)
arr[0] = 42
print(arr)
print(x,"\n",c)
print(y,"\n",z)
output:
Prac cal- 7
Aim-Implement string opera ons (e.g., concatena on, slicing)
Code:
Str1=”aakash”
Str2=”yadav”
Str3=str1+str2
print(str3)
print(str1[::-1])
output:
Aim:-Use regular expressions to validate email addresses
Code:
Import re
email_pa ern = re.compile(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$')
def validate_email(email):
if email_pa ern.match(email):
print(f"{email} is a valid email address.")
else:
print(f"{email} is not a valid email address.")
output
Prac cal-8
Aim-Read data from a text file and perform opera ons.; Handle excep ons for
file opera ons and input valida on .
Code:
def func1():
f=open("hype.txt","r")
f1=open("hello.txt","w")
s=f.readlines()
print(s) #show a list of lines of file in a form of list.
w=f.writelines(s) #it will takes a list of lines a store in the file
print(w)
func1()
import pickle
def func1():
f=open("hello.dat","rb")
c=0
try:
s=pickle.load(f) #it will read the binary file
if s[0]=="h":
c+=1
print("no of words star ng with h",c)
except:
print("end of file error")
f.close()
func1()
import pickle
def func1():
f=open("hello.dat","rb")
c=0
l1=["aakash","ravi","himesh"]
while True:
pickle.dump(f,l1) #it will write in the file.
ch=input("enter 1 to con nue: ")
if ch!=1:
break
func1()