• Home
  • About
  • Services
  • Portfolio
  • Quiz
  • Charts
  • Contact

1 - Print "Hello World"

In [ ]:
print("Hello World!")
Hello World!

2 - Input your name and print Welcome along with name

In [ ]:
a = input("Enter your name ")
print("Welcome", a)
Enter your name Ishita
Welcome Ishita

3 - Input a number and print it.

In [ ]:
r = input("Enter the number ")
print("The number is", r)
Enter the number6
The number is 6

4 - Input two numbers and print the greater one.

In [ ]:
a = int(input("Enter number one"))
b = int(input("Enter number two"))
if a > b:
  print("Number one is greater")
else:
  print("Number two is greater")
Enter number one9
Enter number two7
Number one is greater

5 - Write a program to display grade of a student based on marks.

In [ ]:
s = int(input("Marks obtained by student"))
if s >= 90 and s <= 100:
  print("Grade A")
elif s >= 75 and s <= 89:
  print("Grade B")
elif s >= 50 and s <= 74:
  print("Grade C")
else:
  print("Grade D")
Marks obtained by student78
Grade B

6 - Input three numbers and print the greatest and smallest among them

In [ ]:
a = int(input("Enter number 1"))
b = int(input("Enter number 2"))
c = int(input("Enter number 3"))
if a > b and a > c:
  print("1st number is greatest")
elif b > a and b > c:
  print("2nd number is greatest")
else:
  print("3rd number is greatest")
if a < b and a < c:
  print("1st number is smallest");
elif b < a and b < c:
  print("2nd number is smallest");
else:
  print("3rd number is smallest");
Enter number 1898
Enter number 2500
Enter number 3378
1st number is greatest
3rd number is smallest

7 - Input distance, time and print speed.

In [ ]:
dis = int(input("Enter the distance in meter"))
tm = int(input("Enter the time taken to cover the distance in seconds"))
print("Speed is", dis/tm, "m/s")
Enter the distance in meter6895
Enter the time taken to cover the distance in seconds1000
Speed is 6.895 m/s

8 - Enter the initial speed, time taken and acceleration and print the displacement

In [ ]:
u = int(input("Enter the initial speed in m/s"))
t = int(input("Enter the time taken in seconds"))
a = int(input("Enter the acceleration m/s^2"))
print("Displacement is", u*t + (a*t**2)/2)
Enter the initial speed in m/s5
Enter the time taken in seconds10
Enter the acceleration m/s^22
Displacement is 150.0

9 - Input principle amount, interest rate, numbers of times interest is compounded per year and time period and print the total amound.

In [ ]:
p = float(input("Enter the principal amount"))
r = float(input("Enter the interest rate"))
n = float(input("Enter the number of times interest is compounded per year"))
t = float(input("Enter the time period"))
a = p * (1 + r/n)**(n * t)
print("The compound interest is", a)
Enter the principal amount700
Enter the interest rate2
Enter the number of times interest is compounded per year2
Enter the time period2
The compound interest is 11200.0

10 - Input length and breadth of a rectangle and print its area.

In [ ]:
l = float(input("Enter the length"))
b = float(input("Enter the bredth"))
print("The area of Rectangle is", l * b)
Enter the length78
Enter the bredth10
The area of Rectangle is 780.0

11 - Input radius of a circle and print its area.

In [ ]:
r = float(input("Enter the radius"))
print("Area of the circle is", 22 / 7 * r**2)
Enter the radius7
Area of the circle is 154.0

12 - Input name of student and his/her marks of all subject and print percentage.

In [ ]:
nm = input("Enter the name of student")
a = float(input("Enter marks in Maths"))
b = float(input("Enter marks in English"))
c = float(input("Enter marks in Hindi"))
d = float(input("Enter marks in Social Science"))
e = float(input("Enter marks in Sanskrit"))
total = float(input("Enter Total marks"))
print("Percentage of", nm, "is", (a + b + c + d + e)/total * 100)
Enter the name of studentDeepika
Enter marks in Maths49
Enter marks in English78
Enter marks in Hindi76
Enter marks in Social Science70
Enter marks in Sanskrit79
Enter Total marks480
Percentage of Deepika is 73.33333333333333

