PYTHON PROGRAMS
1. Add Two Numbers with User Input
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
sum = num1+ num2
# Display the sum
print(“The sum is “, sum)
2. Calculate the Area of a Triangle
a=5
b=6
c=7
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a) *(s-b) *(s-c)) ** 0.5
print(“The area of the triangle is”,area)
3. Swap Two Variables
x=5
y = 10
# create a temporary variable and swap the values
temp = x
x=y
y = temp
print(x,y)
4. Square of a number:
X=20
Print(x*x)
5. Check whether a number is even or odd.
num1=20
if(num1%2==0):
print(“the given number is even”)
else:
print(“The given number is odd”)
6. Check whether Year is a Leap Year or not.
year = int(input("Enter Year: "))
# Leap Year Check
if year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
7. Check Vowel or Consonant
ch = input("Enter a character: ")
if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
print(ch, "is a Vowel")
else:
print(ch, "is a Consonant")
8. Check Whether a Number is Positive or Negative
n=int(input("Enter number: "))
if(n>0):
print("Number is positive")
else:
print("Number is negative")
9. Find Largest of Three numbers using Elif Statement
a = float(input("Please Enter the First value: "))
b = float(input("Please Enter the First value: "))
c = float(input("Please Enter the First value: "))
if (a > b and a > c):
print("a is Greater Than both b and c".)
elif (b > a and b > c):
print("b is Greater Than both a and c".)
elif (c > a and c > b):
print("c is Greater Than both b and a".)
else:
print("Either any two values or all the three values are equal")
10. Check Number is Divisible by 5 and 11
number = int(input(" Please Enter any Positive Integer : "))
if((number % 5 == 0) and (number % 11 == 0)):
print("Given Number {0} is Divisible by 5 and 11".format(number))
else:
print("Given Number {0} is Not Divisible by 5 and 11".format(number))
11. Program to Check Alphabet
ch = input("Enter a character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
print(ch, "is an Alphabet")
else:
print(ch, "is not an Alphabet")
12. Area of a circle
PI =3.142
R=4
Area=PI*r*r
Print(Area)
13. Program to find Simple Interest
P=input("Enter p: ")
t=input("Enter t: ")
r=input("Enter r: ")
print(“The principal is”, p)
print(“The time period is”, t)
print(“The rate of interest is”,r)
si = (p * t * r)/100
print(“The Simple Interest is”, si)
14.Slicing of List
List Length
To determine how many items a list has, use the len() function:
a = ["apple", "banana", "cherry"]
print(len(a))
Positive indexing
a=[21,22,"welcome","cat",30.5]
print(a[0])
Negative indexing
a=[21,22,"welcome","cat",30.5]
print(a[-1])
Range
a = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(a[2:5])
b= ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(b[:4])
c = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(c[2:])
d = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(d[-4:-1])
Slicing a list
a=[21,22,"welcome","cat",30.5]
print(a[2:4])
print(a)
Adding and deleting items from list
a=[21,22,"welcome","cat",30.5]
a.append("dog")
a.remove("welcome")
print(a)
INSERT METHOD
The insert () method inserts an item at the specified index.
a=[21,22,"welcome","cat",30.5]
a.insert(0,1000)
print(a)
POP METHOD
The pop() method removes the specified index.
a=[21,22,"welcome","cat",30.5]
a.pop(3)
print(a)
Leap Year:
Range value:
While loop:
Square of a number:
square root:
Write a Python program to reverse a string.