(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)

 OR

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

reversed_num = 0

 

while num > 0:

    digit = num % 10       

    reversed_num = reversed_num * 10 + digit 

    num = num // 10        

print("Reversed:", reversed_num)

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 the word: ")

c = 0

u = w.upper()

for i in u:

    if i!='A' and i!='E' and i!='I' and i!='O' and i!='U':

        c = c + 1

print("Number of consonants =", c)


 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)

 

4. Write output of the following. Show in the dry run table.

                 i.           

n=0

s=0

for n in range(11):

if n%2!=0:

s=s+n

print(s)

n

s

Output

0

0

0

0

1

1

2

1

3

4

4

4

5

9

6

9

7

16

8

16

9

25

10

25

25

 

               ii.           

n=8

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

if n%i==0:

print(i)

n

i

Output

8

1

1

8

2

2

8

3

8

4

4

8

5

8

6

8

7

8

8

8

 

 

             iii.           

n=783

s=0

while n!=0:

r=n%10

s=s*10+r

n=n//10

print(f"The result is {s}")

n

r

s

Output

783

0

783

3

3

78

8

38

7

7

387

0

387

The result is 387

 

             iv.           

p=1

n=5

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

p=p*i

print(f"The result is {p}")

 

p

n

i

Output

1

5

1

5

1

2

5

2

6

5

3

24

5

4

120

5

5

The result is 120

 

               v.           

n=7

for i in range(1,6):

print(n)

if n%2==0:

n=n/2

else:

n=n*3+1

n

i

Output

7

1

7

22

2

22

11.0

3

11.0

34.0

4

34.0

17.0

5

17.0

 

             vi.           

x="Python"

y="Programming"

a=len(x)

b=len(y)

if a>b:

print(x)

else:

print(y)

X

y

a

b

Output

Python

Programming

6

11

Programming

 

           vii.           

z="python"

t=""

for i in z:

t=i+t

print(t)

i

z

t

Output

python

""

p

python

p

y

python

yp

t

python

typ

h

python

htyp

o

python

ohtyp

n

python

nohtyp

nohtyp