Posts

Image
  S ort a given data in ascending order  USING NESTED LOOP: a=eval(input("a:")) for i in range(len(a)):     for j in range(len(a)-i-1):         if a[j]>a[j+1]:             a[j],a[j+1]=a[j+1],a[j] print(a) OUTPUT:

Python code to search a number in a sorted list

Image
 To search a number in a sorted list USING WHILE LOOP: a=eval(input("Insert a list: ")) val=int(input("value to find:  ")) s=0 e=len(a)-1 while s<=e:     m=(s+e)//2     if val==a[m]:         print(val,"found at",m)         break     elif a[m]<val:         s=m+1     else:         e=m-1 else:     print(val,"not found") OUTPUT:

Python program to check a number is prime or not

Image
 CHECK WHETHER A NUMBER IS PRIME OR NOT  USING FOR LOOP: n=int(input("Enter a number:")) if n==1:     print("neither a prime nor composite") elif n>1:     for i in range(2,n):         if n%i==0:             print("It is not a prime")             break      else:             print("It is a prime")              OUTPUT:         

Python program to find hcf of two numbers

Image
TO FIND HCF OF TWO GIVEN NUMBERS  USING WHILE LOOP: a=int(input("dividend")) b=int(input("divisor:")) rem=1 if a>b:     while rem>0:         if a%b==0:             print("HCF is",b)             break         else:             rem=a%b             a=b             b=rem else:     print("dividend must be greater than divisor") OUTPUT:

PYTHON CODE TO REVERSE A GIVEN STRING

Image
  TO REVERSE A GIVEN STRING: METHOD -1 BY LOOPING AND JUST VIEWING THE REVERSE TEXT a=input("A:") j=len(a)-1 for i in range(len(a)-1,-1,-1):     print(a[i] ,end="") OUTPUT- METHOD-2 BY CREATING A NEW STRING  s=input("text:") c="" for i in range(len(s)-1,-1,-1):     y=s[i]     c=c+y print("The reverse of the given string is",c) OUTPUT-

Sum of the digits present in a list

Image
  PYTHON CODE FOR FINDING SUM OF DIGITS  PRESENT IN A STRING  METHOD-1 USING LOOP: a=input("Enter a string") s=0 for i in a:     if i.isdigit():        s=s+int(i) print("Sum of the digit present in the string is:",s) OUTPUT: METHOD-2 my converting a string in list: a=input("Enter a string") m=list(a) s=0 for i in m:     if i.isdigit():        s=s+int(i) print("Sum of the digit present in the string is:",s)