Set:-1
Practical – 1
Aim: Write a Python program to print “Hello World”.
Code: print("Hello world!")
Output: Hello world
page 1
Practical – 2
Aim: Write a Python program to swap two variables using third variable.
Code: x= int(input("Enter The first variable "))
y=int(input("Enter the second variable"))
print(x,y)
temp = x
x=y
y=temp
print("After swapping value of x",x)
print("After swapping value of y",y)
print(x,y)
Output: Enter the first variable 3
Enter the second variable4
3 4
After swapping value of x 4
After swapping value of y 3
4 3
page 2
Practical – 3
Aim: Write a Python program to swap two variables without third variable.
Code: ap=int(input("Enter the value of first variable"))
q=int(input("Enter the value of second variable"))
print(p,q)
p,q=q,p
print(p,q)
Output: Enter the value of first variable5
Enter the value of second variable9
5 9
9 5
page 3
Practical – 4
Aim: Write a Python program to find the square root of a positive number.
Code: y=int(input("Enter the value of y"))
print(y)
square_root= y**0.5
print("here the square root of y :", square_root)
Output: Enter the value of y49
49
here the square root of y : 7.0
page 4
Practical – 5
Aim: Write a Python program to find area of a rectangle and circle.
Code: l=float(input("Enter the value of length"))
b=float(input("Enter the value of breadth"))
r=float(input("Enter the vale of radius"))
Rect_radius=l*b
Circle_radius=(2*3.14*(r*r))
print("this is the rectangle area:",Rect_radius)
print("This is the area of circle",Circle_radius)
Output: Enter the value of length 5.6
Enter the value of bredth7.3
Enter the vale of radius 3.7
this is the rectangle area: 40.879999999999995
This is the area of circle 85.9732
page 5
Practical – 6
Aim: Write a Python program to find sum of n natural numbers without loop.
Code: n=int(input("Enter the value of n number "))
sum_n=n*(n+1)/2
print("this is the sum of n :",sum_n)
Output: Enter the value of n number 45
this is the sum of n : 1035.0
page 6
Practical – 7
Aim: Check various arithmetic operators of Python.
Code: am=float(input("Enter the vale of m"))
n=float(input("Enter the value of n"))
print("This is the sum of two number:",m+n)
print("This is the subtraction of two number:",m-n)
print("This is the multiplication of two number:",m*n)
print("This is the division of two number:",m/n)
print("This is the division floor of two variable:",m//n)
print("This is the modulo of two variable ",m%n)
print("This is the exponent of two variable",m**n)
Output: eEnter the vale of m5
Enter the value of n10
This is the sum of two number: 15.0
This is the subtraction of two number: -5.0
This is the multiplication of two number: 50.0
This is the division of two number: 0.5
This is the division floor of two variable: 0.0
This is the modulo of two variable 5.0
This is the exponent of two variable 9765625.0
page 7
Practical – 8
Aim: Write a Python program to check output of modulo operator.
Code: a=float(input("Enter the valus of a"))
b=float(input("Enter the value of b"))
c=a%b
c1=-a%b
c2=a%-b
print(c,c1,c2)
Output: Enter the value of a5
Enter the value of b8
5.0 3.0 -3.0
page 8
Set:-2
Practical – 1
Aim: WAP to check whether entered number is even or odd.
Code: num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output: Enter a number: 456
456 is Even
page 9
Practical – 2
Aim: WAP to find whether entered number is positive, negative or zero.
Code: number = int(input("Enter a number:"))
if(number>0):
print("number is positive number".format(number))
elif(number<0):
print("number is negative number".format(number))
else:
print("number is zero")
Output: Enter a number:-67
number is negative number
page 10
Practical – 3
Aim: WAP to find roots of quadratic equations if roots are real.
Code: aimport math
a=int(input("Enter the value of a"))
b=int(input("Enter the value of b"))
c=int(input("Enter the value of c"))
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))
if dis > 0:
print("real and different roots")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("real and same roots")
print(-b / (2 * a))
# when discriminant is less than 0
else:
print("Complex Roots")
print(- b / (2 * a), + i, sqrt_val)
print(- b / (2 * a), - i, sqrt_val)
equationroots(a, b, c)
Output: real and different roots
2.0
-12.0
page 11
Practical – 4
Aim: WAP to check whether entered character is vowel or consonant.
Code: list = ["a", "e", "i", "o", "u"]
a = input("Enter any character: ")
if a in list:
print(f"{a} is a vowel")
else:
print(f"{a} isn't a vowel")
Output: Enter any
character: f
f isn't a vowel
page 12
Practical – 5
Aim: WAP to find maximum of three numbers (nested if-else).
Code: a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if(a > b) :
if (a > c) :
print ("Maximum is ", a)
else :
print ("Maximum is", c )
else :
if (b > c) :
print ("Maximum is ", b)
else :
print ("Maximum is", c )
Output: Enter first
number: 34
Enter second number: 44
Enter third number: 22
Maximum is 44
page 13
Practical – 6
Aim: WAP to calculate the salary of an employee based on following conditions (nested if-else):
1. if degree = B.E. and experience < 5 years, salary=30000
2. if degree = B.E. and experience >= 5 years, salary=40000
3. if degree = M.E. and experience < 5 years, salary=50000
4. if degree = M.E. and experience >= 5 years, salary= 60000
Code: degree = input("Enter degree(B.E. or M.E.): ")
experience = int(input("Enter Experience: "))
if (degree == "B.E") :
if (experience < 5) :
salary = 30000
else :
salary = 40000
else :
if (experience < 5) :
salary = 50000
else :
salary = 60000
print("Employees salary is", salary)
Output: Enter degree(B.E. or M.E.): be
Enter Experience: 7
Employees salary is 60000
page 14
Practical – 7
Aim: WAP to check whether entered input is character, digit or special symbol using ladder if-
else.
Code: ch = input("Enter any character: ")
if (ch.isalpha()):
print('Character is alphabet')
elif (ch.isdigit()):
print('Character is digit')
else :
print('Character is special character')
Output: Enter any character: h
Character is alphabet
page 15
Set:-3
Practical – 1
Aim: WAP to find sum of first N numbers.
Code: n = int(input("Enter a number: "))
sum = (n*(n+1))/2
print(f"The sum of first {n} natural numbers is {sum}")
Output: Enter a number: 43
The sum of first 43 natural numbers is 946.0
page 16
Practical – 2
Aim: WAP to find sum of N scanned numbers.
Code: n = int(input("Enter the value of Total Number: "))
sum = 0
for i in range(n):
num = int(input("Enter a number: "))
sum = sum + num
print(f"The sum of {n} scanned numbers is {sum}")
Output: Enter the value of Total Number: 5
Enter a number: 3
Enter a number: 53
Enter a number: 26
Enter a number: 4
Enter a number: 9
The sum of 5 scanned numbers is 95
page 17
Practical – 3
Aim: Write a Python program to find N!.
Code: def fact(n):
if n == 1:
return 1
else:
return n * fact(n-1)
n = int(input("Enter a number: "))
print(f"Factorial of {n} is a", fact(n))
Output: enter a number: 7
Factorial of 7 is a 5040
page 18
Practical – 4
Aim: Write a Python program to print Fibonacci series upto n terms.
Code: n = int(input("Enter a number: "))
n0, n1 = 0 , 1
temp = 0
print("Fibonacci Series: ")
while temp <= n:
print(temp)
n0 = n1
n1 = temp
temp = n0 + n1
Output: Enter a
number: 34
Fibonacci Series:
13
21
34
page 19
Practical – 5
Aim: WAP to find the reverse of given numbers (Example 2564-4652).
Code: n = int(input("Enter a number: "))
num = str(n)
num1 = num[::-1]
num2 = int(num1)
print(num2)
Output: Enter a number: 8435
5348
page 20
Practical – 6
Aim: WAP to check whether entered number is prime or not.
Code: n = int(input("Enter a number: "))
count = 0
for i in range(1, n+1):
if n % i == 0:
count = count + 1
if count == 2:
print(f"{n} is a prime number")
else:
print(f"{n} is not a prime number")
Output: Enter a number: 45
45 is not a prime number
page 21
Practical – 7
Aim: WAP to print all even numbers between 1 to n except the numbers divisible by 6.
Code: n = int(input("Enter a number: "))
print(f"Even number between 1 to {n} except the numbers divisible by 6.")
for i in range(1, n+1):
if i%2==0:
if i%6==0:
pass
else:
print(i)
Output: Enter a number: 55
Even number between 1 to 55 except the numbers divisible by 6.
2
4
8
10
14
16
20
22
26
28
32
34
38
40
44
46
50
52
page 22
Practical – 8
Aim:Write a python program to calculate N!.
Code: def fact(n):
if n == 1:
return 1
else:
return n * fact(n-1)
n = int(input("Enter a number: "))
print(f"Factorial of {n} is a", fact(n))
Output: Enter a number: 4
Factorial of 4 is a 24
page 23
Practical – 9
Aim: Write a python program to check whether given number is Armstrong or not.
Code: num = int(input("Enter a number: "))
temp = num
sum = 0
num_digits = len(str(num))
while temp > 0:
digit = temp % 10
sum = sum + digit ** num_digits
temp = temp // 10
if num == sum:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
Output: Enter a number: 121
121 is not an Armstrong number.
page 24
Practical – 10
Aim: Write a python program to check whether given number is Palindrome or not.
Code: n = int(input("Enter a number: "))
num = str(n)
num1 = num[::-1]
num2 = int(num1)
if n == num2:
print(f"{n} is a palindrome number")
else:
print(f"{n} is not a palindrome number")
Output: Enter a number: 121
121 is a palindrome number
page 25
Practical – 11
Aim:
WAP to print the following:
1) 1 2) * * * * *
12 ****
123 ***
1234 **
12345 *
For 1:-
Code: n = int(input("Enter a number: "))
print("First Output")
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=" ")
print()
Output: Enter a number: 6
First Output
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
for2:-
Code: print()
print("Second Output")
for i in range(1, n+1):
for j in range(1, n-i+2):
page 26
print("*", end=" ")
print()
Output: Second Output
* * * * * *
* * * * *
* * * *
* * *
* *
page 27