Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Conditions

 Conditions:

                   This chapter is about a concept that will make our script more interesting so far the interpreter always execute one command after the other with condition this changes

                        A program that has conditional  is called conditional program and the process is known as conditional programming

        IF , ELIF , ELSE

Basically a condition need to return true so that our script continues with the code in its block

IF -  In this the code will be executed only when the if condition is true

ELSE - In this it consist of 2 segments if the  condition is true one segment will  be                            executed or else the other Segment will be executed

Example:

            


Here when i 1st run the program i get an error called IndentationError. In python indentation is very important

Example:

a = 4
if a%2==0:
     print("a is even number")
elif a==0:
     print("a is zero")
else:
     print("a is odd number")

The important keywords here are if, elif and else. In this variable a is assigned as 4.Our first if-statement check if the member is even or not by dividing it with two. Remember that comparison always return true or false. If the return is true then the code is executed

If the return is false it continues to elif-statement and check if this condition is met. The same procedure happens here. You can have as many elif block as you want. If again the condition return false we get into the else block

Output:

ELIF


NESTED IF-STATEMENTS:

      A condition inside another condition same as if-block inside another if-block. This is called nested if- statement

Example:  (check indentation )

number = 4
if number % 2 == 0 :
   if number == 0 :
        print ( "Your number is even but zero" )
   else :
        print ( "Your number is even" )
else :
   print ( "Your number is odd" )

Output: Your number is even

So, here we have the first condition, which checks if the number is even.
When the number is even it then checks if it’s a zero or not. That’s a trivial example
but you get the concept









Comments

Popular Posts