Hello Everyone,
I was practicing some sums and I found it really interesting. So I thought to share with you all. If in-case you were struggling like me for hours and hours until the solution was very obvious... then you might stumble upon this blog.
Lets get on with the code:-
1) Calculate change for Groceries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | >>> #_Sum no.1 Calculate change remaining after grocerie shopping >>> milk = 48; butter = 2*150; curd = 20.50; bread = 30; eggs = 250.68; vegetables = 100.75 >>> >>> shopping_cost = milk + butter + curd + bread + eggs + vegetables >>> print (shopping_cost) 749.9300000000001 >>> >>> change = int(input("Change you have....\nRs.1 \nRs.2 \nRs.5 \nRs.10 \nRs.50 \nRs.100 \nRs.200 \nRs.500 \nRs.1000 \n\nYour Change:- ")) Change you have.... Rs.1 Rs.2 Rs.5 Rs.10 Rs.50 Rs.100 Rs.200 Rs.500 Rs.1000 Your Change:- 5 >>> >>> my_change = shopping_cost - change >>> print(my_change) 744.9300000000001 >>> >>> print(round(my_change,2)) 744.93 >>> print(round(my_change,0)) 745.0 |
2) Calculate area of a triangle
1 2 3 4 5 6 7 8 9 10 11 12 | >>> #_Sum no.2 Calculate area of a triangle >>> # Note:- Area of a triangle is 1/2 * b * h >>> base = float(input('Enter base of a triangle:- ')) Enter base of a triangle:- 60 >>> height = float(input('Enter height of triangle:- ')) Enter height of triangle:- 75 >>> area = 1/2 * base * height >>> print(area) 2250.0 >>> print('Area of a triangle with base', base, 'and height', height, 'is', area) Area of a triangle with base 60.0 and height 75.0 is 2250.0 >>> |
3) Calculate Quadratic Formula
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | >>> #_Sum no.3 Calculate equation using quadratic formula with cmath.sqrt >>> # Note:- Quadratic formula has two output as an answer >>> # sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a >>> # sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a >>> # Q.Write a program in such a way that it accepts a, b, c >>> # and returns correct output >>> >>> from cmath import sqrt >>> a = float(input('Enter a: ')) Enter a: 1 >>> b = float(input('Enter b: ')) Enter b: 2 >>> c = float(input('Enter c: ')) Enter c: 3 >>> >>> # Using sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a >>> sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a >>> >>> # Using sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a >>> sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a >>> >>> #Answer >>> print('The Answer is {0} and {1}'.format(sol_1,sol_2)) The Answer is (-2+1.4142135623730951j) and (-2-1.4142135623730951j) >>> >>> print('sol_1 =', sol_1) sol_1 = (-2+1.4142135623730951j) >>> print('sol_2 =', sol_2) sol_2 = (-2-1.4142135623730951j) |
3.1) Math Domain Error
Note:- If you use 'from math import sqrt' you will get an Error -
## ValueError: math domain error
therefore instead use cmath import sqrt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | >>> #Note:- if you use 'from math import sqrt' you will get an error >>> #Example:- >>> from math import sqrt >>> a = float(input('Enter a: ')) Enter a: 1 >>> b = float(input('Enter b: ')) Enter b: 2 >>> c = float(input('Enter c: ')) Enter c: 3 >>> # Using sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a >>> sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> sol_1 = -b + sqrt(b**2 - 4*a*c) / 2*a ValueError: math domain error >>> # Using sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a >>> sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a Traceback (most recent call last): File "<pyshell#71>", line 1, in <module> sol_2 = -b - sqrt(b**2 - 4*a*c) / 2*a ValueError: math domain error >>> >>> #Answer >>> print('The Answer is {0} and {1}'.format(sol_1,sol_2)) Traceback (most recent call last): File "<pyshell#74>", line 1, in <module> print('The Answer is {0} and {1}'.format(sol_1,sol_2)) NameError: name 'sol_1' is not defined >>> |
Hope you like the tutorial.
Python is simple and fun to play with and yet very powerful. 🔥