(Grade X) Part 1- Review of Basic Python-Exercise

 




Exercise Page number-168

Write a program for the following.

1.     To calculate the area of four walls of a room [A=2H(L+B)]

h = float(input("Enter height: "))

l = float(input("Enter length: "))

b = float(input("Enter breadth: "))

area = 2 * h * (l + b)

print("Area of four walls =", area)

 

2.     To calculate the sum, difference and product of any two numbers given by the user.

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

sum_ = a + b

diff = a - b

product = a * b

print("Sum =", sum_)

print("Difference =", diff)

print("Product =", product)

 

3.      To calculate the area of a triangle when three sides are given by the user. s=(a+b+c)/2​ A=s(sa)(sb)(sc)

import math

a = float(input("Enter side a: "))

b = float(input("Enter side b: "))

c = float(input("Enter side c: "))

s = (a + b + c) / 2

area = math.sqrt(s * (s - a) * (s - b) * (s - c))

print("Area of triangle =", area)

 

4.     To check whether the given number is even of odd

n = int(input("Enter a number: "))

if n % 2 == 0:

    print("The number is even")

else:

    print("The number is odd")

 

5.     To check whether the given number is positive, negative or neutral

n = float(input("Enter a number: "))

if n > 0:

    print("The number is positive")

elif n < 0:

    print("The number is negative")

else:

    print("The number is zero")

 

6.     To display the greatest number among the three numbers given by the user

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if a >= b and a >= c:

    print("Greatest number is", a)

elif b >= a and b >= c:

    print("Greatest number is", b)

else:

    print("Greatest number is", c)

 

7.     To display the middle number (neither greater nor smaller) from the three numbers given by the user.

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if (a > b and a < c) or (a < b and a > c):

    print("Middle number is", a)

elif (b > a and b < c) or (b < a and b > c):

    print("Middle number is", b)

else:

    print("Middle number is", c)

 

8.     To display all the numbers from 1 to 50. (Use for loop)

for i in range(1, 51):

    print(i)

 

9.     To display all the numbers from 100 to 1 (Use While loop)

i = 100

while i >= 1:

    print(i)

    i -= 1

 

10.  To display the factors of the given number

n = int(input("Enter a number: "))

print("Factors of", n, "are:")

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

    if n % i == 0:

        print(i)

 

11.  To display the factorial of a given number

n = int(input("Enter a number: "))

fact = 1

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

    fact *= i

print("Factorial =", fact)

 

12.  To display the following series

a)     2 4 8 16 32 ......upto 10th terms

n = 2

for i in range(10):

    print(n, end=" ")

    n = n * 2

 

b)     100 97 94 91 upto 10th terms

n = 100

for i in range(10):

    print(n, end=" ")

    n = n – 3

 

13.  To display the multiplication table of a given number.

n = int(input("Enter a number: "))

for i in range(1, 11):

    print(n, "x", i, "=", n * i)

 

14.  To display whether the given number is prime or composite.

n = int(input("Enter a number: "))

count = 0

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

    if n % i == 0:

        count += 1

if count == 2:

    print("The number is prime")

else:

    print("The number is composite")

 

15.  To display the given number in reverse order.

n = input("Enter a number: ")

rev = n[::-1]

print("Reverse number =", rev)

 

16.  To display the sum of individual digits of a given number

n = input("Enter a number: ")

s = 0

for i in n:

    s += int(i)

print("Sum of digits =", s)

 

17.  Write a program to input ten numbers from the user and store them in the list. The program should then display the greatest number among ten numbers.

L = []

for i in range(10):

    n = int(input("Enter number: "))

    L.append(n)

 

print("Greatest number =", max(L))

 

18.  Write a program to store five numbers in the list. The program should then display the product of all the numbers.

L = []

product = 1

for i in range(5):

    n = int(input("Enter number: "))

    L.append(n)

for x in L:

    product *= x

print("Product =", product)

 

19.  Write a program to input ten numbers from the user and store them in the list. The program should then count the even and odd numbers separately.

L = []

even = 0

odd = 0

for i in range(10):

    n = int(input("Enter number: "))

    L.append(n)

 

for x in L:

    if x % 2 == 0:

        even += 1

    else:

        odd += 1

print("Even numbers =", even)

print("Odd numbers =", odd)

 

20.  To display the longest word among three different words To count the number of consonants present in the given word To count the number of characters N/n present in the given word.

a = input("Enter first word: ")

b = input("Enter second word: ")

c = input("Enter third word: ")

if len(a) >= len(b) and len(a) >= len(c):

    print("Longest word is", a)

elif len(b) >= len(a) and len(b) >= len(c):

    print("Longest word is", b)

else:

    print("Longest word is", c)

 

21.  To count the number of consonants present in the given word.

w = input("Enter a word: ")

count = 0

for i in w.lower():

    if i.isalpha() and i not in "aeiou":

        count += 1

print("Number of consonants =", count)

 

22.  To count the number of characters N/n present in the given word.

w = input("Enter a word: ")

count = 0

for i in w:

    if i == 'N' or i == 'n':

        count += 1

print("Number of N/n characters =", count)