Python. Statement for. Examples

Python. Statement for. Examples


Contents


Search other websites:

 
1. Purpose of the for loop operator. What forms does the for loop operator have?

The for loop is used to bypass the elements of a sequence. Sequences can be lists, tuples, strings, or other objects that allow iterations. In a for loop, the cyclic process is obtained from a sequence of iterations.

In Python, the for loop has the following varieties (forms):

  • a full form is a form that provides for an else block. In this case, the break statement must be used in the loop body;
  • the abbreviated form is a form that does not include an else block.

 

2. The full form of the for statement, providing for the use of the break and continue instructions

The full form of the for statement, which provides for the use of the break and continue instructions, is as follows:

for <variable> in <object>:
    <statements1>
    if <condition1>: break
    if <condition2>: continue
else:
    <statements2>

here

  • variable – a loop variable that in turn takes the value of the elements from the object;
  • object – some list, tuple, string, or other object that contains multiple items to bypass;
  • statements1 – one or more statements (instructions) that are executed for the specified variable value;
  • statements2 – one or several statements (instructions) that are executed if the break instruction in the body of the for loop has not been executed once.

The above for loop works as follows. In a loop, the variable sequentially takes the value from the first to the last item of the sequence.

In the loop, alternately one after the other, the cycle variable is assigned the elements of the sequence object. For each of the sequence elements, the body of the loop is executed. If, during the passage of the sequence, the break instruction was not executed even once, then the transition to the else block occurs.

 

3. Abbreviated form of the for statement (without using the else block)

The for statement can be applied in abbreviated form without using an else block. In this case, the abbreviated form of the for operator is:

for <variable> in <object>:
    <statements>

here

  • variable – a loop variable that in turn takes the value of the elements from the object;
  • statements – one or more statements (instructions) that are executed for the given variable value.


 

4. Examples of using the for statement that does not contain an else block
4.1. An example of calculating the sum of the items of the list
# calculating the sum of the list items
# The specified list
T = [2.8, 3.5, 4.9, 0.01, 2.34]
s=0
for t in T:
    s = s + t
print("s = ", s) # s = 13.549999999999999

As a result of executing the above code, the following result will be displayed

s = 13.549999999999999

 

4.2. An example of calculating the maximum value in the list

Given a list of real numbers. Find the maximum value in the list

# search for the maximum value in the list
# the specified list
T = [1.8, 5.2, 10.9, 8.1, 2.4]
max = T[0] # first list item

for t in T:
    if max<t:
        max=t
print("max = ", max) # max =   10.9

As a result of executing the above code, the following result will be obtained.

max = 10.9

 

4.3. An example of calculating the sum of the items of a tuple that are within 5 … 10
# The sum of the items of the tuple, which are within 5..10
# Specified tuple
C = (3, 5, 6, 4, 2, 8, 1, 9)
s = 0 # the required sum

# calculation
for c in C:
    if (c>=5)and(c<=10):
        s=s+c
print("Sum = ", s) # Sum =   28

 

4.4. An example of finding a string in the string list

Given a string item and a list of strings S. It is necessary to determine the positions of the occurrence of the item element in the list S. Positions of entries display on screen. If the item element is never found in the S list, then display the corresponding message.

Solving this problem using a for loop can be, for example, the following:

# search for a string in the string list
# The specified list
S = ['abc', 'def', 'hij', 'abc', 'fhg', 'jkl', 'jpr']

item = str(input("Enter the item you want to find: "))

P=[] # positions of entry of the item in the list S

# calculation
i=0
for s in S:
    if s==item:
        P=P+[i]
    i=i+1

if len(P)>0:
    print("Positions the entry of the item ", item, " in the list")
    print(P)
else:
    print("The item ", item, " is not in the list")

The result of the above code can be as follows

Enter the item you want to find: abc
Positions the entry of the item abc in the list
[0, 3]

 

5. An example of using a for statement containing an else block

The example determines the ownership of the specified item of the tuple with the display of the corresponding message.

# Determining whether a given item belongs to a tuple
# Specified tuple
C = (3, 5, 6, 4, 2, 8, 1, 9)

# input of item
item = int(input("Input the item you are looking for: "))

# calculation
for c in C:
    if c==item:
        print("Item ", item, "is in the list")
        break
else:
    print("The item ", item, " is not in the list")

The result of the above code is as follows

Input the item you are looking for: 7
The item 7 is not in the list

 


Related topics