Important Notice:

for Loop and Counter

for Loop and Counter

3 views 2 min read
for Loop and Counter :-
Frequency को for loop और counter variable की मदद से भी find किया जा सकता है।

इस method में हम List के प्रत्येक element को check करते हैं। जब required element मिलता है, तो counter की value को 1 से increase कर देते हैं।

Example :-
 
numbers = [10, 20, 10, 30, 10, 20]

search = 10
frequency = 0

for num in numbers:
    if num == search:
        frequency += 1

print("Frequency of 10:", frequency)
 
Output:
Frequency of 10: 3
 
Explanation :-

सबसे पहले:

frequency = 0

Frequency को 0 से initialize किया जाता है।

इसके बाद for loop List के प्रत्येक element को check करता है।

10 → Match → frequency = 1
20 → No Match
10 → Match → frequency = 2
30 → No Match
10 → Match → frequency = 3
20 → No Match

अंत में:

Frequency = 3

इसलिए 10 की frequency 3 है।
 
IN ENGLISH -
 
for Loop and Counter :-

The frequency can also be found using a for loop and a counter variable.

In this method, we check each element of the List. When the required element is found, we increase the value of the counter by 

Example :-
numbers = [10, 20, 10, 30, 10, 20]

search = 10

frequency = 0

for num in numbers:
    if num == search:
        frequency += 1

print("Frequency of 10:", frequency)
 
Output:-
Frequency of 10: 3
Explanation :-

First:

frequency = 0

The frequency is initialized to 0.

After that, the for loop checks each element of the List.

10 → Match → frequency = 1

20 → No Match

10 → Match → frequency = 2

30 → No Match

10 → Match → frequency = 3

20 → No Match

Finally:

Frequency = 3

Therefore, the frequency of 10 is 3.

Related Notes