Conditionals-for and while, Python Programming

Assignment Help:

 

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

 


Related Discussions:- Conditionals-for and while

Data wrangling, Ask quesICT702 TASK 2 1 2018 Semester 2 Data Wrangling Due ...

Ask quesICT702 TASK 2 1 2018 Semester 2 Data Wrangling Due in two parts - Friday of Week 9 and Week 12 Below Zero - ice cream store The local ice-cream store needs a new ordering s

Lists, simple program using list

simple program using list

Interaction and debugging, We encourage you to adopt an interactive style o...

We encourage you to adopt an interactive style of debugging and programming. Use the Python shell a lot. Write short pieces of code and check them.  It is much  simpler to test the

Environments in python, Environments in Python Generally, Python estab...

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Particle Movement, Imagine a "particle" located on the centre square of a t...

Imagine a "particle" located on the centre square of a two-dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right

Lab programming, protocol rdt2.2 considers there is a bit errors between se...

protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

Hw, Suppose the cover price of a book is $24.95, but bookstores get a 40% ...

Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the tota

Robotics, how to started robotics proggraming in begning

how to started robotics proggraming in begning

assignment 10, #question.Program 10 Assignment (Banking Objects – Savings/...

#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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd