Showing posts with label Flow Control in Python.. Show all posts
Showing posts with label Flow Control in Python.. Show all posts

Tuesday, December 3, 2019

The range( ) function

  • It generates a list of numbers, which is generally used to iterate over with for loop. 
  • range( ) function uses three types of parameters, which are: 
  • start: Starting number of the sequence.
  • stop: Generate numbers up to, but not including last number.
  • step: Difference between each number in the sequence. 

Python use range( ) function in three ways: 

  1. range(stop)
  2. range(start, stop)
  3. range(start, stop, step) 

Note: 


  • All parameters must be integers. 
  • All parameters can be positive or negative.

1. range(stop): By default, It starts from 0 and increments by 1 and ends up to stop, but not including stop value. 
Example: 
for x in range(4): 
print(x) 
Output: 

2. range(start, stop): It starts from the start value and up to stop, but not including stop value. 
Example: 
for x in range(2, 6): 
print(x) 
Output: 
3. range(start, stop, step): Third parameter specifies to increment or decrement the value by adding or subtracting the value. 
Example: 
for x in range(3, 8, 2): 
print(x) 
Output: 
7

Explanation of output: 

3 is starting value, 8 is stop value and 2 is step value. First print 3 and increase it by 2, that is 5, again increase is by 2, that is 7. The output can’t exceed stop-1 value that is 8 here. So, the output is 3, 5, 8. 

Difference between range( ) and xrange( ):


Loops in Python

Another one of the control flow statements is loops. 
  • Execute a set of statements that are repeated until a particular condition is satisfied.
  • Loops are used when we want to repeat a block of code to run a number of times.


Basically there are two types of loops in python programming


  1. for loop
  2. while loop 

For loop

For loop is a control flow statement in which the control of the program is continuously transferred to the beginning to execute the body of for loop for a fixed number of times.

Syntax of for loop
                            for a in sequence:
                            body of for
  1. The for loop iterate over a given sequence (it may be list, tuple or string). 
  2. Note: The for loop does not require an indexing variable to set beforehand, as the for command itself allows for this. 

Example
primes = [2, 3, 5, 7] 
for x in primes: 
print(x)

while loop

  • With the help of while loop we can execute a set of statements as long as a condition is true. 
  • It requires to define an indexing variable.

Syntax of While Loop
                while test_expression:
                body of while

Example
To print table of number 2
                        i=2
                        while i<=20:
                        print(i)
                        i+=2

Note:- While other languages contain conditions and increment expression in the syntax of for loop, in Python, the iteration and incremented value are controlled by generating a sequence. 

The range( ) function:

Friday, November 29, 2019

Flow Control in Python.

In this session of flow control and looping in python we will learn about working of loop and controlling the flow of statements, there has always been a series of statements faithfully executed by Python in exact top-down order. What if you wanted to change the flow of how it works? 



  • For example, you want the program to take some decisions and do different things depending on different situations, such as printing 'Good Morning' or 'Good Evening' depending on the time of the day?

According to the above situation it can be categorized into three parts.
  • Decision Making and branching (Conditional Statement)
  • Looping or Iteration
  • Jumping statements


Decision making is about deciding the order of execution of statements based on certain conditions. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome.

There are three types of conditions in python:
  1. if statement
  2. if-else statement
  3. elif statement

if statement: 

  • It is a simple if statement. When condition is true, then code which is associated with if statement will execute.


Example:


a=40
b=20
if a>b:
print(“a is greater than b”)

2. if-else statement: 

  • When the condition is true, then code associated with if statement will execute, otherwise code associated with else statement will execute.
Example:

a=10
b=20
if a>b:
print(“a is greater”)
else:
print(“b is greater”)

3. elif statement: 

  • It is short form of else-if statement. If the previous conditions were not true, then do this condition". It is also known as nested if statement.
Example:

a=input(“Enter first number”)
b=input("Enter Second Number:")
if a>b:
print("a is greater")
elif a==b:
print("both numbers are equal")
else:
print("b is greater")