PYTHON
PROGRAMS
Progranm 1: Write a program to Print statements
main.py Run Shell
1 print("The cat drove the car, ") The cat drove the car.
2 print("The puppy makes great pizzas.") The puppy makes great pizzas.
3
4
main.py Run Output
1 print(" Name" ) Name
2 print (" Name" ) Name
3 print("Name") Name
Program 2: Write a program to Print statements
main.py Run Shell
1 print("2+5") 2+5
2 print(2+5) 7
3 print("The sum is",2+5) The sum is 7
4
5
main.py Run Output
1 print(4*(3*6) /9*(5+1)) 48.0
2 print (8* (4+7)*50/(10*2) ) 220.0
Program 3: To Calculate Sum of 2 Numbers
main.py Run Shell
1 a-10 a+b= 14
2 b=4
3 print ("a+b=" ,a+b)
Program 4: To Calculate area of Rectangle
1 l=10 Area= 50
2 b=5
3 print("Area=", 1*b)
4
5
Program 5: To Calculate quotient
main.py Run Shell
1 b=25 The result
2 C=5 b/c= 5.0
3 q-b/c
4 print (" The result")
5 print("b/c=".q)
6
Program 6: Write a program to demonstrate
the use of Input)function
main.py Run Output
1 name-input("Enter your name: ") Enter your name: karan
2 num=input ("Enter any number:") Enter any number:120
3 print("Your name is", name) Your name is karan
4 print("you have entered" , num) you have entered 120
Program 7: Write a program to enter 2
numbers and display their concatenation (to
link together)
main.py Run Output
1 n1=input ("Enter number1 =") Enter number1=2
2 n2-input ("Enter number2=") Enter number2=7
3 Ans=n1 tn2 Ans= 27
4 print("Ans=" ,Ans)
Note: The values are added, once int) and float(0 are used.
Program 8: Write a program to demonstrate
an error while performing calculation on
string values
main.py Run Output Clear
1 n1=input ("Enter number 1=") Enter number14
2 n2-input ("Enter number2=") Enter number2-6
3 Ansn1"n2 ERROR !
4 print ("Ans=", Ans) Traceback (most recent call last):
File "main.py>", line 3, in <module>
TypeError: can't multiply sequence by non-int of type
'str'
* Code Exited With Errors =
Note:
" Toget rid of this problem, python provides int() or float) to be used alongwith the input() function to
convert the strings into integer or float values.
" The values are added, once int) and float() are used.
Program 9; Write a program to input two
numbers, Display the numbers with a
message.
main.py Run Shell
1 no1=int (input ("Enter the first number=") ) Enter the first number=10
2 no2=int(input (" Enter the second number=")) Enter the second number=5
3 print ("The first umber which you entered is" The first number which you entered is 10
, no1) The second number which you entered is 5
4 print ("The second number which you entered
is",no2)
Program 10: Write a program to inputtwo
numbers, Find the sum and the product.
main.py Run Shell
1 a-int (input ("Enter first number- ")) Enter first number - 20
2 b=int (input ("Enter second number-")) Enter second number-5
3 sum=a+b The sum is 25
4 product=a*b The product is 100
5 print(" The sum is" , sum)
6 print(" The product is",product)
Program 11: Writea program to input two numbers.
Find the sum.difference.product and quotient
main.py Run Shell
1 a=int (input (" Enter first number -")) Enter first number-15
2 b=int (input("Enter second number-")) Enter second number-5
sum=a-b The sum is 20
4 difference-a-b The difference is 10
5 product=a*b The product is 75
6 quotient=a/b The quotient is 3.0
7 print (" The sum is", sum)
8 print (" The difference is",difference)
9 print ("The product is" , product)
10 print(" The quotient is",quotient)
Program 12: Write a program to input length and
breadth. Caleulate the area of rectangle
main.py Run Shell
1 L=int (input ("Enter Length=")) Enter Length=20
2 B-int (input ("Enter Breadth=" )) Enter Breadth=5
3 area-L*B Area= 100
4 print("Area=",area)
Program 13: Write a program to input length and
breadth. Calculate the Perimeter of rectangle
main.py Run Shell
1 L=int (input ("Enter Length=")) Enter Length=2
2 B-int (input ("Enter Breadth=")) Enter Breadth=3
3 p=2*(L+B) Perimeter= 10
4 print("Perimeter=",p)
Program 14: Write a program to input length and
breadth. Calculate the area of square.
main.py Run Shell
1 side=int (input ("Enter side=")) Enter side-8
2 area-side*side Area of square= 64
3 print ("Area of square=", area)
Program 15; Write a program to input radius.
Calculate the area of circle.
main.py Rurn Output
1 r=float (input ("Enter Radius=")) Enter Radius=5
2 area-3.14*rtr Area of Circle 5.0
3 print(CArea of Circle=", r)
Program 16: Write a program to input radius.
Caleulate the perimeter of circle.
main.py Run Output
1 rfloat(input ("Enter Radius=")) Enter Radius=6
2 p=2*3.14*r Perimeter of Circle= 37.68
3 print ("Perimeter of Circle=".p)
Program 17: Write a program to input radius.
Calculate the area oftriangle.
main.py Run Output
1 b-float (input ("Enter Base=") ) Enter Base=5
2 h-float(input("Enter Height=")) Enter Height=6
3 area=1/2*b*h Area of Triangle= 15.0
4 print("Area of Triangle=" ,area)
Program 18: Write a program to input marks and
find sum and average of four subiects.
main.py Run Shell
1 s1=1nt (input("Enter Science marks=")) Enter Science marks=18
$2-int(input ("Enter Maths marks=*)) Enter Maths marks=20
3 S3-1nt(input ("Enter English marks=" )) Enter English marks=17
4 s4=int (input("Enter Computer marks=")) Enter Computer marks=19
Sum=s1+s2-53+s4 Sum of four subject marks: 74
averagesum/4 Average of four subject marks: 18.5
7 print ("Sum of four subject marks:",sum)
8 print("Average of four subject marks:",average)
Program 19: Write a program to input any
number and check whether it is positive or
negative number.
main.py Run Shell
1 n=int (input("Enter any number=")) Enter any number=-3
2 1f n>0: The number is negative
3 print("The number is positive")
4- else:
5 print ("The number is negative")
Program 20: Write a program to input
age and check whether the person is
eligibleto vote or not.
main.py Run Output
1 age-int (input (" Enter the age=")) Enter the age=20
2- if (age>=18) : You are eligible to vote
3 print("You are eligible to vote")
4- else: === Code Execution Successful ===
5 print ("You are not eligible to vote")