0% found this document useful (0 votes)
32 views12 pages

Python Practical

Uploaded by

sinhamohit1947
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
32 views12 pages

Python Practical

Uploaded by

sinhamohit1947
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

PROGRAMMING

FUNDAMENTALS USING
PYTHON

PRACTICALS
NAME : MOHIT KUMAR

ROLL NO : 24HEL2129

COURSE : B.Sc.(H) ELECTRONICS

SEMESTER : I
1) AIM : Write a python menu driven program to
calculate area of circle, rectangle, square using if-elif-
else.
Program :
#Menu Driven

while True :

print('''

----------Menu Driven-----------

1. Area of Rectangle

2. Area of Sqaure

3. Area of Circle

4. Exit

--------------------------------''')

ch = int(input("Enter a Choice (1-4): "))

if ch == 1 :

l = int(input("Enter length of rectangle : "))

b = int(input("Enter breadth of rectangle : "))

print("Area of Rectangle :" , l*b)

elif ch==2 :

s = int(input("Enter side of square : "))

print("Area of Square : ",s*s)

elif ch==3 :

r = int(input("Enter radius of circle : "))

print("Area of circle : ",3.14*r*r)

elif ch==4 :

break

else :

print("Wrong Choice")
Output :
2) AIM : Write a python program to print Fibonacci series
up to a certain limit (use ‘while’).
Program :
#Fibonacci Series
n = int(input("Number of Fibonacci Series to be Printed: "))

a, b = 0, 1
print(a, b, end=' ')

i=0
while (i < n-2):
c=a+b
# add the last two numbers and print
print(c, end=' ')
a=b
b=c
i=i+1

Output :

3) AIM : Write a python program to print the Pascal


triangle.
Program :
# Pasacal Triangle
n = int(input("Enter no."))
for i in range(n):
print(' ' * (n - i), end='')
num = 1
for j in range(i + 1):
print(num, end=' ')
num = num * (i - j) // (j + 1)
print()

Output :

4) AIM : Write a python program to find HCF (GCD) of


two numbers.
Program :
# HCF of two numbers
num1 = int(input("Enter first Number :"))
num2 = int(input("Enter Second Number : "))
if num1>num2:
s = num2
else :
s= num1
for i in range (1,s+1):
if (num1 % i ==0) and (num2 % i==0):
hcf = i
print("HCF of both the numbers is :", hcf )

Output :

5) AIM : Write a python program to find LCM of two


numbers.
Program :
# LCM of two numbers
num1 = int(input("Enter first Number :"))
num2 = int(input("Enter Second Number : "))
if num1>num2:
g = num1
else :
g = num2
for i in range (1,g+1):
if (g%num1 ==0) and (g%num2==0):
lcm = g
break
print("LCM of both numbers is :",lcm)

Output :
6) AIM : Write a python program to illustrate the various
functions of the “Math” module, “Statistics” module in
python.
Program :
import math
import statistics

print("Math module functions :")


print("CONSTANTS :")
print("Value of Pi is : ", math.pi)
print("Value of Euler's number is : ", math.e)
print("Basic mathematical functions :")
print("Square root of 36 : ", math.sqrt(36))
print("Factorial of 5 : ", math.factorial(5))
print("5 raised to the power 2 : ", math.pow(5,2))
print("Other functions :")
print("Greatest common divisor of 12 and 15 : ", math.gcd(12,15))
print("180 Degrees to Radians : ", math.radians(180))
print("pi radians to degrees : ", math.degrees(math.pi))
print("Absolute value of -45.5 : ", math.fabs(-45.5))
print("Trigonometric functions")
print("Sine of 60 degrees : ", math.sin(60))
print("Cosine of 0 degrees : ", math.cos(0))
print("Tangent of 45 degrees : ", math.tan(45))
print("Logarithmic functions :")
print("Natural log of 10 : ", math.log(10))
print("Base 10 log of 100 : ", math.log10(100))
print("Base 2 log of 8 : ", math.log2(8))
print("Rounding functions :")
print("Floor of 6.9 : ", math.floor(6.9))
print("Ceiling value of 6.4 : ", math.ceil(6.4))
print("Statistics module functions")
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

print("Mean : ", statistics.mean(data))


print("Median : ", statistics.median(data))
print("Mode : ", statistics.mode(data))
print("Standard Deviation : ", statistics.stdev(data))
print("Variance : ", statistics.variance(data))

Output :
7) AIM : Write a python program to count number of
vowels using sets in given string.
Program :
#Vowels in a string
s = input("Enter string to check : ")
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
c=0
for i in s :
if i in vowels :
c+=1
print("Total numeber of vowels in string :",c)

Output :

8) AIM : Write a python program to remove all duplicates


from a given string in python.
Program :
#Remove duplicates
s = input("Enter a sting to check for duplicates :")
result = ""
for char in s:
if char not in result:
result += char

print("String after removing duplicates: ",result)

Output :
9) AIM : Write a python program to count positive and
negative numbers in a list.
Program :
#count negative positive
l = [1,6,3,7,-3,7,2,-4]
n,p = 0,0
for i in l :
if i>=0:
p+=1
else :
n+=1
print("Count of positive numbers in the list :",p)
print("Count of negative numbers in the list :",n)

Output :

10) AIM : Write a python program to find sum of


elements in list.
Program :
#sum of characters
l = [1,2,1,6,3,7,3,7,2,4]
s=0
for i in l:
k = int(i)
s+=k
print(“List =”, l )
print("Sum of characters of the list is :",s)
Output :

11) AIM : Write a python program to read a list of ‘n’


integers (positive or negative) and create two new lists
one having all positive numbers and the other having all
negative numbers from the given list. Print all three list.
Program :
#Seperate list for negative and positive
l = eval(input("Enter a list of integers (e.g., [1, 2, 3]): "))
p =[]
n =[]
for i in l :
if i>=0 :
p.append(i)
else :
n.append(i)
print("Normal list :",l)
print("Positive list :",p)
print("Negative list :",n)

Output :

You might also like