Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

D21IT171 Harshal Jethwa

Python Study Jam – 1


1. Write Python Program (WPP) to enter length and breadth of a
rectangle and calculate area and perimeter of the rectangle.

w = float(input('Please Enter the Width of a Rectangle: '))


h = float(input('Please Enter the Height of a Rectangle: '))

Area = w * h

Perimeter = 2 * (w + h)

print("\n Area of a Rectangle is:",Area)


print(" Perimeter of Rectangle is: ",Perimeter)

2. WPP to enter radius of circle and calculate area and circumference


of circle.
r = float(input("Enter the radius of the circle:"))

area = 3.14*r**2

circ = 2*3.14*r

print("The area of the circle is", area)

print("The circumference of the circle is:", circ)


D21IT171 Harshal Jethwa

3. WPP to enter value in centimetre and convert it to meter and


kilometre.

print("Enter the length in centimeter::")

c = float(input())

m = (float)(c / 100)
k = (float)(c / 100000)

# Output
print("Length in Meter = ", m, " meter")
print("Length in Kilometer = ", k, " kilometer")

4. WPP to enter value in Celsius and convert it to farhenient.


celsius = float(input("Enter temperature in celsius: "))
fahrenheit = (celsius * 9/5) + 32
print('{0} Celsius is: {1} Fahrenheit' .format(celsius,fahrenheit))

5. WPP to enter value in days and convert in form of years, months and
days (assuming all month has 30 days and year is of 360).

eg: input is 400 so output should be 1 year 1 month and 10 days

days=int(input("Enter Day:"))

years =(int) (days / 365)


weeks =(int) (days / 7)
months =(int) (days / 30)
days = (days % 365) % weeks
D21IT171 Harshal Jethwa

print("Days to Years:",years)
# print("Days to Weeks:",weeks)
print("Days to Months:",months)
print("Days to days:",days)

6. WPP to find power of any number in form of x^y where x and y are
user inputs.
print("Enter the base & exponent values::")

b = int(input())
e = int(input())
r=1

r = pow(b, e)
print("Result:: ", b, "^", e, " = ", r)

7. WPP to enter 2 angles and using function thirdangle( angle1, angle2 )


calculate third angle.
def Thirdangle(a, b):
return 180 - (a + b)

a = float(input('Please Enter the First Angle of a Triangle: '))


b = float(input('Please Enter the Second Angle of a Triangle: '))

c = Thirdangle(a, b)
print("Third Angle of a Triangle = ", c)
D21IT171 Harshal Jethwa

8. WPP to enter base and height of triangle and calculate area of


triangle.
b = float(input('Enter base of a triangle: '))

h = float(input('Enter height of a triangle: '))

area = (b * h) / 2
print(area)

9. WPP to enter marks of 5 subjects and find the mean of 5 subjects,


calculate percentage. If percentage is less than 35 print fail else print
pass.
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg<=35):
print("Fail")
else:
print("Pass")

10. WPP to enter principal amount, time and interest rate. Create
simple_interest(principal, time, rate) function to calculate simple
interest.
def Simple_interest(P,R,T):
print('The principal is', P)
print('The rate of interest is', R)
print('The time period is',T)
Si = (P * R * T)/100
D21IT171 Harshal Jethwa

print('The Simple Interest is', Si)


return Si
P = float(input('Please Enter the p data: '))
R = float(input('Please Enter the r data '))
T = float(input('Please Enter the t data'))

Simple_interest(P, R, T)

11. WPP to enter principal amount, time and interest rate. Create
compound_interest(principal, time, rate) function to calculate
compound interest.
def compound_interest(principle, rate, time):
result = principle * (pow((1 + rate / 100), time))
return result

p = float(input("Enter the principal amount: "))


r = float(input("Enter the interest rate: "))
t = float(input("Enter the time in years: "))

amount = compound_interest(p, r, t)
interest = amount - p
print("Compound amount is %.2f" % amount)
print("Compound interest is %.2f" % interest)
D21IT171 Harshal Jethwa

12. Write a Python program that accepts an integer (n) and computes
the value of n+nn+nnn.
Sample value of n is 5
Expected Result : 615
a = int(input("Input an integer : "))
n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
print (n1+n2+n3)

