•Problem specification –our approach must be to determine overall goals, but precise ones, and then slowly divide the work into smaller problems until they become of manageable size. •Program design –Each part of a large program must be well organized, clearly written, and thoroughly understood, •Choice of data structures –How they are arranged in relation... Continue Reading →
Reversing a list in python
def Reverse(lst): return [element for element in reversed(lst)] lst = [3,14,8,24,1] print(Reverse(lst))
List Length in Python
The length of a list can be calculated in Python by using various methods. The built-in len() method in Python is widely used to calculate the length of any sequential data type. It calculates the number of elements or items in a list and returns the same as the length of the list. l=[2,4,6,8,10] print("The length of... Continue Reading →
Swapping the elements in a list
def swap(list, postn1, postn2): list[postn1], list[postn2] = list[postn2], list[postn1] return list List = [24,1,11,9,30,31] pos1, pos2 = 1, 3 print(swap(List, pos1-1, pos2-1))
Interchange first and last elements in a list in python
def swap_num_List(Listofnum): size = len(Listofnum) temp = Listofnum[0] Listofnum[0] = Listofnum[size - 1] Listofnum[size - 1] = temp return Listofnum Listofnum = [34,14,8,11,10] print(swap_num_List(Listofnum))
Checking for monotonic array in Python
def isMonotonic(A): return (all(A[i] <= A[i + 1] for i in range(len(A) - 1)) or all(A[i] >= A[i + 1] for i in range(len(A) - 1))) # main A = [5,6,7,8,9,10,11,12] print(isMonotonic(A)) def isMonotonic(A): return (all(A[i] <= A[i + 1] for i in range(len(A) - 1)) or all(A[i] >= A[i + 1] for i in... Continue Reading →
Python program to print all the prime numbers in a given range
The prime numbers are whole numbers greater than 1, that have only two factors – 1 and the number itself. Prime numbers are divisible only by the number 1 or itself. examples: 2*1=2 and 1*2=2 start = int (input("Enter the lower range ")) end = int (input("Enter the upper range ")) for i in range(start,... Continue Reading →
Easy Python programs
Print the ASCII value of Characters charr =(input("Enter a charcater")) print("The ASCII value of '" + charr + "' is", ord(charr)) Find the roots of quadratic equation import cmath a = int(input("Enter the value of a ")) b = int(input("Enter the value of b ")) c = int(input("Enter the value of c ")) # calculating... Continue Reading →
Area and Circumference of circle in python
#area and circumference of circle rad=float(input("Enter the radius of the circle ")) area= 3.14*rad*rad print("Area of the circle of given radius is = ", area) circum= 2*3.14*rad print("Circumference of the circle of given radius is = ", circum)
Calculate Compound Interest using Python
Formula for compound interest : =final amount=initial principal balance=interest rate=number of times interest applied per time period=number of time periods elapsed def compound_interest(principle, rate, time): CI = principle * (pow((1 + rate / 100), time)) print("Compound interest : ", CI) p=int(input("Enter Principal amount: ")) r=int(input("Enter interest rate: ")) t=int(input("Enter time period: ")) compound_interest(p, r, t)
Calculate simple interest using python
Formula for simple interest is : (principal amount* rate of interest* time period)/100 SI= (P*R*T)/100 p=int(input("Enter the principal amount ")) r=int(input("Enter the interest rate per annum ")) t=int(input("Enter the time period in years ")) simple_interest=(p*r*t)/100 print("SIMPLE INTEREST = ", simple_interest)
Add all the numbers from 1 to n where, n is given by user.
sum=0 n=int(input("value of n: ")) for i in range(1,n+1): sum=sum+i print("SUM =",sum) OUTPUT
Print the table of n where, n is given by user.
num = int(input("Enter the number: ")) print("Multiplication Table of", num) for i in range(1, 11): print(num,"X",i,"=",num * i) OUTPUT
LOOP LEARNING : Loop, Increment, Decrement
Example: While Loop i=1 while i <= 10: print (i) i=i+1 OUTPUT Example: Range Function print ("range(10) --> ", list(range(10))) print ("range(0,20) --> ", list(range(0,20))) print ("range(10,20) --> ", list(range(10,20))) print ("range(0,20,2) --> ", list(range(0,20,2))) print ("range(-10,-20,2) --> ", list(range(-10,-20,2))) print ("range(-10,-20,-2) --> ", list(range(-10,-20,-2))) OUTPUT Examples: For Loop for i in range(0,10): print (i) OUTPUT for i in range(0,20,2): print (i) OUTPUT for i in range(0,-10,-1): print (i) OUTPUT Example: Print table of 5 for i in range(1,11): print (5," * ", i , " = ", i * 5) OUTPUT Example: Sum all numbers from 1 to 10 s=0 for i in range(1,11): s=s+i print ("Sum is --> ",s) OUTPUT
Python beginner’s programs.
(Without Inputs From User) "Hello World" in python. print ("Hello World") OUTPUT Adding two numbers in python. a = 10 b = 220 c = a + b print (a, ” + “, b, ” –> “, c) OUTPUT Concatenation of Strings. a = "Bhagat" b = "Singh" c = a + b print (a, ” + “, b, ” –> “, c) OUTPUT (With Inputs From User) Adding Two Numbers in python. a = int(input(“Enter First No: “)) b = int(input(“Enter Second No: “)) c = a + b print (a, ” + “, b, ” –> “, c) OUTPUT Concatenation Of Strings. a = input(“Enter First String: “) b = input(“Enter Second String: “) c = a + b print (a, ” + “, b, ” –> “, c) OUTPUT
Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
def fact(number): if number==1: return 1 else: return number*fact(number-1) n=int(input(“Enter the number:”)) print(“Factorial of the number:” ,fact(n)) OUTPUT:
Write a Python program to reverse a string.
def reverse(string): return string[::-1] s=input(“Enter the string:”) print(“Reverse of the string:” ,reverse(s)) OUTPUT
Write a Python function to multiply all the numbers in a list.
def prolist(lst): pro=1 for x in lst: pro=pro*x return pro lst1=list(map(int,input(“Enter elements of list:”).split())) print(“Product of elements:”,prolist(lst1)) OUTPUT
Write a Python function to sum all the numbers in a list.
def sumlist(lst): return sum(lst) lst1=list(map(int,input(“Enter elements of list:”).split())) print(“Sum of elements:”,sumlist(lst1)) OUTPUT
Write a Python function to find the Max of three numbers.
def max(a,b,c): if a>b and a>c: return a elif b>c: return b else: return c a,b,c=map(int,input(“Enter three numbers:”).split()) print(“Maximum of the three:”,max(a,b,c)) OUTPUT