Skip to main content

Iterations and Conditions in Python


What You Will Learn

  • You will learn the syntax of for, while loops and if statement in Python
  • You will apply these to solve a mathematical problem.

For loop

Python identifies code blocks by indentation. The for loop has the syntax shown below. Observe the colon at the end of the first line and the indentation at the start of the second line. Whatever is to happen under the for loop should be given as an indented block. If the indentation is wrong, it raises an exception.
for i in range(0,10):

print i

The above code prints the integers from 0 to 9. One can modify the above code for appending the values to a blank array as shown in the following code.
from scipy import *
arr=[]
for i in range(0,10):

arr.append(i)

print arr
The loops being slow, the above method is not encouraged. Instead, vectorized computation is used as shown below.
from scipy import *
arr=r_[0:10]
print arr

While loop

The syntax is
from scipy import *
arr=[]
while i<10:

arr.append(i)

i=i+1

print arr

Checking Conditions with if ... else

from scipy import *
print("Enter an integer")
x=input()
if (x<10):

print("The entered number is less than 10")


elif ((x>=10) and (x<=100)):

print("The entered number is between 10 and 100")


else:

print("The entered number is greater than 100")


The second lines reads an input from the terminal and stores in the variable x.The if, elif, else statements check the range of the number and print responses.The condition whether the value is between 10 and 100 is realized by the logical and operator. Run this code, input an integer value and appreciate.

Vacillating Mathematician

A mathematician starts walking from her office to her office at say unit distance. When she reaches half of the distance (0.5 unit) she turns and walks back home. Again when she reaches half that distance (0.25 unit) she changes her mind and walks back to the office. When she reaches half of the remaining distance to the office (0.25+0.5*0.75 unit) she again turns back. We can ascertain her position after a few iterations with the help of the code below.

from scipy import *
from pylab import *
vals=[]
oldpos=0
for i in range(0,50):

if (i%2==0):

   newpos=oldpos-(oldpos)*0.5

else:

   newpos=oldpos+(1-oldpos)*0.5

oldpos=newpos

vals.append(newpos)

plot(vals)
grid('on')
show()

One can see that the mathematician ends up between 1/3 and 2/3 after a few iterations.

What You Learned

  • You understood the syntax of for, while loops and if statements
  • You solved the vacillating mathematician problem.

Comments

Popular posts from this blog

Functions in Python

Functions in Python Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You will Learn You will learn the syntax of Python functions You will learn to import the functions to other codes The Syntax The syntax used the keyword $def$ followed the name of the function again followed by a colon (:) that is then followed by an indented code block of instructions. The function def function_name(arg1.arg2,....argN): statements return results A Simple Python Function Needless to say that the statements and the return command should form an indented block. Consider a simple function that returns the sum of the input values. It is realized as the function below. def summ(x,y): return x+y Unlike MATLAB or IDL, it is possible to execute a function in the Python shell. You may type the above function in a Python shell or an enhanced Python shell such as IPython. Once the ...

Ex:2 Familiarization of Scientific Computing