13 - Input distance in feet and convert it into inches.

In [ ]:
k = float(input("Enter the distance covered in feet"))
print(12 * k, "Inches")
Enter the distance covered in feet56
672.0 Inches

14 - Write A program to entre value of tempreture in Fahrenheit and convert it into Celcius.

In [ ]:
k = float(input("Templeture in Fahrenheit"))
print("Tempreture in degree celcius will be", 5 / 9 * (k - 32))
Templeture in Fahrenheit72
Tempreture in degree celcius will be 22.22222222222222

15 - Write A Program to enter radius and height of cylinder and calculate volume of cylinder

In [ ]:
r = float(input("Enter the radius of cylinder"))
h = float(input("Enter the height of cylinder"))
print("Volume of cylinder is", 22/7 * r**2 * h)
Enter the radius of cylinder5
Enter the height of cylinder7
Volume of cylinder is 550.0

16 - Make a program to calculate a given arithmetic operations.

In [ ]:
s = input("The operation you want to do")
x = float(input("First number"))
y = float(input("Second number"))
if s == "+":
  print("The answer of operation is", x + y)
elif s == "-":
  print("The answer of operation is", x - y)
elif s == "*":
  print("The answer of operation is", x * y)
elif s == "/":
  print("The answer of operation is", x / y)
elif s == "%":
  print("The answer of operation is", x % y)
