Basic Python Programs

1. Maximum of two numbers in Python.

Method #1: This is the native approach where we will compare tow numbers using if-else statement and will print the output accordingly.

Example:

# Python program to find the
# maximum of two numbers
 def maximum(a, b):
       if a >= b:
              return a
        else:
              return b
 
# Driver code
a = 2
b = 4
print(maximum(a, b))

2. Python Program for factorial of a number

# Python 3 program to find 
# factorial of given number
def factorial(n):
           if n < 0:
                  return 0
           elif n == 0 or n == 1:
                  return 1
         else:
                   fact = 1
                  while(n > 1):
                         fact *= n
                         n -= 1
                return fact
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))

3. Python Program for simple interest

Simple interest formula is given by:

Simple Interest = (P x T x R)/100

Where,

P is the principle amount

T is the time and

R is the rate

# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.
 
 
def simple_interest(p,t,r):
print('The principal is', p)
print('The time period is', t)
print('The rate of interest is',r)
 
si = (p * t * r)/100
 
print('The Simple Interest is', si)
return si
 
# Driver code
simple_interest(8, 6, 8)

4. Python Program for compound interest

Formula to calculate compound interest annually is given by:

A = P(1 + R/100) t

Compound Interest = A – P

Where,

A is amount

P is principle amount

R is the rate and

T is the time span

Example:

Input : Principle (amount): 1200
        Time: 2
        Rate: 5.4
Output : Compound Interest = 1333.099243
# Python3 program to find compound
# interest for given values.
 
def compound_interest(principle, rate, time):
 
# Calculates compound interest 
Amount = principle * (pow((1 + rate / 100), time))
CI = Amount - principle
print("Compound interest is", CI)
 
# Driver Code 
compound_interest(10000, 10.25, 5)

5. Python Program to check Armstrong Number

Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits)

Example:

Input : 153
Output : Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

Input : 120
Output : No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

Input : 1253
Output : No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

Input : 1634
Output : Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
# Function to check whether the given 
# number is Armstrong number or not
def isArmstrong(x):
 
n = order(x)
temp = x
sum1 = 0
 
while (temp != 0):
r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp // 10
 
# If condition satisfies
return (sum1 == x)
 
# Driver code
x = 153
print(isArmstrong(x))
 
x = 1253
print(isArmstrong(x))

6. Python Program for Program to find area of a circle

Area of a circle can simply be evaluated using following formula.

Area = pi * r2
where r is radius of circle 
# Python program to find Area of a circle
 
def findArea(r):
PI = 3.142
return PI * (r*r);
 
# Driver method
print("Area is %.6f" % findArea(5));

7. Python program to print all Prime numbers in an Interval

# Python program to print all 
# prime number in an interval
# number should be greater than 1
start = 11
end = 25
 
for i in range(start, end+1):
if i>1:
for j in range(2,i):
if(i % j==0):
break
else:
print(i)

Output: 
 

11
13
17
19
23

8. Python program to check whether a number is Prime or not

Given a positive integer N, The task is to write a Python program to check if the number is prime or not.
Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.

Examples : 

Input:  n = 11
Output: true

Input:  n = 15
Output: false

Input:  n = 1
Output: false

# A optimized school method based
# Python3 program to check
# if a number is prime
 
 
def isPrime(n) :
 
# Corner cases
if (n <= 1) :
return False
if (n <= 3) :
return True
 
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0) :
return False
 
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
 
return True
 
 
# Driver Program
if (isPrime(11)) :
print(" true")
else :
print(" false")
 
if(isPrime(15)) :
print(" true")
else :
print(" false")
 

9. Python Program for n-th Fibonacci number.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation 

Fn = Fn-1 + Fn-2

With seed values  

F0 = 0 and F1 = 1.
# Function for nth Fibonacci number
 
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
 
# Driver Program
 
print(Fibonacci(9))

10. Python Program for Sum of squares of first n natural numbers

Given a positive integer N. The task is to find 12 + 22 + 32 + ….. + N2.

Examples:

Input : N = 4
Output : 30
12 + 22 + 32 + 42
= 1 + 4 + 9 + 16
= 30

Input : N = 5
Output : 55

Method 1: The idea is to run a loop from 1 to n and for each i, 1 <= i <= n, find i2 to sum.

# Python3 Program to
# find sum of square
# of first n natural 
# numbers
 
 
# Return the sum of
# square of first n
# natural numbers
def squaresum(n) :
 
# Iterate i from 1 
# and n finding 
# square of i and
# add to sum.
sm = 0
for i in range(1, n+1) :
sm = sm + (i * i)
 
return sm
 
# Driven Program
n = 4
print(squaresum(n))

Output:

30