Blogger Themes

Search This Blog

www.embeddedstudy.com | Copyright © 2017 | All Rights Reserved | Nithin Pradeep . Theme images by Storman. Powered by Blogger.

Follow by Email

Popular KOZHI

Sponsor

Download

Blogger Tricks

FEATURED

What is an Embedded System?

An embedded system is a combination of hardware and software that is designed to carry out a certain task or tasks, meaning it has a s...

JOIN THE TEAM

Popular Posts

Wikipedia

Search results

Search This Blog

Social

More Links

Social

ad

ads

Thursday, 17 August 2017

Control Statements in Python


TOPICS COVERED IN THIS SECTION
Control Statements

  1. Indentation in Python
  2. if,else,elif,nested if,else-if ladder
  3. For and while loops.
  4. Basic loop programs and Pattern printing.
  5. Range() and Xrange() functions.

In this section we will discuss about control statements in python...

control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement, or decides which of two statements to execute. A loop decides how many times to execute another statement.

Before that let us see what is indentation in python

what is indentation?

   One of the most distinctive features of Python is its use of indentation to mark blocks of code.To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.
IF and ELSE Condition
if pwd == 'apple':
    print('Logging on ...')
else:
    print('Incorrect password.')

print('All done!')

The lines print('Logging on ...') and print('Incorrect password.') are two separate code blocks. These ones happen to be only a single line long, but Python lets you write code blocks consisting of any number of statements.
if for any non block elements if we give indentation it will give error.
If for writing dummy statements write pass as statement.

If/elif-statements

An if/elif-statement is a generalized if-statement with more than one condition. It is used for making complex decisions.
# airfare.py
age = int(input('How old are you? '))
if age <= 2:
    print(' free')
elif 2 < age < 13:
    print(' child fare)
else:
    print('adult fare')
After Python gets age from the user, it enters the if/elif-statement and checks each condition one after the other in the order they are given. So first it checks if age is less than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. If age is not less than 2, then it checks the next elif-condition to see if age is between 2 and 13. If so, it prints the appropriate message and jumps out of the if/elif-statement. If neither the if-condition nor the elif-condition is True, then it executes the code in the else-block.

Nested if-statements

Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
Syntax:

for iterator_var in sequence:
    for iterator_var in sequence:
        statements(s)
        statements(s)
The syntax for a nested while loop statement in Python programming language is as follows:
while expression:
    while expression: 
        statement(s)
        statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.

While Loop


This will prints in loop 5 times along with numbers and at last prints loop terminated.

Now if we want to provide conditions inside the loop.


Another salient feature of python is that it can use else condition with while loop,
 


this will print up to 012 and also else part is executed.

For Loop

Unlike in C pythons for loop format is:
                                                            for variable in list
Let us see one example:

OUTPUT:

But by this method rotating a loop 500 or 1000 times will be difficult so for that to over come and for printing an arithmetic series like 2,4,6,8,10.... we use range() and xrange().

OUTPUT:

ASSIGNMENT:

Write a program to find even numbers between 0 and 20.
Ans:


What is the difference between range() and xrange() then?  
Range() generates list of elements,and stores them in memory,So if we generate 1 Billion data then memory will not be sufficient.
Xrange() is a generator and data is not stored in the memory.It will return an Object so creation of large data is possible.

Xrange() is not available in python 3,In python 2 range() will create a list and in Python3 it will work as a generator .Return value is an object.

ASSIGNMENT:

Write a program to print the following patterns.
*
* *
* * * 
* * * * 
* * * * *

ANS:

2)

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

ANS:

In the next section we will learn about functions in Python....
Thank You..

0 on: "Control Statements in Python"