0% found this document useful (0 votes)
4 views

bibo

Python

Uploaded by

bala9486555779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

bibo

Python

Uploaded by

bala9486555779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Star pattern

i=1

while(i<=6):

for j in range(1,i):

print("*",end=' ')

print(end='\n')

i+=1

Fibonacci

nterms=int(input("Enter the no of terms needed: "))

n1,n2=0,1

count= 0

if nterms<=0:

print("Please enter a positive integer")

elif nterms==1:

print("Fibonacci sequence upto",nterms,":",n1)

else:

print("Fibonacci sequence:")

while count<nterms:

print(n1)

nth=n1+n2

n1=n2

n2=nth

count+=1

square root newton method

def newton_method(number,loop=500):

a = float(number);
for i in range(loop):

number = 0.5*(number+a/number)

return number

number=int(input("Enter the number :"));

print ("The square root value of",number,"is",newton_method(number));

perfect number

num=int(input())

sum_v=0

for i in range(1,num):

if (num%i==0):

sum_v=sum_v+i

if(sum_v==num):

print("Perfect number")

else:

print("Not a perfect number")

calculator

a=int(input("Enter the first number : "));

b=int(input("Enter the second number : "));

print("1.Addition");

print("2.Subtraction");

print("3.Multiplication");

print("4.Division");

print("5.Modulus");

choice=int(input("\nEnter the operation need to do: "));

if(choice==1):

c=a+b;
print("Addition of",a,"and",b,"is",c);

elif(choice==2):

c=a-b;

print("Subtraction of",a,"and",b,"is",c);

elif(choice==3):

c=a*b;

print("Multiplication of",a,"and",b,"is",c);

elif(choice==4):

c=a/b;

print("Division of",a,"and",b,"is",c);

elif(choice==5):

c=a%b;

print("Modulus of",a,"and",b,"is",c);

else:

print("Invalid number")

liner array search

def linear(arr,x):

for i in range(len(arr)):

if arr[i]==x:

return i

return -1

arr=[124,48,88,96,220,78,10,500]

print(arr)

x=int(input("Enter the number which one need to find: "))

print("The element is ",x," it index value is",str(linear(arr,x)))


swapping of element

n=int(input())

seat=[]

ele=input()

seat=ele.split()

i=0

while(i<n-1):

t=seat[i]

seat[i]=seat[i+1]

seat[i+1]=t

i=i+2

for i in range(0,n):

print(seat[i],end=" ")

binary search

def binary_search(list1, n):

low = 0 ,high = len(list1) – 1,mid = 0

while low <= high:

mid = (high + low) // 2

if list1[mid] < n:

low = mid + 1

elif list1[mid] > n:

high = mid - 1

else:

return mid

return -1

list1 = [12, 24, 32, 39, 45, 50, 54];print(list1)

n=int(input("Enter the element need to be searched:"))


result = binary_search(list1, n)

if result != -1:

print("Element is present at index", str(result))

prime numbers

def prime(n):

for number in range(1,n+1):

if number>1:

for i in range(2,number):

if (number%i)==0:

break

else:

print(number)

n=int(input("Enter the first N prime numbers :"))

prime(n)

matrix multiple

result=[[0,0,0],[0,0,0],[0,0,0]]

def Multiply(A,B):

for i in range(len(A)):

for j in range(len(B[0])):

for k in range(len(B)):

result[i][j]+=A[i][k]*B[k][j]

for r in result:

print(r)

return 0

A=[[1,2,3],[4,5,6],[7,8,9]]

B=[[1,2,3],[4,5,6],[7,8,9]]
print("Result:")

Multiply(A,B)

Argument

Import sys

n=len(sys.argv)

print(“Total arguments passed:”)

print(“name the python script”,sys.argv)

print(“argument passed”)

for i in range(1,n):

print(sys.argv[i],end=””)

sum=0

for i in range(1,n):

sum+=int(sys.argv[i])

print(“Result”,sum)

rev word

class python_string:

def __init__(self,inp_string):

self.s = inp_string

def reverse_words(self):

return ' '.join(reversed(s.split()))

s= input()

a = python_string(s)

print(a.reverse_words())

file handling

f=open("pp.txt",'x')
f.close()

f=open("pp.txt","w")

f.write("python programming")

f.close()

print("Reading the file")

f=open("pp.txt","r")

print(f.read())

f.close()

print("\nAppending the text")

f=open("pp.txt","a")

f.write(" lab")

f=open("pp.txt","r")

print(f.read())

f.close()

f1=open("new.txt",'x')

f1.close()

f1=open("new.txt","w")

f1.write("Hello everyone to the ")

f1.close()

f=open("pp.txt","r")

f1=open("new.txt","a")

for word in f:

f1.write(word)

f1.close()

print("\nAdding new file into the old file ")

f2=open("new.txt","r")

print(f2.read())

You might also like