Basic Python Prorams

Python Programs


1.Python Program to add two numbers:

num1=int(input("Enter First number\n"))
num2=int(input("Enter Second number\n"))
sum=num1+num2
print(sum)
Output:
Enter First number
10
Enter Second number
10
20

2.Maximum of two numbers in Python:

def maximum(a,b):
    if (a>=b):
        return a
    else:
        return b
a=2
b=5
print(maximum(a,b))
Output:
5

Using max() function:

a=2
b=5
maximum=max(a,b)
print(maximum)
Output:
5

3.Python Program for factorial of a number:

num=int(input("Enter number\n"))
fact=1
for i in range(1,num+1):
        fact=fact*i
print(fact)
Output:
Enter number
5
120

4.Python Program to check Armstrong Number:

num=int(input("Enter number\n"))
sum=0
temp=num
while(temp>0):
    rem=temp%10
    c=rem*rem*rem
    sum=sum+c
    temp=temp//10    
if(num==sum):
    print(num,"is an Armstron number")
else:
    print(num,"is not an Armstron number")
Output:
Enter number
153
153 is an Armstron number
Enter number
123
123 is not an Armstron number

5.Python Program to print all prime numbers:

num=int(input("Enter number\n"))
for i in range(2,num):
        for j in range(2,i):
            if(i%j==0):
                break
        else:
                print(i)
Output:
Enter number
10
2
3
5
7

6.Python Program to check wheter a number is Prime or not:  

num=int(input("Enter number\n"))
if(num>1):
    for i in range(2,num):
        if(num%i==0):
            print(num,"is not a prime number")
            break
        else:
            print(num,"is a prime number")
            break
else:
    print(num,"is not prime number")
Output:
Enter number
5
5 is a prime number
Enter number
12
12 is not a prime number

7.Python Program for n-th Fibonacci number:

def fibonacci(n):
    a=0
    b=1
    if n<0:
        return a
    elif n==1:
        return b
    else:
        for i in range(2,n):
            c=a+b
            a=b
            b=c
        return b
print(fibonacci(9))
Output:
21

8.Program to print ASCII value of a character

c='V'
print("The ASCII value of " +c+ " is", ord(c))
Output:
The ASCII value of V is 86

9.Python Program for Sum of squares of first n natural numbers:

num=int(input("Enter number\n"))
def squaresum(num):
    sum=0
    for i in range(1,num+1):
        square=i*i
        sum=sum+square
    return sum
print(squaresum(num))
Output:
Enter number
4
30
Enter number
5
55

10.  Python Program for cube sum of first natural numbers:

num=int(input("Enter number\n"))
def cubesum(num):
    sum=0
    for i in range(1,num+1):
        cube=i*i*i
        sum=sum+cube
    return sum
print(cubesum(num))
Output:
Enter number
5
225
Enter number
25
105625
    

11.Python program for Simple Interest

p=int(input('Enter principal\n'))
t=int(input('Enter time\n'))
r=int(input('Enter rate of interest\n'))
def simple_interest(p,t,r):
    si=(p*t*r)/100
    print('The Simple Interest is', si)
    return si
simple_interest(p,t,r)
Output:
Enter principal
5
Enter time
5
Enter rate of interest
5
The Simple Interest is 1.25

12.Python Program to check if a number is Positive, Negative or Zero:

num=int(input("Enter number\n"))
if num>0:
    print("Positive number")
elif num==0:
    print("Zero")
else:
    print("Negative number")
Output:
Enter number
2
Positive number
Enter number
-5
Negative number
Enter number
0
Zero

13.Python Program to find te Factors of a Number

num=int(input("Enter number\n"))
def factor_numbers(num):
    for i in range(1,num+1):
        if(num%i==0):
            print(i)
factor_numbers(num)
Output:
Enter number
25
1
5
25

14.Python Program to find CF or GCD

x=int(input("Enter x\n"))
y=int(input("Enter y\n"))
def hcf(x,y):
    if(x>y):
        m=y
    else:
        m=x
    for i in range(1,m+1):
        if((x%i==0)and(y%i==0)):
            hcf=i
    return hcf
print("GCD of two number is ", hcf(x,y))
Output:
Enter x
2
Enter y
4
GCD of two number is  2

15.Python Program to Find the Largest Among three numbers:

num1=int(input("Enter First number\n"))
num2=int(input("Enter Second number\n"))
num3=int(input("Enter Third number\n"))
if(num1>=num2)and(num1>=num3):
    largest=num1
elif(num2>=num1)and(num2>=num3):
    largest=num2
else:
    largest=num3
print("The Larest number is", largest)
Output:
Enter First number
5
Enter Second number
2
Enter Third number
97
The Larest number is 97