Python Programs
Python Programming Language
Program to calculate the number of times a specific letter occurs in a string.
name = input('What is your name : ')print('\nHello '+ name)st = input('Please enter a message : ')ch = input('Which letter would you like to count the occurences of : ')print('Your message : ' + st)st = st.lower()count = st.count(ch)print(name + ', your message has ' + str(count) + ' ' + ch + " 's in it.")
Output :-
What is your name : Passable BlogHello Passable BlogPlease enter a message : This is Passable Blog. Here are some python programs.Which letter would you like to count the occurences of : pYour message : This is Passable Blog. Here are some python programs.Passable Blog,your message has 3 p's in it.
Program to create a python calculator that will perform addition, subtraction, multiplication and division.
#PYTHON CALCULATORdef add(num1,num2):result=num1+num2print('Addition : '+ str(result))def sub(num1,num2):result=num1-num2print('Subtraction : '+ str(result))def mul(num1,num2):result=num1*num2print('Multiplication : '+ str(result))def div(num1,num2):result=num1/num2print('Division : '+ str(result))print('Python Calculator : ')print('1.Addition')print('2.Substraction')print('3.Multiplication')print('4.Division')print('5.Exit')while True:ch=int(input('\nEnter your choice : '))if (ch>=1 and ch<=6):if ch==1 :num1 = int(input('Enter first number : '))num2 = int(input('Enter second number : '))add(num1,num2)if ch==2:num1 = int(input('Enter first number : '))num2 = int(input('Enter second number : '))sub(num1,num2)if ch==3:num1 = int(input('Enter first number : '))num2 = int(input('Enter second number : '))mul(num1,num2)if ch==4:num1 = int(input('Enter first number : '))num2 = int(input('Enter second number : '))div(num1,num2)if ch==5:breakelse :print('You entered a wrong choice')
Output:-
Python Calculator :1.Addition2.Substraction3.Multiplication4.Division5.Exit
Enter your choice : 1Enter first number : 2Enter second number : 3Addition : 5
Enter your choice : 2Enter first number : 9Enter second number : 6Subtraction : 3
Enter your choice : 3Enter first number : 6Enter second number : 5Multiplication : 30
Enter your choice : 4Enter first number : 6Enter second number : 3Division : 2.0
Enter your choice : 5
Program to solve quadratic equations.
import cmathn = int(input('\nHow many equations would you like to solve today : '))for i in range(1,n+1):print('Solving equation #' + str(i))print('------------------------------')a = float(input('\nPlease enter your value of a(coefficient of x^2) : '))b = float(input('Please enter your value of b(coefficient of x) : '))c = float(input('Please enter your value of c(coefficient) : '))x1 = (-b+cmath.sqrt(b**2-4*a*c))/(2*a)x2 = (-b-cmath.sqrt(b**2-4*a*c))/(2*a)print('The solution to ' + str(a)+ 'x^2 + ' + str(b) + ' x + ' + str(c) + ' are : ')print('\tx1 = ' + str(x1))print('\tx2 = ' + str(x2))
Output:-
How many equations would you like to solve today : 2Solving equation #1------------------------------Please enter your value of a(coefficient of x^2) : 2Please enter your value of b(coefficient of x) : 4Please enter your value of c(coefficient) : 2The solution to 2.0x^2 + 4.0 x + 2.0 are :x1 = (-1+0j)x2 = (-1+0j)Solving equation #2------------------------------Please enter your value of a(coefficient of x^2) : 3Please enter your value of b(coefficient of x) : 5Please enter your value of c(coefficient) : 2The solution to 3.0x^2 + 5.0 x + 2.0 are :x1 = (-0.6666666666666666+0j)x2 = (-1+0j)
Program to convert temperature from degree fahrenhite to degree celcius and degree kelvin.
f = float(input('What is the given temperature in degrees Fahrenheit : '))k = (f+459.67)*5/9c = (f-32)*5/9f = round(f,4)c = round(c,4)k = round(k,4)print('\nDegrees Fahrenheit :\t\t'+ str(f))print('Degrees Celsius :\t\t'+ str(c))print('Degrees Kelvin :\t\t'+ str(k))
Output:-
What is the given temperature in degrees Fahrenheit : 36Degrees Fahrenheit : 36.0Degrees Celsius : 2.2222Degrees Kelvin : 275.3722
Program to print multiplication and exponential table.
n = float(input('What number would you like to work with : '))print('Multiplication Table : \n')for i in range(1,10):print('\t' + str(i) + ' * ' + str(n) + ' = ' + str(round(i*n,2)))print('Exponential Table : \n')for i in range(1,10):print('\t' + str(i) + ' ** ' + str(n) + ' = ' + str(round(i**n,4)))
Output:-
What number would you like to work with : 3.6Multiplication Table :1 * 3.6 = 3.62 * 3.6 = 7.23 * 3.6 = 10.84 * 3.6 = 14.45 * 3.6 = 18.06 * 3.6 = 21.67 * 3.6 = 25.28 * 3.6 = 28.89 * 3.6 = 32.4Exponential Table :1 ** 3.6 = 1.02 ** 3.6 = 12.12573 ** 3.6 = 52.19594 ** 3.6 = 147.03345 ** 3.6 = 328.3166 ** 3.6 = 632.91377 ** 3.6 = 1102.43498 ** 3.6 = 1782.88769 ** 3.6 = 2724.4136
Comments
Post a Comment