for count in range(0,100,2):
print 'For:',count
count1 =0
while(count1<100):
print 'While:',count1
count1=count1+2
Solve Problems by Coding Solutions - A Complete solution for python programming
Evaluating expression in Python
x=2
y=3
z=2
print x**y**z #Answer 512
print (x**y)**z #Answer 64
print x**(y**z) #Answer 512
y=3
z=2
print x**y**z #Answer 512
print (x**y)**z #Answer 64
print x**(y**z) #Answer 512
Perimeter and area of a circle and finding Natural Exponential Function
import math
r=int(raw_input('Enter the radius of circle:'))
perimeter=2*math.pi*r
print 'Perimeter of a circle :',perimeter
area=math.pi*(r**2)
print 'Area of acircle :',area
x=int(raw_input('Enter the value of x in y=e**x:'))
y=math.e**x
print 'Natural Exponential Function value :',y
r=int(raw_input('Enter the radius of circle:'))
perimeter=2*math.pi*r
print 'Perimeter of a circle :',perimeter
area=math.pi*(r**2)
print 'Area of acircle :',area
x=int(raw_input('Enter the value of x in y=e**x:'))
y=math.e**x
print 'Natural Exponential Function value :',y
Sorting the numbers in the List
L=int(raw_input("Enter the length of list: "))
p=list()
for i in range(L):
num=int(raw_input("Enter the elements : "))
p.append(num)
print '\nUnsorted list is: ',p
for i in range (len(p)):
for j in range (i+1,len(p)):
if p[i]>p[j]:
t=p[i]
p[i]=p[j]
p[j]=t
else:
print '\nSorted List is:' ,p
p=list()
for i in range(L):
num=int(raw_input("Enter the elements : "))
p.append(num)
print '\nUnsorted list is: ',p
for i in range (len(p)):
for j in range (i+1,len(p)):
if p[i]>p[j]:
t=p[i]
p[i]=p[j]
p[j]=t
else:
print '\nSorted List is:' ,p
Search the Element in a list
flag=0
L=int(raw_input("Enter the length of list: "))
p=list()
for i in range(L):
num=int(raw_input("Enter the elements : "))
p.append(num)
j=int(raw_input("Enter the element to find: "))
for i in range(L):
if (p[i]==j):
flag=1
if flag==1:
print "Element is found"
else:
print "Element is not found"
L=int(raw_input("Enter the length of list: "))
p=list()
for i in range(L):
num=int(raw_input("Enter the elements : "))
p.append(num)
j=int(raw_input("Enter the element to find: "))
for i in range(L):
if (p[i]==j):
flag=1
if flag==1:
print "Element is found"
else:
print "Element is not found"
Delete Duplicate numbers in a list
l1=list()
l2=list()
w=int(raw_input("Enter the elements in list:"))
for i in range(w):
n=int(raw_input("Enter the list:"))
l1.append(n)
for i in l1:
if i not in l2:
l2.append(i)
print ' Delete Duplicate numbers in a list: ', l2
l2=list()
w=int(raw_input("Enter the elements in list:"))
for i in range(w):
n=int(raw_input("Enter the list:"))
l1.append(n)
for i in l1:
if i not in l2:
l2.append(i)
print ' Delete Duplicate numbers in a list: ', l2
Program to check the Perfect Number or Not
num=input("Enter the number:")
i=1
while i<=num/2:
s=i**2
if s==num:
print "perfect square"
break
i+=1
else:
print "Not a perfect square"
i=1
while i<=num/2:
s=i**2
if s==num:
print "perfect square"
break
i+=1
else:
print "Not a perfect square"
Program to find even number using functions without return
def even(n):
if n%2==0:
print 'Even number'
else:
print 'Odd Number'
n=int(raw_input('Enter the number to check even or not:'))
even(n) # No return from function but the message is displayed inside the function itself
if n%2==0:
print 'Even number'
else:
print 'Odd Number'
n=int(raw_input('Enter the number to check even or not:'))
even(n) # No return from function but the message is displayed inside the function itself
Simple Calculator using functions
def su(a,b):
c=a+b
print 'Added value is:',c
def subt(a,b):
if a>b:
c=a-b
print 'Subtracted value is:',c
else:
c=b-a
print 'Subtracted value is:',c
def mul(a,b):
c=a*b
print 'Multiplication value is:',c
def div(a,b):
if b==0:
print 'Division by zero not defined'
else:
c=float(a)/b
print 'Divison value is:',c
def exp(a,b):
c=a**b
print 'Exponent value is:',c
a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add 2.sub 3.mul 4. div 5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
su(a,b) # Function call
elif n==2:
subt(a,b) # Function call
elif n==3:
mul(a,b) # Function call
elif n==4:
div(a,b) # Function call
elif n==5:
exp(a,b) # Function call
else:
print 'Invalid options'
c=a+b
print 'Added value is:',c
def subt(a,b):
if a>b:
c=a-b
print 'Subtracted value is:',c
else:
c=b-a
print 'Subtracted value is:',c
def mul(a,b):
c=a*b
print 'Multiplication value is:',c
def div(a,b):
if b==0:
print 'Division by zero not defined'
else:
c=float(a)/b
print 'Divison value is:',c
def exp(a,b):
c=a**b
print 'Exponent value is:',c
a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add 2.sub 3.mul 4. div 5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
su(a,b) # Function call
elif n==2:
subt(a,b) # Function call
elif n==3:
mul(a,b) # Function call
elif n==4:
div(a,b) # Function call
elif n==5:
exp(a,b) # Function call
else:
print 'Invalid options'
Sum of digits of the a given number
n=int(raw_input('Enter the number: '))
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=n/10
print 'Sum of digits of the number is: ',sum
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=n/10
print 'Sum of digits of the number is: ',sum
Subscribe to:
Posts (Atom)