Friday, January 10, 2014

Basic part-4 | Conditional Statements

Hello ... Friends, Today we are going to discus about Conditional statement, as from the name Conditional. means such type of statements used at/for any particular conditions.
In this tut we are going to discus about these topics :

if statements 
while loop
for loop 

So let's start one by one

If Condition :


if statement work when one condition match with our condition or not, if matches then work else check next condition & execute the following statements which are given bellow in the code
So here is the syntax for conditional statement :
if condition 1
   do that
   do that
elif condition 2
   do that
   do that
elif condition 3
   do that
   do that
else
   do that

                                     

Here i am using a text editor in my Linux environment i am using gedit , you can use other editor in your Linux environment here if you are using windows environment then you can use notepad or wordpad.

                    

Here the main thing to be noted that we can string comparisons, numeric value comparisons, or the statement which perform the true & false conditions. we can put them in this if else or if elis else conditions. 

while loop : 

while loop is used for the statements until the condition not meat with the defined conditions. when the conditions meat then we exit from the loop easily . else the loop is continue until you stop the conditions.Basically loops are the set of codes or block of codes which are repeating continue until the condition meat, So Let's see how it works

Syntax :

while condition
          do that
          do that
          .
          .
          .
          .
          do that until meat the condition
think what we need to break this while loop as we want to stop or we want to get out from the loop then what we can use. So here we are playing with some other statements along with while as :
break : break is used to get out from the innermost loop
continue : start the next pass of the innermost loop
pass : do nothing, just a placeholder

So Let's take an example one by one how while loop is working

         

this is a very simple script in this script we just define the age as variable, then we set loop for age & the condition is that age must be > 10 then the loop is ok but if condition fails then it exit or we can say when age is < 10 then it exit.
        
if we try to understand it more clearly then we are going to make it more complex i am using if else statement inside the while loop

                               

in the above code we are using while conditions simply with if else statement. this is same as our previous if else statement just the difference is we will executing these conditions in a loop until meet with our conditions.

      

Next we understand how to stop the infinite loop without conditions like if else . so here we can use else with the while statement . if we see more clearly this condition become nearly equal to if else condition, while else the only difference is of loop here we will continue until meet with our conditions.

      

 very simple code easy to understand & very clear to understand .we can see the output file too how our code working :")

       

So here is our exercise that :

1. How to stop while loop using break statement, 
2. How to use multiple loops & use the continue statements 

For loop 

Next we are going to study about the for loop,  for loop in python has the ability to iterate over the items of any sequence, like as list or a string.
Syntax :

for item in object :
      do something
      do something
let us understand how for loop is working
in this very easy code snippet i am just garbing a letters from the words & different different words from a list .

Here is our output of my code that how a function works, I hope you understand it well


Exercise :
1.Define a list & give the no. in list & try to find out the sum of the no.s in the list.
2.Define a item list & try to find the items in a list separately.

This is all coding from the basic everyone can understand about it i hope so you enjoy it :')

No comments:

Post a Comment