Python Program| Exercises of Python Program

1.   Write a python program to print your address.
print("Name:Ms. Eliza Begum")
print("P.O:Tinsukia")
print("Dist:Tinsukia")
print("State:Assam")
print("Pin:786125")

Output:

Name:Ms. Eliza Begum
P.O:Tinsukia
Dist:Tinsukia
State:Assam
Pin:786125

2.   Write a program to store two integer values in two variables and using them calculate addition, subtraction, multiplication and division.
x,y=20,15
print("Addition: ",x+y)
print("Subtraction: ",x-y)
print("Multiplication: ",x*y)
print("Division: ",x/y)

Output:

Addition: 35
Subtraction: 5
Multiplication: 300
Division: 1.3333333333333333

 

3.   Write a program to print pay-slip using the following data.
Employee number is 100
Basic salary is 5000
DA must be calculated as 10% of basic
HRA must be calculated as 20% of basic
Total salary must be calculated as Basic+DA+HRA
ENo=100
BS=5000
DA=BS*10/100
HRA=BS*20/100
Total=BS+DA+HRA
print(“Pay Slip”)
print(“********************”)
print(“Employee Number:”,ENo)
print(“Basic salary:”,BS)
print(“DA:”,DA)
print(“HRA:”,HRA)
print(“Total Salary:”,BS+DA+HRA)
4.   Write a program to calculate total marks and percentage of a student of 5 subjects and based on the percentage display the grade according to the following criteria.

 

Below 30%          D
30-44                  C
45-54                  B
55-64                   B+
65-85                   A
Above 85%         A+
s1,s2,s3,s4,s5=map(int,input("Enter marks of 5 subjects of a student: ").split())
total=s1+s2+s3+s4+s5
per=(total/500*100)
if(per<30):
print("Grade: D")
elif(per>=30 and per<45):
print("Grade: C")
elif(per>=45 and per<55):
print("Grade: B")
elif(per>=55 and per<65):
print("Grade: B+")
elif(per>=65 and per<=85):
print("Grade: A")
else:
print("Grade: A+")

Output:

Enter marks of 5 subjects of a student: 80 78 56 98 67
Grade: A

5.   A company decided to give bonus to employees according to following criteria:
Time period of service                   Bonus
More than 20 years              25%
More than 10 years              15%
>=5 and <=10 years              8%
Less than 5 years                 5%

 

6.   Write a program to find the Factorial of a number

num = int(input(“Enter a number: “))

fact= 1

# check if the number is negative, positive or zero

if num < 0:

   print(“Sorry, factorial does not exist for negative numbers”)

elif num == 0:

   print(“The factorial of 0 is 1”)

else:

   for i in range(1,num + 1,1):

       fact= fact*i

   print(“Factorial: “,fact)

Output:

Enter a number: 5

Factorial: 120

 

7.   Write a program to find maximum of two numbers using function.

 

 

def maximum(a, b):

          

           if a >= b:

                        return a

           else:

                        return b

          

print(maximum(2, 7))

 

 

Output:

 

7

Method 2: Using inbuilt max() Function

 

a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
print("Larger number is", max(a,b))

 

Output:

 

Enter 1st number: 4

Enter 2nd number: 3

Larger number is:  4

 

 

8.   Python Program to Find Area of a Circle using function.

def findArea(r):

    PI = 3.142

    return PI * (r*r);

print(“Area is %.2f” % findArea(5));

9.   Find the Factorial of a Number Using Function.

def factorial(n):

    if n < 0:

        return 0

    elif n == 0 or n == 1:

        return 1

    else:

        fact = 1

        while(n > 1):

            fact *= n

            n -= 1

        return fact

num = 5

print(“Factorial of”,num,”is”,

factorial(num))

Output:

Factorial of 5 is 120

10.  Find the Factorial of a Number Using Recursive approach.

def factorial(n):

    if (n==1 or n==0)

          return 1

    else n * factorial(n – 1)

print(“Factorial of”,num,”is”,factorial(5))

Output:

Factorial of 5 is 120

Leave a Comment