The For loop is used when we want to do something with the elements of iterable objects like strings, tuples, list, dictionary or any user defined objects. For example a string like "meter" consist of individual letters- m, e, t, e and r and we can use the for loop to go through each of these letters. Tuples are ordered list but not mutable. For example (x, y, z) is a tuple in Python with specific ordering of elements that cannot be changed. A List such as ["position", "velocity", "acceleration"] has also iterable elements with fixed positional ordering. Similarly, a dictionary declared in curly braces {"position": 10, "velocity": 6, "acceleration": 3} has also iterable elements called items with key as the index-able part.

for loop


For loop can be any thing we want to do something with the elements.

Let's start with for loop with string. Consider the following example code.

s = 'The acceleration due to gravity is 9.8 ms^2'snew = ''
for x in s:
    if x == ' ':
        snew = s.replace(' ', ',')

print(snew)

In the code example above, s is a string and snew is an empty string. We literate over each elements or characters of the string using the for loop. We can use the for loop to check each character for certain occurrence of character. That is we can check whether the string has any alphabetic character, numbers character or any other character. In this python program, we check or rather look for space character and when we find it replace the space with a comma. This python program can thus be used to convert a text file containing strings with text with words separated by commas.

Below is the output of the above program.

The,acceleration,due,to,gravity,is,9.8,ms^2


Consider using for loop with tuples. Let L be a list of 3 tuples, then we can use the for loop to print the tuples as follows:

L = [(2, 3), (7, 4), (8, 5)]

for a,b in L:
    print(a, b)


The output is as follows:

2 3
7 4
8 5

Next consider List example. For the first example of using for loop with list, consider we have a list of numbers and we want to calculate the sum of its elements.

L = [1, 4, 7, 3, 9, 4, 5]
sum = 0
for k in L:
    sum = sum + k

print(sum)


The output is:

33

For another example of using for loop with list, consider a list of words. We can analyze the words using the for loop, for simple example, test whether certain word exist in the list.

L = ["john", "micheal", "rommy", "hitman", "ostin"]
mem = input('Enter your name: ')

for k in L:
    if k == mem:
        print(mem,'a is member')
        break    else:
        print(mem,'is not a member')
        break

Here the words are names of people.  The for loop is used to test whether an input from the user exist in the list or not. Following are outputs of this python program.

Enter your name: john
john a is member

and,

Enter your name: emmpy
emmpy is not a member

for loop with dictionary

 Likewise, for loops can be used with dictionary. The for loop can be used to scan through the keys of the dictionary and do something with the values of the dictionary.

Consider the following example code.

D = {'name': 'john', 'addr': 'street 14', 'age': 24, 'job': 'mgr'}
x = input('What do you want to know?(name, addr, age, job): ')

for key in D:
    if x in D:
        print(D[x])
        exit()
    elif key != x:
        print('Enter: name, addr, age or job')
        break

 We have a dictionary D which contains information about a person. An input prompt asks a user to input what he want wants to know about the person. If the input matches the key of the dictionary then the corresponding value is outputted.

Below is the sample input and output of this python program,

What do you want to know?(name, addr, age, job): asdf
Enter: name, addr, age or job

and,

What do you want to know?(name, addr, age, job): addr
street 14

0 comments:

Post a Comment

 
Top