Python codes
Areas
Write a program to calculate average of two
numbers
x= eval(input(“enter the first number”))
y= eval(input(“enter the second number”))
print(“ave= “, (x+y)/2)
Write a program to calculate area of triangle
base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))
area = (1/2) * base * height
print('Area of triangle = ',area)
Another way (with inputs)
a = float(input('Enter the first side of the triangle: '))
b = float(input('Enter the second side of the triangle: '))
c = float(input('Enter the third side of the triangle: '))
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print('Area of triangle = %0.2f' %area)
Write a program to calculate area of circle
r = int(input('Enter the radius of the circle : '))
area = 3.14 * r**2
print('Area of circle = %0.2f' %area)
Write a program to calculate area of Rectangle
L = int(input('Enter the length of Rectangle : '))
B = int(input('Enter the breadth of Rectangle: '))
area = (L*B)
print('Area of Rectangle = %0.2f' %area)
Write a program to calculate area of Square
S= int(input('Enter the length of side of the Square : '))
area = (S**2)
print('Area of Square = %0.2f' %area)
Patterns
Write a program to print this pattern
***
***
***
for x in range (1,3):
for y in range (1,3):
print("*",end="")
print()
Write a program to print this pattern
*
**
***
****
x=1
n=int(input())
for i in range (n):
for j in range (x):
print("*",end="")
print()
x+=1
Write a program to print this pattern
*****
****
***
**
*
for x in range (1,6):
for y in range (6,x,-1):
print("*",end="")
print()
Write a program to print this pattern
*
**
***
****
*****
for x in range (1,6):
for y in range (5,x,-1):
print(" ",end="")
for z in range (0,x):
print("*",end="")
print()
Try use if statement
Write a program to print this pattern
*****
****
***
**
*
for x in range (5,0,-1):
for y in range (x,5):
print(" ",end="")
for z in range (0,x):
print("*",end="")
print()
Write a program to print this pattern
*
**
***
****
*****
for x in range (1,6):
for y in range (5,x,-1):
print(" ",end="")
for z in range (0,x):
print("*",end="")
print()
Write a program to print this pattern
*****
****
***
**
*
for x in range (5,0,-1):
for y in range (5,0,-1):
if x>=y:
print("*",end="")
else:
print(" ",end="")
print()
Other Programs
Prime (using if)
count=0
n=int (input("Enter the number"))
for x in range (1,n+1):
if n%x==0:
count+=1
if count ==2:
print(n," is a prime number")
else:
print(n,"is not a prime number")
Factorial
def factoriall(n):
if n<=1:
return 1
else:
n=n*factoriall(n-1)
return n
n =int(input("Enter the number: "))
print ("The Factorial of ",n,"is",factoriall(n))
Even & Odd
n =int(input("Enter the number: "))
if n%2 ==0:
print ("The number is Even ")
else:
print ("The number is Odd ")
Max & Min
l= [2,5,6]
maximum= max(l)
print ("the maximum number is :",maximum)
minimum= min(l)
print ("the minimum number is :",minimum)
Sqrt:
l= [2,5,6]
for x in l:
sqrt = x ** 0.5
print("square root:", sqrt)