Showing posts with label Learn Python Online. Show all posts
Showing posts with label Learn Python Online. Show all posts

Thursday, 8 November 2012

Check Given Number is Prime or Not?


# This is the second method to check the given number is Prime Number or Not?

count=0
num=input('Enter Any +ve Integer: ')
for i in range(1,num+1/2):
    if num%i==0:
        count=count+1
if count==1:
    print num , 'is Prime Number....'
else:
    print num, 'is Not Prime Number.....'

Prime Number.


# Check the given number is prime or Not???
count=0
num=input('Enter Any +ve Integer: ')
for i in range(1,num+1):
    if num%i==0:
        count=count+1
if count==2:
    print num , 'is Prime Number....'
else:
    print num, 'is Not Prime Number.....'

Tuesday, 6 November 2012

Find Leap Year using NOT Operator?

#leap Years are those who can be completely divisible by 4. On the other hand those years in which Month February contains 29 Days. like in 2012.

year=input('Enter An Year: ')
if not year%4==0:
    print year,'is not a Leap Year.'
else:
    print year,'is A leap Year.'

Calculate sum,Average and Number of Even and odds in given length Using For loop


# Get Length from user and calculate the number of even and odds , sum of even and odd and average of even and odds separately.

even_sum=0
even_counter=0
odd_sum=0
odd_counter=0
print 'This Program will calculate the averge of Even Numbers from which length you will give.'
length=input('Enter Number upto which you want to get Average: ')
print'\n'
for i in range(0,length):
    if i%2==0:
        even_sum=even_sum+i
        even_counter=even_counter+1
    if i%2==1:
        odd_sum=odd_sum+i
        odd_counter=odd_counter+1
print 'Number Of Odds is =',odd_counter
print 'Sum is =',odd_sum
odd_avg=odd_sum/odd_counter
print 'Averge of', odd_counter, 'Odd(s) is =',odd_avg
print '\n'
print 'Number Of Even is =',even_counter
print 'Sum is =',even_sum
even_avg=even_sum/even_counter
print 'Averge of', even_counter, 'Even(s) is =',even_avg

Sum and Number of Even's in 1 to 50 using If and For Loop?


sum=0
counter=0
for i in range(0,50):
    if i%2==0:
        sum=sum+i
        counter=counter+1
print 'Number Of Even is =',counter
print 'Sum is =',sum

Get length From User And Print Table using For Loop?



num=input('Enter Number For Table: ')
length=input('Enter Length of Table: ')
length=length+1
for i in range (1,length):
    print num,'*',i,'=',num*i

Factorial of Given Number Using While Loop?


n=input('Enter +ve Integer: ')
fact=1
i=1
while (i<=n):
    fact=fact*i
    i=i+1
print 'Factorial is: ',fact

Find Factorial of Given Number Using For loop?


# This is code to generate factorial of given number

n=input('Enter number For Factorial: ')
fact=1
#i=1
for i in range(1,n + 1):
    fact=fact*i
print 'Factorial is: ',fact

Table using For Loop


#Table of Given Number using for loop
n=input('Enter An Interger: ')
for i in range (1,11):
    print n, '*' , i ,'=',n*i

Find Factorial Using For Loop...


n=input('Enter number For Factorial: ')
fact=1
for i in range(1,n + 1):
    fact=fact*i
print 'Factorial is: ',fact

For Loop

The For statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
here is the simple example of for loop:


for i in range(1,11):
    print i

This is simple code of For loop and this will print the first ten natural numbers.
Here," for" is the keyword "in range()" is the function for the range of loop. Here the range of loop is 1 to 11 because for loop work with only up to n-1. Here n can be a number which stores the rang of loop given by user.

Check Even or Odd with Logical Operator NOT


#Here is the Working of Not opertator..........

num=input('Enter A Number: ')
if not num%2==0:
     print num, 'is Odd.'
else:
    print num, 'is Even.'

Find Vowel and Consonant via logical operator OR?


# Here is the Working of OR operator...


charr=raw_input('Enter An alphabet ')
if charr=='a' or charr=='A':
    print 'You enterd vowel',charr
elif charr=='e' or charr=='E':
    print 'You enterd vowel',charr
elif charr=='i' or charr=='I':
    print 'You enterd vowel',charr
elif charr=='o' or charr=='O':
    print 'You enterd vowel',charr
elif charr=='u' or charr=='U':
    print 'You enterd vowel',charr
else:
    print 'You Entered Consonant',charr

Find Greater Number Via Logical Operator AND?


#Here is the use of And operator in Detail...........

n1=input('Enter First Number: ')
n2=input('Enter Second Number: ')
n3=input('Enter Third Number: ')
if n1>n2 and n1> n3:
    print n1, 'is greater.'
elif n2>n3 and n2>n1:
    print n2, 'is greater.'
elif n3> n1 and n3>n2:
    print n3, 'is greater.'
else: print 'Numbers are same.'

Logical Operators

Logical operators are those which help for putting limits in conditions.
Such that if you want to put a limit in if statement of 1 to 20 and 20 to 30. There we will use logical operators. There are Three types of Logical Operators:


  1. And Operator.
  2. Or Operator.
  3. Not Operator.

Syntax of AND Operator:

                          AND operator works when both the given conditions are true and this will give the true. syntax is given:
                                if Firstcondition and Secondcondition:
Here the and is working as and operator.

Syntax of OR Operator:

                         OR operator works if one condition is true from given conditions and this gives a true. Syntax is given:
                             if Firstconditon or secondcondition:
Here the or  working as or operator.

Syntax of NOT Operator:

                        NOT operator only swap the condition, Mean if the condition is true it will make it false and if false then it will make it true.And not operator comes before condition. Syntax is given as
                              if not condition:
Here not is working as NOT operator.

Calculate House Rent And Convince Allowance


# This code is to calculate house rent and convince allowance bonus and adding to your salary

salary=input('Enter Salary: ')
#Here i have assign values to house_rent and convince allownce to make them local not global variables. if i not give them values then in else condition this will give some errors and will become odd type of program.
house_rent=0
convince_allownce=0
if salary >=20000:
    house_rent=salary/100*50
    convince_allownce=salary/100*20
elif salary >=10000:
    house_rent=salary/100*30
    convince_allownce=salary/100*15
elif salary >=7000:
    house_rent=salary/100*10
    convince_allownce=salary/100*5
else:
    print 'You Are Unable to get Bonus....!!!'
   
print 'Your House Rent Bonus is: ', house_rent
print 'Your Convince Allownce Bonus is: ', convince_allownce
nsalary=salary+house_rent+convince_allownce
print 'Your Salary is: ', nsalary

Sum of Even Numbers via For loop


sum=0
for i in range(1,50):
    if i%2==0:
        sum=sum+i
print sum

Sum of odd numbers via for loop


The sum of all odd numbers from 1 to 50. Using for loop.

sum=0
for i in range(1,50):
    if i%2==1:
        sum=sum+i
print sum

Saturday, 3 November 2012

Check given number is even or Odd with If-else


## this program working with if-else statement..


num1=input('Emter Number: ')
if num1%2==0:
    print num1, 'is Even.'
else:
    print num, 'is odd.'

Conditional Statements

Conditional Statements:

    Conditional Statements are those in which simple statements works according to given condition. If one condition become fail then pointer move to second condition, if that condition fails too then it moves forward toward other conditions until it reach to true condition and print statements against that condition.

  1. If-else Statement.
  2. Nested If-else Statement
  3. Conditional Operator
  4.  Switch Statement