13. Write a Python program to find whether a given number (accept


from the user) is even or odd, print out an appropriate message to
the user.
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")

14. Write a Python program to count the number 4 in a given list. Note
use the given list

arr = ( 1, 3, 4, 5, 3, 24, 2, 4, 5, 2, 222, 32, 4, 10, 3, 4, 50, 100, 4)

def counta(nums):
count = 0
for num in nums:
if num == 4:
count = count + 1

return count
print(counta([1, 3, 4, 5, 3, 24, 2, 4, 5, 2, 222, 32, 4, 10, 3, 4, 50, 100, 4]))
D21IT171 Harshal Jethwa

15. Write a Python program to test whether a passed letter is a vowel or


not using vowel( character) function.
def is_vowel(char):
all_vowels = 'aeiou'
return char in all_vowels
print(is_vowel('c'))
print(is_vowel('e'))

16. Write a Python program to concatenate all elements in a list into a


string and return it using function.
def concatenate_list_data(list):
result= ''
for element in list:
result += str(element) + ","
return result

print(concatenate_list_data([2, 10, 22, 21]))

17. Write a Python program to print all even numbers from a given
numbers list in the same order and stop the printing if any numbers
that come after 27 in the sequence.

numbers = ( 33, 2, 5, 12, 23, 55, 6, 78, 9, 27, 56, 7, 89, 4)

numbers = [
33, 2, 5, 12, 23, 55, 6, 78, 9, 27, 56, 7, 89, 4
]

for x in numbers:
if x == 27:
print(x)
break;
elif x % 2 == 0:
D21IT171 Harshal Jethwa

print(x)

18. Write a Python program to compute the greatest common divisor


(GCD) of two positive integers. Note both numbers will be user
input.
def gcd(a, b):
if(b == 0):
return a;
else:
return gcd(b, a % b)

num1 = float(input(" Please Enter the First Value Num1 : "))


num2 = float(input(" Please Enter the Second Value Num2 : "))

Val = gcd(num1, num2)


print("\n GCD of {0} and {1} = {2}".format(num1, num2, Val))

19. Write a Python program to get the least common multiple (LCM) of
two positive integers. Note both numbers will be user input.
def compute_lcm(x, y):

if x > y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
D21IT171 Harshal Jethwa

lcm = greater
break
greater += 1

return lcm

num1 = float(input())
num2 = float(input())

print("The L.C.M. is", compute_lcm(num1, num2))

20. Write a Python program to compute the distance between the points
(x1, y1) and (x2, y2). Note both coordinates will be user input.
print(distance)
import math
x = [4, 0]
y = [6, 6]
distance = math.sqrt( ((x[0]-y[0])**2)+((x[1]-y[1])**2) )

21. Write a python program to sum of the first n positive integers. Note
n will be defined by user.
n = int(input("Input a number: "))
sum = (n * (n + 1)) / 2
print("Sum of the first {0} positive integers:{1}" .format(n,sum))

22. Write a Python program to find factorial of any number defined by


user.
num = int(input("Enter a number: "))
fact = 1
if num < 0:
D21IT171 Harshal Jethwa

print(" Factorial does not exist for negative numbers")


elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact*i
print("The factorial of {0} is {1}".format(num,fact))

23. Write a Python program to calculate the hypotenuse of a right


angled triangle.
from math import sqrt
print("Input lengths of shorter triangle sides:")
a = float(input("a: "))
b = float(input("b: "))
c = sqrt(a**2 + b**2)
print("The length of the hypotenuse is:", c )

24. Write a Python program to calculate the sum of the digits in an


integer.
n=int(input("Enter a number:"))
total=0
while(n>0):
digit=n%10
total=total+digit
n=n//10
print("The total sum of digits is:",total)
D21IT171 Harshal Jethwa

25. Write a Python program to find number of prime numbers between


1 to n where n is defined by user.
Num = int(input("Please Enter any Number: "))
count = 0
for i in range(2, (Num//2 + 1)):
if(Num % i == 0):
count = count + 1
break
if (count == 0 and Num != 1):
print(" %d is a Prime Number" %Num)
else:
print(" %d is not a Prime Number" %Num)

You might also like