1. Write a program to print Hello, world!
print(“Hello, world!”)
OR
x=”Hello, World!”
print(x)
Output:
Hello, world!
2. Write a program to add two numbers.
num1 = 10
num2 = 15
sum = num1 + num2
print(“Sum: “, sum)
Output:
Sum: 25
OR
num1,num2=10,15
print(“Sum: “, num1+num2)
3. Write a program to add two numbers with User Input.
num1 = int(input('Enter 1st number: '))
num2 = int(input('Enter 2nd number: '))
sum=num1+num2
print('Sum: ', sum)
Output:
Enter 1st number: 4
Enter 2nd number: 6
Sum: 10
OR
a,b = map(int, input ("Enter 2 numbers :" ).split())
sum=a+b
print("Sum: ",a+b)
Output:
Enter 2 numbers: 6 4
Sum: 10
4. Write a python program to find the Square Root.
num = float(input(‘Enter a number: ‘))
sqrt = num ** 0.5
print(‘Square Root: ‘, sqrt)
Output:
Enter a number: 16
Square Root: 4.0
Note: double-star operator, ** is used for exponentiation, where one number is raised to the power of another. Ex. 2**3 is 8
5. Write a program to calculate the area of a triangle. (Formula: Area = 1/2*Base * Height)
base=int(input(“Enter the value of base: ”))
height= int(input(“Enter the value of height: “))
area=1/2*base*height
print(‘Area of the triangle is: ‘, area)
Output:
Enter the value of base: 8
Enter the value of height: 6
Area of the triangle is: 24.0
6. Write a program to find the area of a circle. ( Formula: Area of circle= πr2, π=22/7)
r=float(input(“Enter the radius of a circle:”))
area=22/7*r*r
print(“Area of the circle: “, area)
print(“Area of circle: %.2f” %area)
Output:
Enter the radius of a circle: 3
Area of the circle: 28.285714285714285
Area of circle: 28.29
Note: %.2f floating-point specifies 2 numbers after the decimal.
7. Write a program in python to find the volume of a sphere. (Formula: Volume of sphere=4/3πr3 )
r=float(input(“Enter the radius of the sphere: “))
V = 4/3* 22/7* r**3
print(‘The volume of the sphere is: ‘, V)
Output:
Enter the radius of the sphere: 6
The volume of the sphere is: 905.1428571428572
8. Write a program to convert Celsius to Fahrenheit.
C = float(input(“Enter the Temperature in Celsius : “))
F= (9/5* C) + 32
print(“Temperature in Fahrenheit :”, F)
Output:
Enter the Temperature in Celsius : 98
Temperature in Fahrenheit : 208.4
9. Write a program to convert Fahrenheit to Celsius.
F = float(input(“Enter the Temperature in Fahrenheit : “))
C= (F-32) * 5/9
print(“Temperature in Celsius:”, C)
Output:
Enter the Temperature in Fahrenheit : 208.4
Temperature in Celsius : 98.0
10. Write a program to swap two variables.
x = int(input(‘Enter value of x: ‘))
y = int(input(‘Enter value of y: ‘))
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print(‘The value of x after swapping: ‘,x)
print(‘The value of y after swapping: ‘,y)
Output:
Enter value of x: 5
Enter value of y: 10
The value of x after swapping: 10
The value of y after swapping: 5
11. Write a program to calculate Simple Interest.
P = int(input(“Enter the principal amount :”))
T = int(input(“Enter the time period :”))
R = int(input(“Enter the rate of interest :”))
si = (P * T * R) / 100
print(‘The Simple Interest is’, si)
Output:
Enter the principal amount :4000
Enter the time period :2
Enter the rate of interest :4
The Simple Interest is 320.0
12. Write a program to check two numbers are equal or not.
a=int(input(“Enter 1st number: “))
b=int(input(“Enter 2nd number: “))
if(a==b):
print(“Numbers are equal”)
else:
print(“numbers are not equal”)
Output:
Enter 1st number: 7
Enter 2nd number: 9
numbers are not equal
13. Write a program to find the larger number among two numbers.
a=int(input(“Enter 1st number: “))
b=int(input(“Enter 2nd number: “))
if(a>b):
print(“Larger number is: “, a)
else:
print(“Larger number is: “, b)
Output:
Enter 1st number: 4
Enter 2nd number: 7
Larger number is: 7
14. Write a program to check a number is even or odd.
num = int(input(“Enter a number: “))
if(num % 2 == 0):
print(num, “is even number”)
else:
print(num, “is odd number”)
Output-1
Enter a number: 8
8 is even number
Output-2
Enter a number: 5
5 is odd number
15. Write a program to check a year is leap year or not.
year = int(input(“Enter a year: “))
if (year % 4 ==0):
print(year, “is leap year”)
else:
print(year, “is not a leap year”)
Output:
Enter a year: 2020
2020 is leap year
16. Write a program to find the largest number among three numbers.
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
num3 = int(input(“Enter third number: “))
if(num1>num2 and num1>num3):
print(“Largest number is: “,num1)
elif(num2>num1 and num2>num3):
print(“Largest number is: “,num2)
else:
print(“Largest number is”, num3)
Output:
Enter first number: 6
Enter second number: 7
Enter third number: 9
The largest number is 9
17. Write a program to check if a number is Positive, Negative or Zero.
num = float(input(“Enter a number: “))
if num > 0:
print(“Positive number”)
elif num == 0:
print(“It is Zero”)
else:
print(“Negative number”)
Output-1:
Enter a number: 2
Positive number
Output-2
Enter a number: 0
It is Zero
Output-3
Enter a number: -5
Negative number
18. Write a program to print characters of a string.
s=’Hello’
for i in s:
print(i)
Output:
H
e
l
l
o
We can do the above program by taking input from user :
s=input(“Enter a string: “)
for i in s:
print(i)
Output:
Enter a string: India
I
n
d
i
a
19. Write a program to print characters of “python”.
for letter in ‘python’:
print(letter)
Output:
p
y
t
h
OR
for letter in ‘python’:
print(letter, end=’ ‘)
Output:
p y t h o n
20. Write a program to print elements in a list.
fruits=[‘banana’,’apple’,’mango’,’orange’]
for i in fruits:
print(i)
Output:
banana
apple
mango
orange
OR
fruits=[‘banana’,’apple’,’mango’,’orange’]
for i in fruits:
print(i, end= ‘ ‘)
Output:
banana apple mango orange
21. Write a program to print numbers from 1 to 20.
for i in range(1,21):
print(i,end=’ ‘)
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
22. Write a program to print series of number from 100 to 50 in reversing order.
for i in range(100,49,-1):
print(i,end=’ ‘)
Output:
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50
23. Write a python program to print all even numbers in a range.
for i in range(2,21,2):
print(i,end=’ ‘)
Output:
2 4 6 8 10 12 14 16 18 20
OR We can do the above program by taking range limit from user,
start = int(input(“Enter the start of range: “))
end = int(input(“Enter the end of range: “))
for num in range(start, end + 1,2):
print(num,end=’ ‘)
Output:
Enter the start of range: 4
Enter the end of range: 30
4 6 8 10 12 14 16 18 20 22 24 26 28 30
24. Write a program to print the following pattern.
*
* *
* * *
* * * *
* * * * *
for i in range(1,6):
for j in range(1,i+1):
print(“*”,end=” “)
print()
25. Write a program to print the following pattern.
* * * * *
* * * *
* * *
* *
*
for i in range(1,6):
for j in range(i,6):
print(“*”,end=” “)
print()
26. Print the following pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for i in range(1, 6):
for j in range(0,i):
print(i,end=” “)
print()
27. Print the following pattern.
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
for i in range(1, 6):
for j in range(i,6):
print(i,end=” “)
print()
28. Write a program to find the sum of first n natural numbers.
n=int(input(“Enter the value of n: “))
sum=0
for i in range(1,n+1):
sum=sum+i
print(“Result is: “,sum)
Output:
Enter a number: 5
Result is: 15
29. Write a program to find the sum and average of first n natural numbers.
n=int(input(“Enter the value of n: “))
sum=0
for num in range(1,n+1):
sum=sum+num
print(“Sum of 1st “,n,”number is: “,sum)
avg=sum/n
print(“Average of 1st “,n,”number is”,avg)
Output:
Enter the value of n: 5
Sum of 1st 5 number is: 15
Average of 1st 5 number is 3.0
30. Write a program to display the multiplication table.
num = int(input("Enter the number: "))
print("Multiplication table of", num)
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
Output:
Enter the number: 5
Multiplication Table of 5
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
31. Write a program display multiplication table from 1 to 5.
for i in range(1,6):
for j in range(1,11):
print(i, 'x', j, '=', i*j)
print()
Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
…………….
……………..
32. Write a program in python to print first 10 natural numbers using while loop.
i=1
while(i<=10):
print(i, end=’ ‘)
i=i+1
Output:
1 2 3 4 5 6 7 8 9 10
33. Write a program in python to print first 10 even numbers using while loop.
x=int(input(“Enter a number: “))
i=1
while(i<=x):
if(i%2==0):
print(i,end=” “)
i=i+1
Output:
Enter a number: 20
2 4 6 8 10 12 14 16 18 20
34. Write a program to print the following pattern using while loop.
*
* *
* * *
* * * *
* * * * *
i=1
while(i<=5):
j=1
while(j<=i):
print(“*”,end=” “)
j+=1
print()
i+=1
35. Write a program to print the following pattern.
* * * * *
* * * *
* * *
* *
*
i=1
while(i<=5):
j=5
while(j>=i):
print(“*”,end=” “)
j-=1
print()
i+=1
36. Write a program in python to print the reverse digits of an integer.
rev = 0
n = int(input(“Enter a number: “))
while n > 0:
rev = rev * 10 + n % 10
n = n//10
print(“Reverse of the number”, rev)
Output:
Enter a number: 234
Reverse of the number 432
37. Find the sum of digits of a given number.
sum=0
n=int(input(“enter a number; “))
while(n>0):
sum=sum+n%10
n=n//10
print(“Sum of digits: “,sum)
Output:
Enter a number: 123
Sum of digits: 6
38. Write a python program to create an array contains six integers. Also print all the members of the array.
import array
vals=array.array(‘i’,[10,20,30,40,50,60])
print(“Elements of the given array: “);
for i in vals:
print(i, end=’ ‘)
OR
import array as arr
vals=arr.array(‘i’,[10,20,30,40,50,60])
print(“Elements of the given array: “);
for i in vals:
print(i, end=’ ‘)
OR
from array import *
vals=array(‘i’,[10,20,30,40,50,60])
print(“Elements of the given array: “);
for i in vals:
print(i, end=’ ‘)
Output:
Elements of given array:
10 20 30 40 50 60
39. Write a program to add two numbers using Function.
def add(a,b)
return a+b
sum=add(10,5)
print(“Sum: “,sum)
Output:
Sum: 15
40. Write a python program to add two positive integers without using the ‘+’ operator.
print(“Sum of two numbers: “);
sum=sum([5,5])
print(sum)
Output:
Sum of two numbers:
10
OR
x=int(input(“Enter 1st number: “))
y=int(input(“Enter 1st number: “))
z=sum([x,y])
print(“Sum: “,z)
Output
Enter 1st number: 20
Enter 1st number: 30
Sum: 50
I truly admired the work you’ve put in here. The design is refined, your authored material stylish, however, you seem to have acquired some trepidation about what you intend to present next. Undoubtedly, I’ll revisit more regularly, similar to I have nearly all the time, in the event you sustain this rise.
Thanks a lot for your comment.
This website has quickly become my go-to source for [topic]. The content is consistently top-notch, covering diverse angles with clarity and expertise. I’m constantly recommending it to colleagues and friends. Keep inspiring us!
Thank you for your kind words. Thanks a lot for recommending my site…
What a fantastic resource! The articles are meticulously crafted, offering a perfect balance of depth and accessibility. I always walk away having gained new understanding. My sincere appreciation to the team behind this outstanding website.
Thank you!!!
Thank you for your response! I’m grateful for your willingness to engage in discussions. If there’s anything specific you’d like to explore or if you have any questions, please feel free to share them. Whether it’s about emerging trends in technology, recent breakthroughs in science, intriguing literary analyses, or any other topic, I’m here to assist you. Just let me know how I can be of help, and I’ll do my best to provide valuable insights and information!
Thank you so much…
Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish just about gossips and internet and this is actually frustrating. A good site with exciting content, that is what I need. Thank you for keeping this web site, I will be visiting it. Do you do newsletters? Can not find it.