Skip to main content

Python | Control Flow Tools

Control Flow Tools

In this session we will see about control flow tools of python.

1)if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program.
For Example:-

>>> x = int(input("Please Enter an integer"))
Please Enter an integer 50
>>> if x<0:
    x=0
    print("Negative change to zero")
elif x==0:
print("Zero")
elif x==1:
print("One")
else:
print("More")

More
>>> 

Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.
    In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also false so it go to the else part of the program. If all conditions were false then else condition is true. So the output of the above program is More.
    You must have to complete if statement with the colon. The part inside the if statement must be proper intended. 

2) for Statements:- We use for statement in the programming for the loop. Now we see how to write for statement in python program.
For Example:-

>>> x = ['Python','Java','Php']
>>> for i in x:
     print(i)

Python
Java
Php

In python for statement is very easy to write. We write 'for i in x' in our program it means that it collect all the elements from the list x, separate the elements of the list and store it into i variable. When you print i inside the for loop it return the all elements of list separately. 
    In this program for loop iterate three times first time 'Python' is store in i and print it then 'Java' and then 'Php'. If the list has n elements then for loop iterates n times.

3) range() Function:- If you want to iterate a number then there is a built-in-function range() to iterate over a sequence.
For example:-

>>> for i in range(5):
         print(i)

0
1
2
3

In this program we use range() function. In the range function we insert value 5 that means it start from 0 to 4. If you write there 11 then it will start from 0 to 10.
In the range() function you can use start, stop and step attribute.
For example:-
>>> for i in range(5,10):
        print(i)

5
6
7
8
9

In this program value of start attribute is 5 and end attribute is 10 so it will return the numbers from 5 to 9. When you give the end value it will always take the value upto one number before it. For example if you give the end value 10 it will take value till 9. If you take end value 100 it will take 99.

4) enumerate(iterable,start=0) :- It returns an enumerate object, iterable must be sequence or some other object which support iteration. Enumerate returns the list of two tuple containing the index number and the values of that number. 
 
For example:-
>>> l=['a','b','c']
>>> print(list(enumerate(l)))
[(0, 'a'), (1, 'b'), (2, 'c')]

5) break statement:-  The break statement terminates the loop. 
For example:-
for val in "String":
if val == "i":
break
print(val)

S
t
r

In this example we iterate the string using for loop. The each element of string is store in val attribute. Inside the for loop we give the condition  if val == "i" that means when the value of val is become i the break the loop. 

6) continue statement:- The continue statement is used to skip the code inside the loop for the current iteration only.
For example:-

for val in "String":
if val == "i":
continue
print(val)

S
t
r
n
g

This is the same example of break statement. The difference is that instead of break statement we use continue statement. So you can see how the output change. In the output of continue statement i will be skip. 
We see another example of continue statement. 

for val in "Python":
        if val == "t":
                continue
        print(val)

P
y
h
o
n

Same thing happen in this example also.

7) pass Statement:- When the pass statement is executed nothing will happen. By using pass statement you can avoid getting error when empty code is not allowed in loops.
For example:-
for x in "String":
        pass

In this example, if you don't written any thing in the for loop then it gives error. But when you write pass inside the loop it not affect your code but it doesn't give any error.

Comments

Popular posts from this blog

Basic Knowledge of Python part 2

Strings using python - Other than numbers python can also handle strings which can be expressed by several ways. In programming a string is a contiguous sequence of symbols or values such as a character string or digit string. Strings is declared in a '....' (single quotes) or "...." (double quotes).     By using  '\n'  you can insert the string to the new line by using  print()  command. For example:  We can concatenate string by using + operator. Concatenate means we can merge two or more separate strings by using + operator. For example: We see another example of multiple concatenate. Here 3*'py' means the string 'py' repeats 3 times and same for the 'thon' string. Now we another method to concatenate two or more strings. In this method you have write strings next to each other without any operator. For example: In the python string is stored in indexes. The index is start from 0 (zero). In python indexes also be calculated from reve...

Python | Data Structures or Data Types (List)

Data Type of Python In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types. Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types. Now we see data types one by one! 1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[]. Empty List-  L = [ ] List with multiple elements -  L1 = [10, 15.10, 'Python', [10,20] ] In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in ...

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...