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

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Python | Data Structure Or Data Types (Sets)

Data Type Of Python In this section we will see new data type which is Sets. 3) Sets:- Set object is an unordered collection of distinct unhashable object. The common user include membership test removing duplicate from a sequence and computing mathematical operation such as intersection, union, differences etc.     Being an unordered collection set do not record element position or order of insertion. Accordingly set do not support indexing, slicing, stepping or other sequence like behavior.      We declare a set by using curly braces {}. Note:- Set can only store immutable data. Empty set:-  S = set() Set with multi element:- S = {10, 'Hi', (10, 20)} Membership:- print(10 in S) => True print(20 in S) => False Iteration:- for i in S:       print(i)  => 10       'Hi'       (10,20) Type casting:- S = "Programming" S2 = set(S) print(S2) => {'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'} In the ...

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 ...