Code Decode

  Python Basic Programs

 

1. Python program to add two numbers

# Python3 program to add two numbers

num1 = 15
num2 = 12

# Adding two nos
sum = num1 + num2

# printing values
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))   


2.Python Program for factorial of a number

# Python 3 program to find
# factorial of given number
def factorial(n):
   
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1);

# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))

3. Python Program for simple interest

# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.


# We can change values here for
# different inputs

P = 1
R = 1
T = 1

# Calculates simple interest
SI = (P * R * T) / 100

# Print the resultant value of SI
print("simple interest is", SI)

4. Python Program for compound interest

# Python3 program to find compound
# interest for given values.


def compound_interest(principle, rate, time):

    # Calculates compound interest
    CI = principle * (pow((1 + rate / 100), time))
    print("Compound interest is", CI)

# Driver Code
compound_interest(10000, 10.25, 5)

5. Python Program for Program to find area of a 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));

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

# Python program to print all
# prime number in an interval

start = 11
end = 25

for val in range(start, end + 1):
   
# If num is divisible by any number
# between 2 and val, it is not prime

if val > 1:
    for n in range(2, val):
        if (val % n) == 0:
            break
    else:
        print(val)

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

# Python program to check if
# given number is prime or not

num = 11

# If given number is greater than 1
if num > 1:
   
# Iterate from 2 to n / 2
for i in range(2, num//2):
       
    # If num is divisible by any number between
    # 2 and n / 2, it is not prime

    if (num % i) == 0:
        print(num, "is not a prime number")
        break
else:
    print(num, "is a prime number")

else:
print(num, "is not a prime number")

8. Python Program for n-th Fibonacci number

 # 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))

9. Python Program for Fibonacci numbers

# 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 to check Armstrong Number

 # Python program to determine whether the number is
# Armstrong number or not

# Function to calculate x raised to the power y
def power(x, y):
    if y==0:
        return 1
    if y%2==0:
        return power(x, y/2)*power(x, y/2)
    return x*power(x, y/2)*power(x, y/2)

# Function to calculate order of the number
def order(x):

    # variable to store of the number
    n = 0
    while (x!=0):
        n = n+1
        x = x/10
    return n

# 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 Program
x = 153
print(isArmstrong(x))
x = 1253
print(isArmstrong(x)

 

Python List Programs

 

1. Python Program to find sum of elements of list

# Python 3 code to find sum
# of elements in given array
def _sum(arr,n):
    
    # return sum using sum
    # inbuilt sum() function
    return(sum(arr)) 

# driver function
arr = [] 

# input values to list
arr = [12, 3, 4, 15] 

# calculating length of array
n = len(arr)
ans = _sum(arr,n) 

# display sum
print (\'Sum of the array is \',ans) 


2. Python Program to find largest element in an array

# Python3 program to find maximum
# in arr[] of size n
# python function to find maximum
# in arr[] of size n

def largest(arr,n):
    # Initialize maximum element
    max = arr[0]
    
    # Traverse array elements from second
    # and compare every element with
    # current max
    for i in range(1, n):
        if arr[i] > max:
            max = arr[i]
    return max

# Driver Code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
Ans = largest(arr,n)
print ("Largest in given array is",Ans)


1 comment:

  1. Your post is very great.I read this post. It’s very helpful. I will definitely go ahead and take advantage of this. You absolutely have wonderful stories. Cheers for sharing with us your blog. For more learning about data science visit at Data science course in Bangalore

    ReplyDelete