Tuesday, 23 May 2023
Monday, 27 April 2020
Chapter 1 Basic Python Questions
Practice
Questions
Part – 1
(Theory)
1. Who is the developer of
Python Programming Language?
2. Python is free and open
source language. here, what is free and open source?
3. What are the advantages of
Python Programming Language?
4. In how many different ways
we can work of Python?
5. Differentiate interactive
and script mode of Python programming language?
6. What are literals in Python?
How many types of literals are there in Python?
7. How string literal is
represented in Python?
8. What is a statement and
expression?
9. What is the role of
indention in Python?
10. What are variables?
11. Differentiate keyword and
identifier.
12. What are tokens in Python?
13.
How comments are given in Python program?
14.
How many parts are there for any complex number?
15. What is data type in python?
Is there any importance of data type?
16. Differentiate implicit type
conversion and explicit type conversion.
Part -2 (
Output and Errors)
1.
What will be the output of following? a=20
b=a+1
a=33
print(a+b)
2.
What is wrong with following code fragment? a=20
print(a)
b=33
print(b)
3.
What is wrong with following code fragment?
name="freya"
classs=4
print(name+classs)
4.
What will be the output of following python code?
a,b=3,4
a,b,c=b,a+3,b-1
print(a,b,c)
5.
What will be the output of following python code?
a,b=3,4
c,a=b*4,a+4
print(a,b,c)
6.
What will be the output of following python code/code
fragment? a. print(print("vishal"))
b. print("vishal")
print("indian")
c.
print("vishal",end=" ")
print("indian")
d. a=int(input("enter
first no"))
b=int(input("enter
second no"))
a,b=b,a
print("a=",a)
print("b=",b)
#if user is entering 5 and
then 10
7. Which of the following is an
invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
8. What is the error of
following code : a,b=100?
9.
What is the output of the following code : print(9//2)
10.
Can you identify the data types of following values,if yes write their
data tpe.
4, 5.2, 8j , ’1’ , ”1” ,
4+0j
11. What will be the output of
the following code
print(5/2)
print(5//2)
12.
What will the output of following codes print (8*2 -
2*4)
print (3 +
18/9 - 3**2+1) print ((4 + 24)/8 - (2**3+3))
13. What will the output of
following code snippet
x = 5
y = -1
print ((x+y)*1./x)
print (3*x )
print (x )
14.
Write the equivalent Boolean value a. 7+3*4 < 3 *3 +
4
15. What is the problem with
following code fragments
a. x="marks"+100
print(x)
b. a=5
b=a+500
a="marks"
c=a/2 print(c)
16.
What will be the output of following code fragments
a,b=5,6
a,b,c=10,b,a
a,b,c=c/2,a+4,b*6+2
print(a)
print(b)
print(c)
17.
What will be the output of following python statements
print(type(100))
print(type(100/2))
print(type('0'))
print(type(5.2))
18. Classify each of the
following as either a legal or illegal Python identifier:
|
1. fred
|
#Keywords
|
||
|
2.
|
#if
|
# Invalid
|
|
|
3.
|
#2x
|
#Invalid
|
|
|
4.
|
-4
|
#Constant
|
|
|
5. sum_total
|
#identifier
|
||
|
6. sumTotal
|
#identifier
|
||
|
7. sum-total
|
#Expression
|
||
|
8.
|
#sum total
|
#Invalid
|
|
|
9.
|
Sumtotal
|
#identifier
|
|
|
10.
|
While
|
#identifier
|
|
|
11. x2
|
#identifier
|
||
|
12. Private
|
#Keywords
|
||
|
13. public
|
#Keywords
|
||
|
14.
|
#$16
|
#Invalid
|
|
|
15. xTwo
|
#identifier
|
||
|
16.
|
_static
|
#identifier
|
|
|
17.
|
_4
|
#identifier
|
|
|
18.
|
___
|
#identifier
|
|
|
19.
|
#10%
|
#Invalid
|
|
|
20. a27834
|
#identifier
|
||
|
21.
|
#wilma’s
|
#Invalid
|
|
19.
If x = 2 Indicate what each of the following Python
statements would print. print(’x’)
print(‘x’)
print(x)
print(‘x+1’)
print(’x’ + 1) print(x + 1)
Part – 3
(Programs)
1. WAP to add 2 numbers.[R=a+b]
2. WAP to add 4
numbers.[R=a+b+c+d]
3. WAP to multiply 3
numbers.[R=a*b*c]
4. WAP to find average 5
numbers[R=(a+b+c+d+e)/5]
5. WAP to display age after 15
years.[nage =age+15]
6. WAP to display a 3 numbers
[R=a*a*a]
7. WAP to find the area of
square. [A=a*a]
8. WAP to find the area of
rectangle [A=a*b]
9. WAP to find the result X N
pow()
10. WAP to find the perimeter of
rectangle[A=2*(l+ b) ]
11. WAP to find the area of
circle [A=3.14*r*r]
12. WAP to find the
circumference of circle [C=2*3.14*r]
13. WAP to swap the values of
two variables.[a= a + b; b=a – b; a= a – b;]
14. WAP to input Hours, Minutes
and Seconds and display in seconds. [TS=H*60*60+M*60+S]
15. WAP to input cost and
display cost after increasing 25% [cost+(cost*25)/100]
16. WAP to display squire of a
numbers [a*a]
17. WAP to find the volume of
sphere.[v=4/3*3.14*r 3 ]
18. WAP to find the area of
triangle using HEROS formula[s= (a + b + c)/2, R=] math.sqrt()
19. WAP to take principal, rate
and time and display C.I. (Compound Interest) CI=p*(1+R/100) T pow()
20. WAP to calculate sum of 5
subjects & find percentage[TOT=s1+s2+s3+s4+s5,Per=TOT/5]
21. WAP which accept temperature
in Fahrenheit and print it in centigrade[c=5/9*(T-32)]
22. WAP which accept temperature
in centigrade and print it in Fahrenheit[F=(1.8*T)+32]
23. WAP to calculate simple
interest. [SI=(PRT)/100]
24. WAP to find gross salary [GS=BASIC+DA-PF+HRA]
25. Write a python program to
solve quadratic equation
26. Python Program to Convert
Kilometers to Miles.
Thursday, 9 April 2020
Monday, 6 April 2020
Wednesday, 5 February 2020
Sample Periodic Test
https://drive.google.com/file/d/1D9hx_WHugaGoM1A3-_MIZzFPkB3NB96i/view
https://drive.google.com/file/d/1NWPmGEEcsb3wjCO4ywCpHXr-7VVEpX4S/view
https://drive.google.com/file/d/1YUB3pc2YWikFEtYxuamBLQk_p8zIddQL/view
https://drive.google.com/file/d/1L0mGe_R74zCm0qgS61H1a-SKt9XA9B5g/view
https://drive.google.com/file/d/1LrdyAUuppzxTAZGFhFcuvV2ypa89f7TF/view
https://drive.google.com/file/d/1vkht-H0Q5PnrkBz1LrMNNLN-_92kVyFp/view
https://drive.google.com/file/d/1NWPmGEEcsb3wjCO4ywCpHXr-7VVEpX4S/view
https://drive.google.com/file/d/1YUB3pc2YWikFEtYxuamBLQk_p8zIddQL/view
https://drive.google.com/file/d/1L0mGe_R74zCm0qgS61H1a-SKt9XA9B5g/view
https://drive.google.com/file/d/1LrdyAUuppzxTAZGFhFcuvV2ypa89f7TF/view
https://drive.google.com/file/d/1vkht-H0Q5PnrkBz1LrMNNLN-_92kVyFp/view
Wednesday, 6 November 2019
Python Programs 3
|
1)
|
Write a program to accepts two integers and print their sum.
|
||||||||||
|
Write a program that accepts radius of a circle and prints its area.
|
|||||||||||
|
3)
|
Write a program that accepts base and height and calculate the area of
triangle.
|
||||||||||
|
4)
|
Write a program that inputs a student’s marks in three subjects (out
of 100) and prints the percentage marks.
|
||||||||||
|
5)
|
Write a program to compute area of square andtriangle.
|
||||||||||
|
6)
|
Write a program to calculate simple interest.
|
||||||||||
|
7)
|
Write a program to read two numbers and prints theirquotient and
reminder.
|
||||||||||
|
8)
|
Write a program to find whether a given number iseven or odd.
|
||||||||||
|
9)
|
Write a program to find largest among three integers.
|
||||||||||
|
10)
|
Write a program to find lowest among three integers.
|
||||||||||
|
11)
|
Write a program that accepts length and breadth of rectangle and
calculate its area.
|
||||||||||
|
12)
|
Write a program that accepts weight in Kg and height in meters and
calculate the BMI.
|
||||||||||
|
Write a program that reads the number n and print the value of n², n³ and n⁴.
|
|||||||||||
|
14)
|
Write a
program to accept the marks of five subjects and calculate the average marks.
|
||||||||||
|
15)
|
Write a program to accept theheight in cm and convert it into feet and
inches.
|
||||||||||
|
16)
|
Write a program that accepts the age and print if one is eligible to
vote or not.
|
||||||||||
|
17)
|
Write a program that accepts two numbers and check if the first number
is fully divisible by second number or not.
|
||||||||||
|
18)
|
Write a program to read base,width and height of parallelogram and
calculate its area and perimeter.
|
||||||||||
|
19)
|
Write a program to accept the year and check if it is a leap year or
not.
|
||||||||||
|
Write a program to obtain x, y, z and calculate 4x⁴+3y³+9z+6π.
|
|||||||||||
|
21)
|
Write a program to input a number and print its square if it is odd,
otherwise print its square root.
|
||||||||||
|
22)
|
Write a program to input a number and check whether it is positive,
negative or zero.
|
||||||||||
|
23)
|
Write a program to input percentage marks of a student and find the
grade as per following criterion:
Marks Grade
>=90 A
75-90 B
60-75 C
Below 60 D
|
||||||||||
|
24)
|
Write a program to enter a number and check if it is a prime number or
not.
|
||||||||||
|
25)
|
Write a program to display a menu for calculating area of circle or
perimeter of the circle.
|
||||||||||
|
26)
|
Write a program that reads two numbers and an arithmetic operator and
displays the computed result.
|
||||||||||
|
27)
|
Write a program to print whether a given character is an uppercase or
a lowercase character or a digit or any other character.
|
||||||||||
|
28)
|
Write a program to calculate and print the roots of a quadratic
equation ax²+bx+c=0.(a≠0)
|
||||||||||
|
29)
|
Write a program to print sum of natural numbers between 1 to 7. Print
the sum progressively i.e. after adding each natural number, print sum so
far.
|
||||||||||
|
30)
|
Write a program to calculate the factorial of a number.
|
||||||||||
|
31)
|
Write a program to create a triangle of stars using nested loop.
|
||||||||||
|
32)
|
Write a Python script to print Fibonacci series’ first 20 elements.
|
||||||||||
|
33)
|
Write a program to read an integer>1000 and reverse the number.
|
||||||||||
|
34)
|
Input three angles and determine if they form a triangle or not.
|
||||||||||
|
35)
|
Write a Python script that displays first ten Mersenne numbers.
|
||||||||||
|
36)
|
Write a Python script that displays first ten Mersenne numbers and
displays ‘Prime’ next to Mersenne Prime Numbers.
|
||||||||||
|
37)
|
Write a program to calculate BMI and
print the nutritional status as per following table:
|
||||||||||
|
38)
|
Write python script to print following pattern.
1
1 3
1 3 5
1 3 5 7
|
||||||||||
|
39)
|
Write a program to find sum of series :
s=1+x+x ²+x ³+x ⁴…+x ⁿ
|
||||||||||
|
40)
|
Write a python script to input two numbers and print their lcm and
hcf.
|
||||||||||
|
41)
|
Write a python script to calculate the sum of the following series:
S=(1)+(1+2)+(1+2+3)+……+(1+2+3+….+n)
|
||||||||||
|
42)
|
Write a program to print the following using a single loop (no nested
loops)
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
|
||||||||||
|
43)
|
Write a program to print a pattern like:
4 3 2 1
4 3 2
4 3
4
|
||||||||||
|
44)
|
44.Program that reads a line and print its statistics like:
Number of uppercase letters:
Number of lowercase letters:
Number of alphabets:
Number of digits:
|
||||||||||
|
45)
|
Write a program that reads a line and a substring and displays the
number of occurrences of the given substring in the line.
|
||||||||||
|
46)
|
Write a program that takes a string with multiple words and then
capitalizes the first letter of each word and forms a new string out of it.
|
||||||||||
|
47)
|
Write a program that reads a string and checks whether it is a
palindrome string or not.
|
||||||||||
|
48)
|
Write a program that reads a string and displays the longest substring
of the given string having just the consonants.
|
||||||||||
|
49)
|
Write a program that reads a string and then prints a string that
capitalizes every other letter in the string.
|
||||||||||
|
50)
|
Write a program that reads the email id of a person in the form of a
string and ensures that it belongs to domain @edupillar.com (Assumption: no
invalid characters are there in email-id)
|
1.Write a program to accepts two integers and print their
sum.
a=int(input('Enter the first integer:'))
b=int(input('Enter the
second integer:'))
Sum=a+b
print('The two integers are:', a, b)
print('The sum of two integers are:', Sum)
OUTPUT:-
2.Write a program that accepts radius of a circle and prints
its area.
r=int(input('Enter the
radius of circle:'))
Area=3.14*r**2
print('The area of the circle is:', Area)
3. Write a program that accepts base and height and calculate
the area of triangle.
b=float(input('Enter the base
of triangle:'))
h=float(input('Enter the
height of triangle:'))
Area=(1/2)*b*h
print('The area of triangle is:', Area)
4. Write a program that inputs a student’s marks in three
subjects (out of 100) and prints the percentage marks.
print('Enter the marks of three subject out of 100')
a=float(input('Enter the marks
of first subject:'))
b=float(input('Enter the marks
of second subject:'))
c=float(input('Enter the marks
of third subject:'))
P=(a+b+c)/3
print('The percentage marks are:', P,'%')
5. Write a program to compute area of square and triangle.
a=float(input('Enter the value
of side:'))
A=a**2
T=((3**0.5)/4)*a**2
print('The area of square is:', A)
print('The area of triangle is:', T)
6. Write a program to calculate simple interest.
P=float(input('Enter the
principal amount in ₹:'))
R=float(input('Enter the rate
of interest:'))
T=float(input('Enter the time
in years:'))
SI=(P*R*T)/100
print('The simple interest is:', SI,'₹')
7. Write a program to read two numbers and prints their
quotient and reminder.
a=float(input('Enter the dividend:'))
b=float(input('Enter the divisor:'))
Q=a//b
R=a%b
print('The quotient is:', Q)
print('The remainder is:', R)
OUTPUT:-
8. Write a program to find whether a given number is even or
odd.
a=int(input('Enter the number:'))
if
a%2==0:
print('The number is even')
else:
print('The number is odd')
OUTPUT:-
9. Write a program to find largest among three integers.
a=int(input('Enter the first integer:'))
b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if
a>b and a>c:
print(a, 'is the largest integer')
if
b>a and b>c:
print(b, 'is the largest
integer')
if
c>a and c>b:
print(c, 'is the largest
integer')
10.Write a program to find lowest among three integer.
a=int(input('Enter the first
integer:'))
b=int(input('Enter the
second integer:'))
c=int(input('Enter the third
integer:'))
ifa<b and a<c:
print(a,
'is the smallest integer')
ifb<a and b<c:
print(b,
'is the smallest integer')
ifc<a and c<b:
print(c,
'is the smallest integer')
11. Write a program to that accepts length and breadth of
rectangle and calculate its area.
l=float(input('Enter the
length of rectangle:'))
b=float(input('Enter the
breadth of rectangle:'))
area=l*b
print('Rectangle
Specifications')
print('Length=',l)
print('Breadth=', b)
print('Area=', area)
12.Write a program that accepts weight in Kg and height in
meters and calculate the BMI.
W = float(input('Enter the
weight in Kg:'))
H = float(input('Enter height in
meters:'))
BMI=W/(H**2)
print('BMI is:', BMI)
OUTPUT:-
13.Write a program that reads the number n and print the
value of n², n³ and n⁴.
a=float(input('Enter the value
of n:'))
b=a**2
c=a**3
d=a**4
print('The value of n² is:', b)
print('The value of n³ is:', c)
print('The value of n⁴ is:', d)
OUTPUT:-
14.Write a program to accept the marks of five subjects and
calculate the average marks.
a=float(input('Enter the marks
of first subject:'))
b=float(input('Enter the marks
of second subject:'))
c=float(input('Enter the marks
of third subject:'))
d=float(input('Enter the marks
of fourth subject:'))
e=float(input('Enter the marks
of fifth subject:'))
Average=(a+b+c+d+e)/5
print('The average marks are:', Average)
15.Write a program to accept the height in cm and convert it
into feet and inches.
a=float(input('Enter your
height in centimeters:'))
Feet=a*0.032
Inch=a*0.393
print('Your height in feet is:', Feet)
print('Your height in inch is:', Inch)
16.Write a program that accepts the age and print if one is
eligible to vote or not.
a=int(input('Enter your
age:'))
if a>=18:
print('You are eligible to vote')
else:
print('You are not eligible to vote')
17.Write a program that accepts two numbers and check if the
first number is fully divisible by second number or not.
a=float(input('Enter the first
number:'))
b=float(input('Enter the
second number:'))
if a%b==0:
print('The first number is fully divisible by second number')
else:
print('The first number is not fully divisible by second
number')
18.Write a program to read base, width and height of
parallelogram and calculate its area and perimeter.
b=float(input('Enter the base of parallelogram:'))
w=float(input('Enter the width of parallelogram:'))
h=float(input('Enter the height of parallelogram:'))
Area=b*h
Perimeter=2*(b+w)
print('The area of
parallelogram is:', Area)
print('The perimeter of
parallelogram is:', Perimeter)
19.Write a program to accept the year and
check if it is a leap year or not.
a=int(input('Enter the
year:'))
if a%4==0:
print('This year is a leap year')
else:
print('This year is not a leap year')
20.Write a program to obtain x, y, z and calculate
4x⁴+3y³+9z+6π.
print('To calculate
4x⁴+3y³+9z+6π')
x=float(input('Enter the
number x:'))
y=float(input('Enter the
number y:'))
z=float(input('Enter the
number z:'))
import math
b=(4*math.pow(x,4))+(3*math.pow(y,3))+(9*z)+(6*math.pi)
print('The result of the above
expression is:',b)
21.Write
a program to input a number and print its square if it is odd, otherwise print
its square root.
x=float(input(‘Enter the
number:’))
import math
a=math.pow(x,2)
b=math.sqrt(x)
if x%2!=0:
print('The value of square is:',a)
else:
print('The value of square root is:',b)
22.Write a program to input a number and check whether it is
positive, negative or zero.
a=float(input('Enter the
number:'))
if a>=0:
if
a==0:
print('The number is zero')
else:
print('The number is a positive number')
else:
print('The number is a negative number')
23.Write a program to input percentage marks of a student and
find the grade as per following criterion:
Marks Grade
>=90 A
75-90 B
60-75 C
Below
60 D
a=float(input('Enter the
percentage marks:'))
if a>=90:
print('The student has got an A grade')
elif a>=75 and a<90:
print('The student has got a B grade')
elif a>=60 and a<75:
print('The student has got a C grade')
else:
print('The student has got a D grade')
24. Write a program to enter a number and check if it is a
prime number or not.
num=int(input('Enter the
number:'))
for i in range(2,num//2+1):
if num%i==0:
print('It is not a prime no.')
break
else:
print('It is a prime number')
25. Write a program to
display a menu for calculating area of circle or perimeter of the circle.
r=float(input(‘Enter the
radius of the circle:’)
print(‘1.Calculate perimeter’)
print(‘2.Calculate area’)
choice=int(input(‘Enter your
choice (1 or 2):’))
if
choice==1:
peri=2*3.14159*r
print(‘Perimeter of
the circle with radius’,r,’:’,peri)
else:
area=3.14159*r*r
print(‘Area of the circle of the radius’,r,’:’,area)
26.Write a program that reads two numbers and an arithmetic
operator and displays the computed result.
a=float(input('Enter the first
number:'))
b=float(input('Enter the
second number:'))
c=input('Enter the operator[/,*,+,-]:')
if
c=='/':
r=a/b
elif
c=='*':
r=a*b
elif
c=='+':
r=a+b
elif
c=='-':
r=a-b
else:
print('Invalid operator')
print(a,c,b,'=',r)
27.Write a program to print
whether a given character is an uppercase or a lowercase character or a digit
or any other character.
ch=input('Enter a single character:')
if ch>='A' and ch<='Z':
print('You have entered an uppercase character.')
elif ch>='a' and ch<='z':
print('You have entered an lowercase character.')
elif ch>='0' and ch<='9':
print('You have entered a digit.')
else:
print('You have entered a special character.')
28.Write a program to
calculate and print the roots of a quadratic equation ax²+bx+c=0.(a≠0)
import
math
print('For quadratic equation, ax²+bx+c=0,enter coefficents
below')
a=int(input('Enter a:'))
b=int(input('Enter b:'))
c=int(input('Enter c:'))
if
a==0:
print('Value of a should not be zero')
print('Aborting!!')
else:
d=b*b-4*a*c
if
d>0:
root1=(-b+math.sqrt(d))/(2*a)
root2=(-b-math.sqrt(d))/(2*a)
print('Roots are real and unequal')
print('Root1=',root1,',Root2=',root2)
elif
d==0:
root1=-b/2*a
print('Roots are real and equal')
print('Root1=', root1,',Root2=',root1)
else:
print('Roots are complex and imaginary')
29. Write a program to print sum of natural numbers
between 1 to 7. Print the sum progressively i.e. after adding each natural
number, print sum so far.
Sum=0
for n in range(1,8):
Sum+=n
print('Sum of natural numbers <=',n,'is',Sum)
30.Write a program to
calculate the factorial of a number.
num=int(input('Enter a
number:'))
fact=1
a=1
while a<=num:
fact*=a
a+=1
print('The factorial of',num,'is',fact)
31. Write a program to
create a triangle of stars using nested loop.
for i in range(1,6):
print()
for j in range(1,i):
print('*',end=' ')
32.Write a Python script to
print Fibonacci series’ first 20 elements.
first=0
second=1
print(first, end=' ')
print(second,end=' ')
for a in range(1,19):
third=first+second
print(third,end=' ')
first,second=second,third
33.Write a program to read
an integer>1000 and reverse the number.
num=int(input('Enter a number
(>1000):'))
tnum=num
reverse=0
while tnum>0:
digit=tnum%10
reverse=reverse*10+digit
tnum=tnum//10
print('Reverse of',num,'is',reverse)
OUTPUT:-
34.Input three angles and
determine if they form a triangle or not.
angle1=angle2=angle3=0
angle1=float(input('Enter the first angle:'))
angle2=float(input('Enter the second angle:'))
angle3=float(input('Enter the third angle:'))
if angle1+angle2+angle3==180:
print('The angles form a triangle')
else:
print('The angles do not form a triangle')
35.Write a Python script
that displays first ten Mersenne numbers.
print('First 10 Mersenne numbers are:')
for a in range(1,11):
mersnum=2**a-1
print(mersnum,end=' ')
print()
36. Write a Python script that displays first ten
Mersenne numbers and displays ‘Prime’ next to Mersenne Prime Numbers.
for a in range(1,21):
mersnum=2**a-1
mid=mersnum//2+1
for b in range(2,mid):
if
mersnum%b==0:
print(mersnum)
break
else:
print(mersnum,'\tPrime')
37.Write a program to calculate
BMI and print the nutritional status as per following table:
Nutritional
Status
|
WHO
criteria BMI cut-off
|
Underweight
|
<18.5
|
Normal
|
18.5-24.9
|
Overweight
|
25-29.9
|
Obese
|
≥30
|
w=float(input('Enter the weight
in kgs:'))
h=float(input('Enter the height
in meters:'))
BMI=w/h**2
print('BMI is',BMI,end='')
if BMI<18.5:
print('...Underweight')
elif BMI>=18.5 and BMI<24.9:
print('...Normal')
elif BMI>=25 and BMI<29.9:
print('...Overweight')
else:
print('...Obese')
38.Write python script to
print following pattern.
1
1
3
1 3 5
1 3 5 7
for a in range(3,10,2):
print()
for b in range(1,a,2):
print(b,
end=' ')
print()
39.Write a program to find
sum of series :
s=1+x+x ²+x ³+x ⁴…+x ⁿ
x=float(input('Enter the value
of x:'))
n=int(input('Enter the value
of n (for x**n):'))
s=0
for a in range(n+1):
s+=x**a
print('Sum of first' ,n,'terms:',s)
40.Write a python script to
input two numbers and print their lcm and hcf.
x=int(input('Enter the first
number:'))
y=int(input('Enter the
second number:'))
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if
x%i==0 and y%i==0:
hcf=i
lcm=(x*y)/hcf
print('The HCF of',x,'and',y,'is:',hcf)
print('The LCM of',x,'and',y,'is:',lcm)
41.Write a python script to
calculate the sum of the following series:
S=(1)+(1+2)+(1+2+3)+……+(1+2+3+….+n)
Sum=0
n=int(input('How many
terms?'))
for a in range(2,n+2):
term=0
for b in range(1,a):
term+=b
print('Term',(a-1),':',term)
Sum+=term
print('Sum of',n,'terms is:',Sum)
42.Write a program to print the following using a single loop
(no nested loops)
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
n=1
for a in range(5):
print(n)
print()
n=n*10+1
43.Write a program to print a pattern like:
4 3 2 1
4 3 2
4 3
4
for i in range(4):
for j in range(4,i,-1):
print(j,end=' ')
else:
print()
44.Program that reads a line and print its statistics like:
Number
of uppercase letters:
Number
of lowercase letters:
Number
of alphabets:
Number
of digits:
line=input('Enter a line:')
lowercount=uppercount=0
digitcount=alphacount=0
for a
in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if
a.isalpha():
alphacount+=1
print('Number of uppercase letters are:',uppercount)
print('Number of lowercase letters are:',lowercount)
print('Number of alphabets are:',alphacount)
print('Number of digits are:',digitcount)
OUTPUT:-
45.Write a
program that reads a line and a substring and displays the number of
occurrences of the given substring in the line.
line=input('Enter line:')
sub=input('Enter
substring:')
length=len(line)
lensub=len(sub)
start=count=0
end=length
while
True:
pos=line.find(sub,start,end)
if pos!=-1:
count+=1
start=pos+lensub
else:
break
if start>=length:
break
print('No. of occurences of',sub,':',count)
46.Write a program that takes a string with multiple words
and then capitalizes the first letter of each word and forms a new string out
of it.
string=input('Enter a
string:')
length=len(string)
a=0
end=length
string2=''
while
a<length:
if a==0:
string2+=string[0].upper()
a+=1
elif (string[a]==' ' and string[a+1]!='
'):
string2+=string[a]
string2+=string[a+1].upper()
a+=2
elif
(string[a]==',' and string[a+1]!=','):
string2+=string[a]
string2+=string[a+1].upper()
a+=2
else:
string2+=string[a]
a+=1
print('Original string:',string)
print('Capitalized words string:',string2)
OUTPUT:-
47.Write a
program that reads a string and checks whether it is a palindrome string or
not.
string=input('Enter a
string:')
length=len(string)
mid=length//2
rev=-1
for a in range(mid):
if string[a]==string[rev]:
print(string,'is a palindrome.')
break
else:
print(string,'is not a palindrome.')
break
48.Write a
program that reads a string and displays the longest substring of the given
string having just the consonants.
string=input('Enter a
string:')
length=len(string)
maxlength=0
maxsub=''
sub=''
lensub=0
for
a in range(length):
if string[a]
in 'aeiou' or string[a] in 'AEIOU':
if lensub>maxlength:
maxsub=sub
maxlength=lensub
sub=''
lensub=0
else:
sub+=string[a]
lensub=len(sub)
a+=1
print('Maximum length consonant substring is:',maxsub,end=' ')
print('with',maxlength,'characters')
49.Write a
program that reads a string and then prints a string that capitalizes every
other letter in the string.
string=input('Enter a
string:')
length=len(string)
print('Original string:',string)
string2=''
for
a in range(0,length,2):
string2+=string[a]
if a<length-1:
string2+=string[a+1].upper()
print('Alternatively capitalized string:',string2)
50.Write a
program that reads the email id of a person in the form of a string and ensures
that it belongs to domain @edupillar.com (Assumption: no invalid characters are
there in email-id)
email=input('Enter your
email id:')
domain='@edupillar.com'
ledo=len(domain)
#ledo=length of domain
lema=len(email)
#lema=length of email
sub=email[lema-ledo:]
if
sub==domain:
if ledo!=lema:
print('It is valid email id')
else:
print('This is invalid email id- contains just the domain name')
else:
print('This email-d is either not valid or belongs to some
other domain')
Subscribe to:
Posts (Atom)