Conditionals-for and while, Python Programming

 

For and While

 

If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types are for and while statements (often called for and while loops).

 

A for loop has the following form:

 

for in :

...

 

The interpreter starts  by calculating listExpr.  If it does not yield a list, tuple,  or string7,  an error occurs.  If it does yield a list-like structure or list, then  the block of instructions will, under normal conditions, be executed one time for every  value  in that  list.  At the end,  the variable will remain bound to the last element of the list (and  if it had  a useful  value  before the for was calculated, that number  will have been permanently overwritten).

Here is a basic for loop:

 

result = 0

for x in [1, 3, 4]:

result = result + x * x

 

At the end of this execution, result will give the value  26, and x will have the value  4.

One situation in which  the body  is not executed once for each value  in the list is when  a return statement is encountered. No situations whether value is nested in a structure or not, if it is calculated it immediately gives a problem a value to be returned from a function call. So, for example, we may  write a function that tests to see if an item is a member of a list, and gives True if it is and 0 if it is not, as given:

 

def member(x, items):

for i in items:

if x == i:

return True return False

 

The procedure loops  through all of the components in items, and  relates them  to x. As soon as it ?nds  an item i that  is same  to x, it can quit  and  return the value  True from the method. If it has all the way through the loop without giving any value, then we know  that x is not in the structure, and we may give False

 

Posted Date: 8/9/2012 7:02:51 AM | Location : United States







Related Discussions:- Conditionals-for and while, Assignment Help, Ask Question on Conditionals-for and while, Get Answer, Expert's Help, Conditionals-for and while Discussions

Write discussion on Conditionals-for and while
Your posts are moderated
Related Questions
Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

I have python game project which is due by next Tuesday. Do you think that I can get it on time if I order today?

Expressions and assignment We can think of most computer programs as performing some sort of transformation on data.  Our code may get as input the exam scores of everyone in

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then create a

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named 10.py that performs the following: Create a class named ChkAcct, and a class n

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

De?nition   A method de?nition has the abstract form: def ( , ..., ): ...   There are essentially three parts: is a name for

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many