Sunday, 30 October 2016

Example of list comprehension

#Example of list comprehension


str=input("enter string:")

l=[i for i in str if i in ('a','e','i','o','u')]
print("number of vowels present in entered string:",len(l))

Saturday, 29 October 2016

check armstrong number or not...


# Python program to check if the number provided by the user is an Armstrong number or not...


n = int(input("Enter a number: "))
sum = 0
t = n
while t > 0:
   r = t % 10
   sum += r ** 3
   t //= 10
if n == sum:
   print(n,"is an Armstrong number")
else:
   print(n,"is not an Armstrong number")




Wednesday, 12 October 2016

Fibonacci series using while loop

n=int(input("enter no:"))
n0=1
n1=1
count=2
if n<=0:
    print("Please enter positive integer")
elif n==1:
    print("fibonacci series:",n0)
else:
    print("fibonacci series:",n0,n1,'',end='')
    while count<n:
        nth=n0+n1
        print(nth,'',end='')
        n0=n1
        n1=nth
        count+=1

Monday, 10 October 2016

Dictionary for days of week & temperature

#program to accept the day of week & temperature of that day and display them in the form of dictionary


d={} 
ch='y'
for i in range(7):
    day=input('enter day:')
    temp=float(input("enter temperature:"))
    d[day]=temp
print('DAY & TEMPERATURE')
print(d)

Wednesday, 5 October 2016

dictionary for student's roll no. and name

#program to accept roll no & name of students as many as user want & store them into a dictionary

d={}
ch='y'
while ch!='n':
    rno=int(input('enter roll number:'))
    sname=input("enter student's name:")
    ch=input('do you want to continue...?(y/n)')
    d[rno]=sname
print('list of students')
print(d)

Sunday, 2 October 2016

prime numbers from upper range to lower range

a=int(input("enter lower range="))
b=int(input("enter upper range="))

for num in range(a,b+1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)

To check number is palindrome or not


n=int(input('enter no:'))
rev=0;t=n
while(n!=0):
    r=n%10
    n=n//10
    rev=rev*10+r
if(t==rev):
    print('the number is palindrome')
else:
    print('no palindrome')