elif s == "//":
  print("The answer of operation is", x // y)
else:
  print("The answer of operation is", x**y)
The operation you want to do+
First number60
Second number40
The answer of operation is 100.0

17 - Write a program to perform arithmetic operation until asked to stop.

In [ ]:
while True:
  s = input("The operation you want to do")
  x = float(input("First number"))
  y = float(input("Second number"))
  if s == "+":
    print("The answer of operation is", x + y)
  elif s == "-":
    print("The answer of operation is", x - y)
  elif s == "*":
    print("The answer of operation is", x * y)
  elif s == "/":
    print("The answer of operation is", x / y)
  elif s == "%":
    print("The answer of operation is", x % y)
  elif s == "//":
    print("The answer of operation is", x // y)
  else:
    print("The answer of operation is", x**y)

  k = input("Do you want to continue Y/N")
  if k == "N" or s == "No":
    print("ok")
    break;
The operation you want to do*
First number4 
Second number5
The answer of operation is 20.0
Do you want to continue Y/NY
The operation you want to do+
First number40
Second number60
The answer of operation is 100.0
Do you want to continue Y/NY
The operation you want to do-
First number10
Second number5
The answer of operation is 5.0
Do you want to continue Y/NN
ok

18 - Write a program to print sum of all numbers upto n.

In [ ]:
n = float(input("Enter the number upto which sum is to be calculated"))
s = 0
while True:
  s += n
  n -= 1
  if n == 0:
    break;
print("The sum of numbers from 1 to n is", s)
Enter the number upto which sum is to be calculated5
The sum of numbers from 1 to n is 15.0

19 - Write a program to output multiplication table of n.

In [ ]:
n = int(input("Enter the number whose multiplication table is to be printed"))
k = 1;
while True:
  print(n, 'X', k, '=', n * k)
  k += 1
  if k == 11:
    break
Enter the number whose multiplication table is to be printed5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

20 - Print numbers from (-8, -2)

In [ ]:
for i in range(-8, -2):
  print(i)
-8
-7
-6
-5
-4
-3

21 - Print number divisible by 7 from 1 to 100

In [ ]:
for i in range(1, 101):
  if i % 7 == 0:
    print(i)
7
14
21
28
35
42
49
56
63
70
77
84
91
98

22 - Write a program to enter a number and print reverse of the number.

In [ ]:
a = int(input("Enter the number"))
b = 0
while a > 0:
  b = b * 10 + a % 10
  a //= 10
print(b)
Enter the number7897
7987

23 - Write a program to enter a number and a digit and count how many times digit is in the number.

In [ ]:
a = int(input("Enter the number"))
b = int(input("Enter the digit"))
cnt = 0
while a > 0:
  if a % 10 == b:
    cnt += 1
  a //= 10;
print("The frequency of", b, "digit in the number is", cnt, "times")
Enter the number578595
Enter the digit5
The frequency of 5 digit in the number is 3 times

24 - Write a program to calculate sum of first n numbers using while loop.

In [ ]:
n = int(input("Enter the number upto which sum is to be calculated"))
s = 0
while n > 0:
  s += n
  n -= 1
print("The sum of numbers from 1 to n is", s)
Enter the number upto which sum is to be calculated10
The sum of numbers from 1 to n is 55

25 - Write a program to input a number and find weather it is prime or not.

In [ ]:
a = int(input("Enter the number"))
for i in range(2, a):
  if a % i == 0:
    print("The number is not prime")
    break
else:
  print("The number is prime")
Enter the number7
The number is prime

26 - Write a program to print factorial of a number

In [ ]:
n = int(input("Enter the number"))
p = 1
while n > 0:
  p *= n;
  n -= 1
print("The factorial of number is", p)
Enter the number5
The factorial of number is 120

OR

In [ ]:
n = int(input("Enter the number"))
f = 1
for i in range(1, n+1):
  f *= i
print("The factorial of", n, "is", f)
Enter the number4
The factorial of 4 is 24

27 - Write a program to compute the greatest common divisor and least common multiple of two integers

In [ ]:
a = int(input("Enter the number"))
b = int(input("Enter the number"))
for i in range(1, a):
  if a % i == 0 and b % i == 0:
    g = i
print("The greatest commond divisor of", a, "and", b, "is", g)
print("The least common divisor of", a, "and", b, "is", (a * b)//g)
Enter the number5 
Enter the number6
The greatest commond divisor of 5 and 6 is 1
The least common divisor of 5 and 6 is 30

28 - Write a program to print fibonacci sequence upto n terms.

In [ ]:
n = int(input("Enter the number of terms"))
a = 0
b = 1
print(a)
print(b)
for i in range(2, n):
  a, b = b, a + b
  print(b)
Enter the number of terms8
0
1
1
2
3
5
8
13

29 - Write a program to print product of all numbers that user inputs.

In [ ]:
p = 1
while True:
  n = int(input("Enter the number"))
  p *= n
  s = input("Do you want to continue Y/N")
  if s == "N" or s == "No":
    break
print("The product of numbers is", p)
Enter the number5
Do you want to continue Y/NY
Enter the number4
Do you want to continue Y/NY
Enter the number3
Do you want to continue Y/NN
The product of numbers is 60

30 - Write a program to input lower limit and upper limit and find sum of even numbers between them as well as odd separately

In [ ]:
l = int(input("Enter the lower limit"))
u = int(input("Enter the upper limit"))
a = 0
b = 0
for i in range(l, u+1):
  if i % 2 == 0:
    a += i
  else:
    b += i
print("The sum of even numbers is", a)
print("The sum of odd numbers is", b)
Enter the lower limit4
Enter the upper limit12
The sum of even numbers is 40
The sum of odd numbers is 32

31 - Write a program to print numbers from 0 to 10 in step of 2

In [ ]:
for i in range(0,10,2):
  print(i)
0
2
4
6
8

32 - Write a program to print the number pattern.

In [ ]:
for i in range(3):
  for j in range(3):
    print(i, j," ",end='')
  print()
0 0  0 1  0 2  
1 0  1 1  1 2  
2 0  2 1  2 2  

33 - Write a program to print number from 1 to 9 in a matrix.

In [ ]:
a = 1
for i in range(3):
  for j in range(3):
    print(a, " ", end='')
    a += 1;
  print()
1  2  3  
4  5  6  
7  8  9  

34 - Write a program to print number in triangle pattern.

In [ ]:
for i in range(6):
  for j in range(i):
    print(i, " ", end="")
  print()
1  
2  2  
3  3  3  
4  4  4  4  
5  5  5  5  5  

35 - Write a program to print a star in triangle and inverted triangle pattern .

In [ ]:
for i in range(6):
  for j in range(i):
    print("*", " ", end='')
  print()
*  
*  *  
*  *  *  
*  *  *  *  
*  *  *  *  *  

36 - Write a program to print a star in inverted triangle pattern .

In [ ]:
for i in range(5):
  for j in range(5 - i):
    print("*", " ", end='')
  print()
*  *  *  *  *  
*  *  *  *  
*  *  *  
*  *  
*  

37 - Write a program to print numbers in triangle pattern

In [ ]:
for i in range(1, 6):
  for j in range(1, 7-i):
    print(j, "", end='')
  print()
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Programs on Strings¶

38 - Write a prgram to check weather a string is palindrome or not.

In [ ]:
st = input("Enter the string ")
if st == st[::-1]:
  print("It is a palindrome")
else:
  print("It is not a palindrome")
Enter the string MadaM
It is a palindrome

39 - Write a prgram to check weather a string is palindrome or not without using slicing.

In [ ]:
rev = ""
st = input("Enter the string")
tp = st
l = len(st)
while l > 0:
  rev = rev + tp[-1]
  tp = tp[:-1]
  l = len(tp)
if rev == st:
  print("It is a palindrome")
else:
  print("It is not a palindrome")
Enter the stringAbcdcbA
It is a palindrome

OR

In [ ]:
s = input("Enter the string")
n = len(s)
for i in range(0, n//2):
  if s[i] != s[n-i-1]:
    print("Not a palindrome")
    break;
else:
  print("It is a palindrome")
Enter the stringDeepika
Not a palindrome

40 - Write a program to input a string and count number of vowels, consonents, uppercase and lowercase in the string

In [ ]:
s = input("Enter the string ")
c = 0
v = 0
u = 0
l = 0
for i in s:
  if i.isupper():
    u += 1
  if i.islower():
    l += 1
  if i in "AEIOUaeiou":
    v += 1
  else:
    c += 1
print("Number of vowels is", v)
print("Number of consonents is", c)
print("Number of Upper values is", u)
print("Number of Lower values is", l)
Enter the string Damini and deepika are sitting
Number of vowels is 12
Number of consonents is 18
Number of Upper values is 1
Number of Lower values is 25

Programs on List¶

41 - Write a program to input the name of all students one by one and print them in a list

In [ ]:
l = []
while True:
  name = input("Enter the name of student")
  l.append(name)
  s = input("Do you want to continue Y/N")
  if s == "N" or s == "No":
    break
print(l);
Enter the name of studentDamini
Do you want to continue Y/NY
Enter the name of studentShreya
Do you want to continue Y/NN
['Damini', 'Shreya']

42 - Write a program to input a list and a number and print its frequency in list.

In [ ]:
l = eval(input("Enter the list"))
k = int(input("Enter the number"))
cnt = 0
for i in l:
  if i == k:
    cnt += 1
print("The frequency of", k, "in list is", cnt)
Enter the list[4,5,6,7,4,5,]
Enter the number4
The frequency of 4 in list is 2

43 - Write a program to input a list and a number and print its frequency in list.

In [ ]:
l = eval(input("Enter the list"))
k = int(input("Enter the number"))
print("The frequency of", k, "in list is", l.count(k))
Enter the list[1,1,2,2,3,3,3,3,]
Enter the number3
The frequency of 3 in list is 4

44 - Write a program to input a list and swap elements at the even location with the elements at odd place.

In [ ]:
l = eval(input("Enter the list"))
for i in range(0, len(l), 2):
  s = l[i]
  l[i] = l[i+1]
  l[i+1] = s
print("The new list is", l)
Enter the list[8,7,6,5,4,3]
The new list is [7, 8, 5, 6, 3, 4]

OR

In [ ]:
l = eval(input("Enter the list"))
for i in range(0, len(l), 2):
  l[i], l[i+1] = l[i+1], l[i]
print("The new list is", l)
Enter the list[1,2,5,6,8,9]
The new list is [2, 1, 6, 5, 9, 8]

45 - Write a program to divide even numbers by 2 and multiply odd numbers by 2 in the list.

In [ ]:
l = eval(input("Enter the list"))
for i in range(0, len(l)):
  if l[i] % 2 == 0:
    l[i] = l[i] // 2
  else:
    l[i] = l[i] * 2
Enter the list[3,5,8,2,8,7]

46 - Write a program to find a number in a list. If present print its index.

In [ ]:
l = eval(input("Enter the list"))
k = int(input("Enter the number"))
for i in range(0, len(l)):
  if l[i] == k:
    print("The number is present at index", i)
    break
else:
  print("The number is not present in the list")
Enter the list[7,3,9,2,7]
Enter the number2
The number is present at index 3

47 - Write a program to input a list and print all its even number in a new list.

In [ ]:
l = eval(input("Enter the list"))
a = []
for i in l:
  if i % 2 == 0:
    a.append(i)
print("The new list is", a)
Enter the list[5,6,2,3,4,8,9]
The new list is [6, 2, 4, 8]

48 - Write a program to input a list of numbers and swap elements at the even location with the elements at odd places.

In [ ]:
l = eval(input("Enter the list"))
for i in range(0, len(l), 2):
  s = l[i]
  l[i] = l[i+1]
  l[i+1] = s
print("The new list is", l)
Enter the list[1, 2, 3, 4, 5, 6]
The new list is [2, 1, 4, 3, 6, 5]

49 - Write a program to input a list and print square of element at even position and cube elements at odd position in a new list.

In [ ]:
l = eval(input("Enter the list"))
a = []
for i in range(0, len(l)):
  if i % 2 == 0:
    a.append(l[i]**2)
  else:
    a.append(l[i]**3)
print("The new list is", a)
Enter the list[1,2,3,4,5,6]
The new list is [1, 8, 9, 64, 25, 216]

50 - Write a program to print frequency of all unique elements in the list.

In [ ]:
l = eval(input("Enter the list"))
b = []
for i in l:
  if b.count(i) == 0:
    print("Frequency of",i,"is",l.count(i))
  b.append(i)
Enter the list[1,6,3,8,9,3,2,1,5,7,8,9,2,4,8]
Frequency of 1 is 2
Frequency of 6 is 1
Frequency of 3 is 2
Frequency of 8 is 3
Frequency of 9 is 2
Frequency of 2 is 2
Frequency of 5 is 1
Frequency of 7 is 1
Frequency of 4 is 1

Tuple¶

51 - Write a program to input a tuple and print min, max and average of elements in tuple

In [ ]:
t = eval(input("Enter the tuple"))
mn = t[0]
mx = t[0]
sum = 0
for i in t:
  if mn > i:
    mn = i
  if mx < i:
    mx = i
  sum += i
print("Minimum element of the tuple is", mn)
print("Maximum element of the tuple is", mx)
print("Average of the list is", sum / len(t));
Enter the tuple8,3,5,9,1,3
Minimum element of the tuple is 1
Maximum element of the tuple is 9
Average of the list is 4.833333333333333

52 - Write a program to traverse a tuple of elements.

In [ ]:
t = eval(input("Enter the tuple"))
for i in range(0, len(t)):
  print(i+1, '-', t[i])
Enter the tuple5,7,3,1,8,9
1 - 5
2 - 7
3 - 3
4 - 1
5 - 8
6 - 9
  1. Write a program to linear search a number in a tuple of elements.
In [80]:
t = eval(input("Enter the tuple"))
k = int(input("Enter the number"))
for i in range(0, len(t)):
  if t[i] == k:
    print("The number is present at index", i)
    break
else:
  print("The number is not present in the list")
Enter the tuple76,98,23,55
Enter the number23
The number is present at index 2