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 Blog

Hello Passable Blog
Please enter a message : This is Passable Blog. Here are some python programs. 
Which letter would you like to count the occurences of : p
Your 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 CALCULATOR

def add(num1,num2):
    result=num1+num2
    print('Addition : '+ str(result))
    
def sub(num1,num2):
    result=num1-num2
    print('Subtraction : '+ str(result))

def mul(num1,num2):
    result=num1*num2
    print('Multiplication : '+ str(result))

def div(num1,num2):
    result=num1/num2
    print('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:
            break
    else :
        print('You entered a wrong choice')

Output:- 
 
Python Calculator : 
1.Addition
2.Substraction
3.Multiplication
4.Division
5.Exit
 
Enter your choice : 1
Enter first number : 2
Enter second number : 3
Addition : 5
 
Enter your choice : 2
Enter first number : 9
Enter second number : 6
Subtraction : 3
 
Enter your choice : 3
Enter first number : 6
Enter second number : 5
Multiplication : 30
 
Enter your choice : 4
Enter first number : 6
Enter second number : 3
Division : 2.0
 
Enter your choice : 5
       

Program to solve quadratic equations.

import cmath

n = 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 : 2
Solving equation #1
------------------------------

Please enter your value of a(coefficient of x^2) : 2
Please enter your value of b(coefficient of x) : 4
Please enter your value of c(coefficient) : 2
The 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) : 3
Please enter your value of b(coefficient of x) : 5
Please enter your value of c(coefficient) : 2
The 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/9
c = (f-32)*5/9
f = 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 : 36

Degrees Fahrenheit : 36.0
Degrees Celsius : 2.2222
Degrees 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.6
Multiplication Table : 

1 * 3.6 = 3.6
2 * 3.6 = 7.2
3 * 3.6 = 10.8
4 * 3.6 = 14.4
5 * 3.6 = 18.0
6 * 3.6 = 21.6
7 * 3.6 = 25.2
8 * 3.6 = 28.8
9 * 3.6 = 32.4
Exponential Table : 

1 ** 3.6 = 1.0
2 ** 3.6 = 12.1257
3 ** 3.6 = 52.1959
4 ** 3.6 = 147.0334
5 ** 3.6 = 328.316
6 ** 3.6 = 632.9137
7 ** 3.6 = 1102.4349
8 ** 3.6 = 1782.8876
9 ** 3.6 = 2724.4136

Comments

Popular posts from this blog

Some tips about the right way to charge your phone

Software Basics

Some unique and weird things about Japan